2010年6月30日 星期三
2010年5月22日 星期六
Java loop 尋找特定的附檔名
File file = new File(".");//.表示目前目錄
File[] files = file.listFiles();
for (File f : files) {
if (f.toString().endsWith(".csv")) {
System.out.println(f.toString());
}
}
2009年7月29日 星期三
Java heap space
2009年7月25日 星期六
jdom xpath java解析xml的套件 demo 3

import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.xpath.*;
import org.jdom.Text;
/*
phone.xml
<?xml version="1.0" encoding="UTF-8"?>
<friends comment="Friends List">
<friend number="1">
<name>張三</name>
<sex value="male" />
<phone type="home">02-55556666</phone>
<phone type="mobile">0931555645</phone>
</friend>
<friend number="2">
<name>李四</name>
<sex value="male" />
<phone type="home">02-36541234</phone>
<phone type="mobile">0932333666</phone>
</friend>
<friend number="3">
<name>王五</name>
<sex value="female" />
<phone type="home">02-80213698</phone>
<phone type="mobile">0939222333</phone>
</friend>
</friends>
*/
public class XPathReader {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
String name = null;
String sex = null;
String phone_home = null;
String phone_mobile = null;
String s = null;
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("phone.xml"));
Element root = doc.getRootElement();
List list = XPath.selectNodes(root,"/friends/friend");
Iterator i = list.iterator();
int j = 1;
while (i.hasNext()) {
Element element = (Element) i.next();
String number = element.getAttributeValue("number");
name = ( (Text) XPath.selectSingleNode(element,"//friend[@number='" + number + "']/name/text()")).getTextNormalize();
sex = ( (Element) XPath.selectSingleNode(element,"//friend[@number='" + number + "']/sex")).getAttributeValue("value");
phone_home =( (Text) XPath.selectSingleNode(element,"//friend[@number='" + number + "']/phone[@type='home']/text()")).getText();
phone_mobile =( (Text) XPath.selectSingleNode(element,"//friend[@number='" + number + "']/phone[@type='mobile']/text()")).getText();
s = "name "+name+",sex "+sex+",phone_home "+phone_home+",phone_mobile "+phone_mobile;
out.println(s);
}
} catch (JDOMException e) {
e.printStackTrace();
}
}
}
/* result
name 張三,sex male,phone_home 02-55556666,phone_mobile 0931555645
name 李四,sex male,phone_home 02-36541234,phone_mobile 0932333666
name 王五,sex female,phone_home 02-80213698,phone_mobile 0939222333
*/
jdom xpath java解析xml的套件 demo 2

catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<composition composer="cc">www
<station ci="11">
<address>tw</address>
</station>
<station ci="10">
<address>jp</address>
</station>
<title>Trio for Flute, Viola and Harp</title>
</composition>
<composition composer="c2">
<station ci="15">
<address>am</address>
</station>
<title>Charmonium</title>
</composition>
</catalog>
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.xpath.*;
public class XPathReader {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("catalog.xml"));
XPath Path = XPath.newInstance("//composition/station");
List list = Path.selectNodes(doc);
Iterator i = list.iterator();
int j = 1;
while (i.hasNext()) {
Element el = (Element) i.next();
if (el != null) {
//列印//composition/station[@ci]
out.println(j+" "+ el.getAttribute("ci"));
//列印composition/station/address
out.println(j+" "+ el.getChildText("address"));
//尋找父元素
out.println(j+" "+ el.getParentElement().getTextTrim());
}
j++;
}
} catch (JDOMException e) {
e.printStackTrace();
}
}
}
/* result
1 [Attribute: ci="11"]
1 tw
1 www
2 [Attribute: ci="10"]
2 jp
2 www
3 [Attribute: ci="15"]
3 am
3
*/
延伸閱讀
http://www.ibm.com/developerworks/cn/xml/x-jdom/
http://www.java3z.com/cwbwebhome/article/article2/2282.html?id=810
2009年7月24日 星期五
jdom xpath java解析xml的套件
web.xml檔案內容

