2008年3月11日 星期二

[Java]Properties檔產生法

java裡用的properties只能讀unicode...
這裡列出利用搞unicode檔案的方法...

方法1:cmd
\jdkx.x.x\bin\native2ascii.exe A檔 B檔
A檔是source, B檔是output, B檔不寫就會印在底下system.out出來

方法2:用java讀檔, 以Properties物件寫出
public void process(String sourceCSV, String outFile) {
BufferedReader br = null;
FileOutputStream fos = null;

try {
Properties p = new Properties();

br = new BufferedReader(new FileReader(sourceCSV));
while(br.ready()) {
String aRow = br.readLine();
StringTokenizer st = new StringTokenizer(aRow, ",");
if(st.countTokens() >= 2) {
System.out.println(aRow);
p.setProperty(st.nextToken(), st.nextToken());
}
}
fos = new FileOutputStream(outFile);
p.store(fos, "create by ("+sourceCSV+"), format:(key:value)");

} catch(Exception e) {
e.printStackTrace();
} finally {
if(br != null) {
try { br.close(); } catch(Exception ee) {}
}
if(fos != null) {
try { fos.close(); } catch(Exception ee) {}
}
}
}

[Java]程式轉檔編碼

讀utf-8檔寫成其他編碼, isTrans那邊是判別該檔是否含有英數字以外的字(中文)
其實這個是為了繁轉簡掃檔案用的~不過老實說~因為os的關係, wordpad跟word跟ultraedit上面看到的字編碼似乎不是那麼正確(我跟同事討論個老半天的結論, 卡住超久~囧~)
結果就在那邊一直改編碼改字排列組合測過來測過去|||orz~

void readAndWriteWord(File sourceFile, String outFile) {
if(!sourceFile.canRead()) {
return;
}
BufferedReader br = null;
FileOutputStream fos = null;
try {
boolean isTrans = false;
StringBuffer sb = new StringBuffer();
InputStreamReader ir = new InputStreamReader(new FileInputStream(sourceFile), "utf-8");
fos = new FileOutputStream(outFile);
br = new BufferedReader(ir);
int line = 0;
while(br.ready()) {
line++;
String aRow = br.readLine();
if(!isTrans) {
for(int i = 0 ; i < aRow.length() ; i++) {
if(aRow.charAt(i) > 255) {
isTrans = true;
System.out.println("---word file:"+outFile);
break;
}
}
}
//fos.write((aRow+LINE_SEP).getBytes("utf-8"));
fos.write((aRow+LINE_SEP).getBytes("GBK"));
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(br != null) {
try { br.close(); } catch(Exception ee) {}
}
if(fos != null) {
try { fos.close(); } catch(Exception ee) {}
}
}
}