Java教程

利用Java字符流复制文本文件

本文主要是介绍利用Java字符流复制文本文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

方法一:InputStreamReader+OutputStreamWriter

需要绑定输入输出流,可以设定字符集

public class CopyFileTest2 {
    public static void main(String[] args) throws IOException {
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本3.txt");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        InputStreamReader reader = null;
        OutputStreamWriter writer = null;

        try {
            fis = new FileInputStream(file1); //文件输入流绑定源文件
            fos = new FileOutputStream(file2); //文件输出流绑定目的文件
            reader = new InputStreamReader(fis, "utf-8");
            writer = new OutputStreamWriter(fos, "utf-8");

            char[] chars = new char[64];
            //如果len==-1,表示已经读取完文件。如果len!=-1,表示这次读取的字节数目
            int len = -1;
            do {
                len = reader.read(chars); //尝试读取一个字符数组长度的数据,len接收实际读取的长度
                if (len!=-1){
                    writer.write(chars,0,len); //写入目标文件,指定输入长度==本次实际读取的长度
                }
            } while (len != -1);   //还没到末尾就继续复制

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
            if (reader != null) {
                reader.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
    }
}

方法二:FileReader+FileWriter

不需要手动绑定输入输出流,不能设定字符集

public class CopyFileTest3 {
    public static void main(String[] args) throws IOException {
        FileReader reader = null;
        FileWriter writer = null;

        try {
            reader = new FileReader("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
            writer = new FileWriter("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本4.txt");
            char[] chars = new char[64];
            int len = -1;
            do {
                len = reader.read(chars);
                if (len != -1) {
                    writer.write(chars, 0, len);
                }
            } while (len != -1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }
}

方法三:BufferedReader+BufferedWriter

缓冲字符流,有一个默认大小为8192的字符数组 char[] 作为缓冲区,在构造方法中可以指定缓冲区大小,需要绑定字符输入输出流,BufferedWriter 的 flush() 方法将缓冲区数据立即写入文件。

public class CopyFileTest4 {
    public static void main(String[] args) throws IOException {
        FileReader fr = null;
        FileWriter fw = null;
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            fr = new FileReader("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
            fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本5.txt");
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);

            char[] chars = new char[64];
            int len = -1;
            do {
                len = br.read(chars);
                if (len != -1) {
                    bw.write(chars,0,len);
                    bw.flush();  //将缓存中字符写入文件
                }
            }while (len!=-1);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                br.close();
            }
            if (bw != null) {
                bw.close();
            }
            if (fr != null) {
                fr.close();
            }
            if (fw != null) {
                fw.close();
            }
        }
    }
}

方法四:BufferedReader+BufferedWriter 按行读取与写入

BufferedReader 的 readLine() 读取一行
BufferedWriter 的 write() 写入一行
BufferedWriter 的 newLine() 写入一个换行符
BufferedWriter 的 flush() 将缓冲区字符立即写入文件

public class CopyFileTest5 {
    public static void main(String[] args) throws IOException {
        FileReader fr = null;
        FileWriter fw = null;
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            fr = new FileReader("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
            fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本6.txt");
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);

            String line = null;
            while ((line = br.readLine()) != null) { //接收一行字符
                bw.write(line); //写入改行字符
                bw.newLine();  //写入换行符
                bw.flush(); //将缓存中字符写入文件
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                br.close();
            }
            if (bw != null) {
                bw.close();
            }
            if (fr != null) {
                fr.close();
            }
            if (fw != null) {
                fw.close();
            }
        }
    }
}
这篇关于利用Java字符流复制文本文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!