2010年8月28日 星期六

Java 小程式 解析 down.dl

自己寫的一點小東西,處理ZcocoComic Viewer運作過程中,down.dl 的暫存檔,使之創造一個批次檔,直接下載檔案。

其中用到「讀檔」「寫檔」「Regular Expressions」「字串處理」

注意事項:如果要在寫入的文字檔中換行,需要加入 "\r\n"

另附 轉換String to int
int x = Integer.parseInt(str); <-------字串轉換成int的整數數值 ;



程式碼:
package downdl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ext01 {
public static void main(String[] args) throws IOException {

FileReader fr = new FileReader(
"n:/ZcocoComic_1.00_cn/support/workshop/down.dl");
BufferedReader buf = new BufferedReader(fr);
String s;
String t;
String picDir = "", pageName = "", wget = "";

String regEx = "picSrc\\[[0-9][0-9]*\\] ?= ?'";

while ((s = buf.readLine()) != null) {
s = s.replace("%7C", "|");
s = s.replace("\t", " ");


// pageName
if (s.indexOf("pageName = ") != -1) {
pageName = s.substring(s.indexOf("pageName = \"") + 12, s.lastIndexOf("\""));

System.out.println("mkdir " + pageName);
System.out.println("cd " + pageName);
}

// picDir
if (s.indexOf("url=") != -1) {
picDir = s.substring(s.indexOf("url=") + 4, s.indexOf("\";"));

}

Pattern pattern = Pattern.compile(regEx);
Matcher m = pattern.matcher(s);

while (m.find()) {

t = s.substring(m.end(), s.indexOf("';"));
System.out.println("wget \"" + picDir + t + "\"");
wget += ("wget \"" + picDir + t + "\"\r\n");

}

// System.out.println(s);

}

t = "M:/DEL/" + pageName + ".bat";

FileWriter fw = new FileWriter(t);
BufferedWriter output = new BufferedWriter(fw);
output.write("mkdir " + pageName + "\r\n");
output.write("cd " + pageName);
output.write(13); // \r 13是歸位字元
output.write(10); // \n 10是換行字元
output.write(wget);
System.out.println(t);
output.close();
}
}


沒有留言: