如下代码实现一个文件内容的拷贝,原文件为b.txt,拷贝生成的文件为b.txt_bak.
-
package com.hxl;
-
-
import java.io.BufferedWriter;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStreamWriter;
-
import java.io.StringReader;
-
-
public class MemoryInput {
-
public static void main(String[] args) throws IOException {
-
-
File file = new File("C:\\dir\\b.txt_bak");
-
// System.out.println(file.getAbsolutePath());
-
-
-
FileOutputStream fos = new FileOutputStream("C:\\dir\\b.txt_bak");
-
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
-
BufferedWriter out = new BufferedWriter(osw);
-
-
if (!(file.exists())) {
-
if (!(file.createNewFile())) {
-
System.out.println("The file created failed!");
-
}
-
System.out.println(file.getAbsolutePath());
-
}
-
;
-
-
StringReader in = new StringReader(
-
BufferedInputFile.read("C:\\dir\\b.txt"));
-
-
int c;
-
while ((c = in.read()) != -1) {
-
System.out.print((char) c);
-
if (c == '\n') {
-
out.newLine();
-
} else {
-
out.write(c);
-
}
-
}
-
out.flush();
-
}
-
-
}
-- The End --
阅读(2746) | 评论(0) | 转发(1) |