例如:
String s 为:
123
wer
想在txt中以原格式存储,方法如下
关键点将字符流中的“/n”换成“/r/n”
public static File Filein(String filename, String s) {
FileWriter fw = null;
String separator = File.separator;
String directory = "E:" + separator + "workSpace" + separator
+ "MyEclipse";
File f = new File(directory, filename);
try {
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
fw = new FileWriter(f);
BufferedWriter out = new BufferedWriter(fw);
out.write(s.replaceAll("\n", "\r\n"), 0, s.length());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return f;
}