http://www.jdom.org/downloads/index.html
下載JDOM 1.1 is the latest release build.
jdom.jar
jaxen-core.jar
jaxen-jdom.jar
saxpath.jar
xalan.jar
xerces.jar
xml-apis.jar
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public static void main(String[] args) throws Exception {
String file = "test.xml";
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(file));
XPath servletPath = XPath.newInstance("//servlet");
// services/service[@type='interval']
List servlets = servletPath.selectNodes(doc);
Iterator i = servlets.iterator();
while (i.hasNext()) {
Element servlet = (Element) i.next();
String s = servlet.getChild("servlet-name").getTextTrim();// ok
System.out.println("w " + sdate);
}
}
------------------------
result
servlet-name => snoop
servlet-name => file

http://www.jdom.org/downloads/index.html
下載JDOM 1.1 is the latest release build.
jdom.jar
jaxen-core.jar
jaxen-jdom.jar
saxpath.jar
xalan.jar
xerces.jar
xml-apis.jar
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public static void main(String[] args) throws Exception {
String file = "test.xml";
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(file));
XPath servletPath = XPath.newInstance("//servlet");
// services/service[@type='interval']
List servlets = servletPath.selectNodes(doc);
Iterator i = servlets.iterator();
while (i.hasNext()) {
Element servlet = (Element) i.next();
String s = servlet.getChild("servlet-name").getTextTrim();// ok
System.out.println("w " + sdate);
}
}
------------------------
result
servlet-name => snoop
servlet-name => file
2009年7月15日 星期三
HttpClient
HttpClient Tutorial ( HTML / PDF )
Some examples of HttpClient in action can be found here
需要
Some examples of HttpClient in action can be found here
需要
- commons-logging-x.x.x.jar commons-logging-1.1.1.jar
- commons-codec-x.x.x.jar commons-codec-1.3.jar
- httpcore-x.x.x.jar HttpCore 4.0.1 (GA)
- httpclient-x.x.x.jar HttpClient 4.0-beta2
- apache-mime4j-x.x.x.jar (*)//非必要
- httpmime-x.x.x.jar (*) httpmime-4.0-beta2.jar 在HttpClient 4.0-beta2同一個lib
2009年7月10日 星期五
ReadHtml()
public static void ReadHtml() throws Exception {
InetSocketAddress ISA = new java.net.InetSocketAddress("192.168.1.5",3082);
Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP,ISA);
String html = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm";
URL srcUrl;
BufferedReader in;
srcUrl = new URL(html);
//使用proxy
HttpURLConnection sms_gw = (HttpURLConnection) srcUrl.openConnection(proxy);
//讀取UTF-8編碼的網頁
in = new BufferedReader(new InputStreamReader(sms_gw.getInputStream(),"UTF-8"));
//不使用proxy
//in = new BufferedReader(new InputStreamReader(srcUrl.openStream(),"UTF-8"));
html="";
while ((thisLine = in.readLine()) != null) {
html += thisLine;
}
System.out.println("html \n"+html);
String stime="";
Pattern pattern = Pattern.compile("[0-9]{4}/[0-1]{1}[0-9]{1}/[0-9]{2} [0-9]{2}:[0-9]{2}");//2009/01/28 14:52
Matcher matcher = pattern.matcher(html);
while(matcher.find()) {
stime = matcher.group();
}
System.out.println(" 異動時間 "+stime);
html = html.replaceAll("\\s*", "");// 去掉空白
html = html.replaceAll(" ", "\n");// 去掉空白
html = html.replaceAll("<[^>]+>", ",");// 去掉html
InputStream in_ = new ByteArrayInputStream(html.getBytes());
BufferedReader buf = new BufferedReader(new InputStreamReader(in_));
while ((s = buf.readLine()) != null) {
if (!s.equals("")) {
System.out.println(s);
}
}
//Thread.sleep(100000);//
}
InetSocketAddress ISA = new java.net.InetSocketAddress("192.168.1.5",3082);
Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP,ISA);
String html = "http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm";
URL srcUrl;
BufferedReader in;
srcUrl = new URL(html);
//使用proxy
HttpURLConnection sms_gw = (HttpURLConnection) srcUrl.openConnection(proxy);
//讀取UTF-8編碼的網頁
in = new BufferedReader(new InputStreamReader(sms_gw.getInputStream(),"UTF-8"));
//不使用proxy
//in = new BufferedReader(new InputStreamReader(srcUrl.openStream(),"UTF-8"));
html="";
while ((thisLine = in.readLine()) != null) {
html += thisLine;
}
System.out.println("html \n"+html);
String stime="";
Pattern pattern = Pattern.compile("[0-9]{4}/[0-1]{1}[0-9]{1}/[0-9]{2} [0-9]{2}:[0-9]{2}");//2009/01/28 14:52
Matcher matcher = pattern.matcher(html);
while(matcher.find()) {
stime = matcher.group();
}
System.out.println(" 異動時間 "+stime);
html = html.replaceAll("\\s*", "");// 去掉空白
html = html.replaceAll(" ", "\n");// 去掉空白
html = html.replaceAll("<[^>]+>", ",");// 去掉html
InputStream in_ = new ByteArrayInputStream(html.getBytes());
BufferedReader buf = new BufferedReader(new InputStreamReader(in_));
while ((s = buf.readLine()) != null) {
if (!s.equals("")) {
System.out.println(s);
}
}
//Thread.sleep(100000);//
}
2009年7月6日 星期一
java-日期相減-計算秒數
Date dt1 = new Date("2009/6/2 18:00:04");
Date dt2 = new Date("2009/6/2 18:00:10");
Calendar calendar1 = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
calendar1.setTime(dt1);
calendar2.setTime(dt2);
System.out.println((calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/1000);
--
6
public static String datetime(String d1,String d2)
{
Date dt1 = new Date(d1);
Date dt2 = new Date(d2);
Calendar calendar1 = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
calendar1.setTime(dt1);
calendar2.setTime(dt2);
long l = (calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/1000;
String s = String.valueOf(l);
return s;
}
Date dt2 = new Date("2009/6/2 18:00:10");
Calendar calendar1 = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
calendar1.setTime(dt1);
calendar2.setTime(dt2);
System.out.println((calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/1000);
--
6
public static String datetime(String d1,String d2)
{
Date dt1 = new Date(d1);
Date dt2 = new Date(d2);
Calendar calendar1 = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
calendar1.setTime(dt1);
calendar2.setTime(dt2);
long l = (calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/1000;
String s = String.valueOf(l);
return s;
}
2009年6月30日 星期二
16進位轉10進位
System.out.println(Integer.parseInt("6d01",16));//6d01是16進位要轉10進位
27905
System.out.println(Integer.toString(100,2));//100是10進位要轉2進位
1100100
27905
System.out.println(Integer.toString(100,2));//100是10進位要轉2進位
1100100
2009年6月29日 星期一
java 批次顯示檔案名稱
public static ArrayList<File> showFileName() // 抓csv為附檔名
{
File file = new File(".");
File[] files = file.listFiles();
String filename;
ArrayList <File> fileList = new ArrayList <File>();
for (int i = 0; i < files.length; i++) {
filename = files[i].toString();
if (filename.substring((filename.length() - 3)).equals("csv"))
{
fileList.add(files[i]);
}
}//end for
return fileList;
}//showFileName end
{
for (int i = 0; i < files.length; i++) {
filename = files[i].toString();
if (filename.substring((filename.length() - 3)).equals("csv"))
{
fileList.add(files[i]);
}
}//end for
return fileList;
2009年6月18日 星期四
ArrayList
List<String> a1 = new ArrayList<String>();
a1.add(ss);
//1.取出資料
for(int i = 0; i < list.size(); i++){
System.out.print(list.get(i) + " ");
}
//2.取出資料
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
//3.取出資料 for J2SE5
for(String s : list) {
System.out.print(s + " ");
}
a1.add(ss);
//1.取出資料
for(int i = 0; i < list.size(); i++){
System.out.print(list.get(i) + " ");
}
//2.取出資料
while(iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.print(s + " ");
}
2009年5月20日 星期三
java regex 正則表示法
String A = "2009-01-01 10:10:10FFFFF";
A.matches("\\d{4}-\\d{2}-\\d{2}.*");
A.replaceAll("F", "");//替代全部
------------------------
public static void main(String[] args) {
String s = "asa09sadas09abdsa";
Pattern pattern = Pattern.compile("09");
Matcher matcher = pattern.matcher(s);
//不分大小寫
Pattern ptn = Pattern.compile("ab|ab", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher mch = ptn.matcher(s);
while(matcher.find()) {
System.out.println("比對符合"+matcher.group());
System.out.println("出現的位置"+matcher.start());
System.out.println("結束的位置"+matcher.end());
}
}
A.matches("\\d{4}-\\d{2}-\\d{2}.*");
A.replaceAll("F", "");//替代全部
------------------------
public static void main(String[] args) {
String s = "asa09sadas09abdsa";
Pattern pattern = Pattern.compile("09");
Matcher matcher = pattern.matcher(s);
//不分大小寫
Pattern ptn = Pattern.compile("ab|ab", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher mch = ptn.matcher(s);
while(matcher.find()) {
System.out.println("比對符合"+matcher.group());
System.out.println("出現的位置"+matcher.start());
System.out.println("結束的位置"+matcher.end());
}
}
2009年5月19日 星期二
java split 字串切割
String Line1 = "a|b|c|hello";
String Line2 = "a,b,c,hello";
String[] w1 = new String[20];
String[] w2 = new String[20];
w1 = Line1.split("\\|");
w2 = Line2.split(",");
--------
w1[0] = a
w1[1]=b
String Line2 = "a,b,c,hello";
String[] w1 = new String[20];
String[] w2 = new String[20];
w1 = Line1.split("\\|");
w2 = Line2.split(",");
--------
w1[0] = a
w1[1]=b
java 顯示目錄檔案
public ArrayList getFileList() {
java.util.ArrayList array1 = new java.util.ArrayList();
try {
java.io.File folder = new java.io.File(".");
String[] list = folder.list();
for (int i = 0; i < list.length; i++) {
if (list[i].substring(list[i].length() - 3).equals("txt")) {
array1.add(list[i]);
System.out.println("File => " + list[i]);
}
}
} catch (Exception e) {
System.out.println("資料夾不存在");
}
return array1;
}
java.util.ArrayList array1 = new java.util.ArrayList();
try {
java.io.File folder = new java.io.File(".");
String[] list = folder.list();
for (int i = 0; i < list.length; i++) {
if (list[i].substring(list[i].length() - 3).equals("txt")) {
array1.add(list[i]);
System.out.println("File => " + list[i]);
}
}
} catch (Exception e) {
System.out.println("資料夾不存在");
}
return array1;
}
2009年5月8日 星期五
java 隨機 亂數
public static void main(String[] args) {
Random rand = new Random();
int a = rand.nextInt(5);//0,1,2,3,4 從0開始找出5個數
int b = rand.nextInt(5)+10;//10,11,12,13,14 從10開始找出5個數
System.out.print(a);
System.out.print(b);
}
----------------
public int getRandom(){
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
System.out.println("Boolean => "+rand.nextBoolean());
System.out.println("Double => "+rand.nextDouble());
System.out.println("Float => "+rand.nextFloat());
System.out.println("Gaussian=> "+rand.nextGaussian());
System.out.println("Int => "+rand.nextInt());
System.out.println("Int(5) => "+rand.nextInt(5));
System.out.println("Long => "+rand.nextLong());
return 0;
}
result:
Boolean => false
Double => 0.21165203005812516
Float => 0.45669615
Gaussian=> -1.1126663932933862
Int => -1475214918
Int(5) => 2
Long => -7153134665631835287
Random rand = new Random();
int a = rand.nextInt(5);//0,1,2,3,4 從0開始找出5個數
int b = rand.nextInt(5)+10;//10,11,12,13,14 從10開始找出5個數
System.out.print(a);
System.out.print(b);
}
----------------
public int getRandom(){
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
System.out.println("Boolean => "+rand.nextBoolean());
System.out.println("Double => "+rand.nextDouble());
System.out.println("Float => "+rand.nextFloat());
System.out.println("Gaussian=> "+rand.nextGaussian());
System.out.println("Int => "+rand.nextInt());
System.out.println("Int(5) => "+rand.nextInt(5));
System.out.println("Long => "+rand.nextLong());
return 0;
}
result:
Boolean => false
Double => 0.21165203005812516
Float => 0.45669615
Gaussian=> -1.1126663932933862
Int => -1475214918
Int(5) => 2
Long => -7153134665631835287
訂閱:
文章 (Atom)
