[size=xx-large][color=orange][b]java流操作总结[/b][/color][/size]
1.控制台读取,控制台输出
2.读取文件,输出到另一个文件
1.控制台读取,控制台输出
/**
* 该类只操作文本文件,支持在文本后追加
*/
public static void saveFile2File01(String fileFromDir, String fileToDir) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileFromDir), System.getProperty("file.encoding")));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileToDir, true), System.getProperty("file.encoding")));
while (br.readLine() != null) {
bw.write(br.readLine());
bw.newLine();
}
bw.close();
br.close();
}
2.读取文件,输出到另一个文件
/**
* 该类可操作任何文件
*/
public static void saveFile2File02(String fileFromDir, String fileToDir) throws Exception {
FileInputStream fi = new FileInputStream(fileFromDir);
FileOutputStream fo = new FileOutputStream(fileToDir);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fi.read(bytes)) != -1) {
fo.write(bytes, 0, len);
}
fi.close();
fo.close();
}