2010年8月28日 星期六

Java 由指令讀取文字檔

ref http://tw.group.knowledge.yahoo.com/java-ppl/listitem/view?iid=20

重點:呼叫參數、賭取檔案方式

程式名稱 readfile.java

文字檔 st.txt (內容為數句英文句子)

[執行]

javac readfile.java

java readfile st.txt

[結果]

印出st.txt 的內容



import java.io.*;

public class readfile{
public static void main (String args[]){

try{
FileReader fw= new FileReader(args[0]);
BufferedReader buf = new BufferedReader(fw);
String s;

while ( (s=buf.readLine() ) != null)
System.out.println(s);
buf.close();
}
catch (IOException e){
System.out.println("Error: " + e.toString());
}
}
}

按鍵精靈 小程式

其中有用的是,搜尋視窗名稱,切換到視窗


Hwnd = Plugin.Window.Search("Zcoco")
Hwnd = replace(Hwnd, "|" , "") //Hwnd 為陣列型式,如有搜尋到多個,將以 | 分隔,固去除 | ,讓切換視窗能夠順利執行
Call Plugin.Window.Active(Hwnd)




'==========以下是按鍵精靈錄製的內容
Hwnd = Plugin.Window.Search("Zcoco")
Hwnd = replace(Hwnd, "|" , "")
Call Plugin.Window.Active(Hwnd)
// MsgBox "find " & Hwnd

//MoveTo 509, 548
//Delay 8
MoveTo 509, 549
Delay 360
LeftClick 1

KeyDown "Enter", 1
Delay 71
KeyUp "Enter", 1
Delay 1
Delay 100
KeyDown "Up", 1
Delay 2
KeyUp "Up", 1
Delay 2000

Hwnd = Plugin.Window.Search("Eclipse")
Hwnd = replace(Hwnd, "|" , "")
Call Plugin.Window.Active(Hwnd)
//MsgBox "find " & Hwnd

// KeyDown "Alt", 1
// KeyUp "Tab", 1
// Delay 200
// Delay 18
// KeyUp "Alt", 1
Delay 500
KeyDown "Ctrl", 1
Delay 250
Delay 6
KeyDown "F11", 1
Delay 161
KeyUp "F11", 1
Delay 25
KeyUp "Ctrl", 1
Delay 2000

Hwnd = Plugin.Window.Search("Zcoco")
Hwnd = replace(Hwnd, "|" , "")
Call Plugin.Window.Active(Hwnd)


//KeyDown "Alt", 1
//Delay 374
//KeyUp "Tab", 1
//Delay 369
//KeyUp "Alt", 1
//Delay 100
'==========以上是按鍵精靈錄製的內容==========

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();
}
}