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) {}
}
}
}

沒有留言: