使用IO流复制文本

1.使用字节流复制文本的两种方法

读一个字节,存一个字节

 @Test
    public void testCase4() throws IOException {
        File src = new File("C:\\Users\\pc\\Desktop\\test\\test.txt");
        File dest = new File("C:\\Users\\pc\\Desktop\\test\\copy.txt");
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        int ch=0;//返回读取字节的int形式,返回-1代表读完。
        while((ch=fis.read())!=-1){
            fos.write(ch);
            fos.flush();
        }
        fis.close();
        fos.close();
    }

先读到缓冲数组中,再存到文件中

 @Test
    public void testCase5() throws IOException {
        File src = new File("C:\\Users\\pc\\Desktop\\test\\test.txt");
        File dest = new File("C:\\Users\\pc\\Desktop\\test\\copy1.txt");
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        byte[] buf = new byte[10];
        int len = 0;//返回读取字节的长度,返回-1表示读完。
        while((len=fis.read(buf))!=-1) {
            fos.write(buf, 0, len);
            fos.flush();
        }
        fos.close();
        fis.close();
    }

2.使用字符流复制文本的两种方法

使用字符流复制文本的时候,出现乱码。

出现乱码的原因:文件的编码,系统的编码,java的默认编码有冲突。假如我们用FileReader这些类来读取系统文件,它调用的字符编码是java默认的UTF-8,但是一般WINDOW的TXT默认是ANSI,而自己本机的中文编码是GBK,但是FileWriter和FileReader不能指定编码方式。参考博客:https://blog.csdn.net/linshao512/article/details/7559991

 @Test
    public void testCase6() throws IOException {
        File src = new File("C:\\Users\\pc\\Desktop\\test.txt");
        File dest2 = new File("C:\\Users\\pc\\Desktop\\copy2.txt");
        FileReader fr = new FileReader(src);
        FileWriter fw = new FileWriter(dest2);
        char[] buf = new char[10];
        int len = 0;
        while((len=fr.read(buf))!=-1){
            fw.write(buf,0,len);
            fw.flush();
        }
        fw.close();
        fr.close();
    }

使用InputStreamReader和OutputStreamWriter指定编码,解决乱码问题。

@Test
    public void testCase7() throws IOException {
        File src = new File("C:\\Users\\pc\\Desktop\\test.txt");
        File dest = new File("C:\\Users\\pc\\Desktop\\copy3.txt");
        InputStreamReader isr = new InputStreamReader(new FileInputStream(src),"GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(dest),"GBK");
        char[] buf = new char[30];
        int len = 0;
        while((len=isr.read(buf))!=-1){
            osw.write(buf,0,len);
            osw.flush();
        }
        osw.close();
        isr.close();
    }

使用缓冲流复制文本,BufferedReader特有方法readLine(),读取一行数据,BufferedWriter特有方法newLine()换行。

 @Test
    public void testCase8() throws IOException {
        File src = new File("C:\\Users\\pc\\Desktop\\test\\test1.txt");
        File dest = new File("C:\\Users\\pc\\Desktop\\test\\copy4.txt");
        FileReader fr = new FileReader(src);
        FileWriter fw = new FileWriter(dest);
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);
        String line=null;//接收方法读取一行数据,返回null的时候表示读完。
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值