记得做专业的IO异常处理!
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Created by Thekey on 2014/10/29.
* <p/>
* 复制文件的方式
*/
public class CopyText {
public static void main(String[] args) {
try {
Copy1();
} catch (IOException e) {
throw new RuntimeException("读取文件失败");
}
}
public static void Copy1() throws IOException {
FileReader fr = null;
FileWriter fw = null;
try {
fw = new FileWriter("C:\\Users\\linxz\\Desktop\\copy.txt");
fr = new FileReader("C:\\Users\\linxz\\Desktop\\FileReaderDemo.java");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
fw.write(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeException("读取失败");
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}