- package myProject0514;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class CopyTest {
-
- public static void mian(String[] args) //throws IOException
-
- {
- copy_2();
- }
- public static void copy_1() throws IOException {
- FileWriter fw = new FileWriter("Copy.txt");
- FileReader fr = new FileReader("Test.txt");
- int ch = 0;
- while ((ch = fr.read()) != -1) {
- fw.write(ch);
- }
- fw.close();
- fr.close();
- }
- public static void copy_2() {
- FileWriter fw = null;
- FileReader fr = null;
- try
- {
- fw = new FileWriter("Copy_2.txt");
- fr = new FileReader("Copy.txt");
- char[] buf = new char[1024];
- int len = 0;
- while ((len = fr.read()) != -1)
- {
- fw.write(buf, 0, len);
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("读写失败");
- }
-
- finally {
-
- if (fr != null)
- try {
- fr.close();
- }
- catch (IOException e)
- {
- }
-
- if (fw != null)
- {
- try {
-
- fw.close();
- }
-
- catch (IOException e) {
- }
- }
- }
- }
- }
复制代码 |
|