2009年3月28日

File encoding

Apache Commons IO
Apache Commons Lang

import java.io.*;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.io.FileUtils;

public class Main {

public static void main(String[] args) throws Exception {
String[] texts = {"台灣","中国","ニュース","한국","========="};
String encoding = "UTF-8";
File file = new File("text.txt");

PrintStream ps = new java.io.PrintStream(new FileOutputStream(file));
try {
for (String text : texts) {
ps.println(text);
}
} finally {
ps.close();
}

BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), encoding));
try {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} finally {
br.close();
}

String data =StringUtils.join(texts, SystemUtils.LINE_SEPARATOR);
FileUtils.writeStringToFile(file, data, encoding);

System.out.println(FileUtils.readFileToString(file, encoding));
}

}

沒有留言:

網誌存檔