Java IO字节流与字符流读写文件

字节流读写文件

字节流是将文件以二进制字节码的形式读写,每次读取指定多少个字节,所以字节流可以处理任何类型文件(包括图片,avi等),下面是字节流常用常用的一种用法

字节输入流:

package com.cy;

import org.junit.Test;

import java.io.*;

public class StreamDemo {

    @Test
    public void testInputStream() throws IOException {
        String inputPath = "H:\\我的文档\\java集合.doc";
        File file = new File(inputPath);
        FileInputStream fis = new FileInputStream(file);    // 创建字节输入流
        // 字节流默认一个字节一个字节读,创建byte数组来指定一次读取多少个字节
        byte[] buf = new byte[1024]; 
        int len = 0;    // 文件尾巴EOF,读取长度-1
        while((len = fis.read(buf)) != -1) {    // read一行一行读
            // 打印字节流读取的二进制数
            System.out.println(new String(buf, "utf8"));
        }
        fis.close();    // 关闭字节输出流
    }
}

字节输出流:

package com.cy;

import org.junit.Test;

import java.io.*;

public class StreamDemo {

    @Test
    public void testInputAndOutputStream() throws IOException {
        String inputPath = "H:/我的文档/java集合.doc";
        File file = new File(inputPath);
        FileInputStream fis = new FileInputStream(file);    // 创建字节输入流
        // 字节流默认一个字节一个字节读,创建byte数组来指定一次读取多少个字节
        byte[] buf = new byte[1024];    
        int len = 0;    // 文件尾巴EOF,读取长度-1
        String outputPath = "H:/我的文档/文档/java字节流.doc";
        FileOutputStream fos = new FileOutputStream(outputPath);   // 创建字节输出流
        while((len = fis.read(buf)) != -1) {    // read一行一行读
            //调用字节输出流将内容写入其他文本
            fos.write(buf, 0, len);
        }
        fos.flush();    // 刷新flush缓冲,字节流不用刷新就可以显示写入的内容
        fos.close();    // 关闭字节输入流,字节流不关闭也可以看到写入的文件
        fis.close();    // 关闭字节输出流
    }
}


字符流读写文件

字符流只能处理字符串类型数据,所以纯文本文件优先考虑字符流,每次读写两个字节,字符流有缓冲区,需要调用缓冲区的close()方法或手动调用flush()方法才可以输出信息

字符输入流:

package com.cy;

import org.junit.Test;

import java.io.*;

public class StreamDemo {

    @Test
    public void testReader() throws IOException {
        String inputPath = "H:/我的文档/java集合.doc";
        File file = new File(inputPath);
        // 创建文件输入流
        FileInputStream fis = new FileInputStream(file);
        // 创建字符输入流(可以指定编码方式)
        InputStreamReader isr = new InputStreamReader(fis, "utf-8");
        // 创建输入流缓冲区(字符流特性)
        BufferedReader br = new BufferedReader(isr);
        //经典套餐 BR--ISR--FIS
//		BufferedReader br = new BufferedReader(new InputStreamReader(
//				new FileInputStream(file),"utf-8"));
        String line;
        while((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();     // 关闭字符输入流
    }
}

字符输出流

package com.cy;

import org.junit.Test;

import java.io.*;

public class StreamDemo {

    @Test
    public void testWriter() throws IOException {
        String outputPath = "H:/我的文档/java集合.doc";
        File file = new File(outputPath);
        // 创建文件输出
        FileOutputStream fos = new FileOutputStream(file);
        // 创建字符输出流(可以指定编码方式)
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
        // 创建输出流缓冲区(字符流特性)
        BufferedWriter bw = new BufferedWriter(osw);
        //经典套餐 BW--OSW--FOS
//        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
//                new FileOutputStream(file), "utf-8"));
        String inputPath = "H:/我的文档/文档/java字符流.doc";
        FileInputStream fis = new FileInputStream(inputPath);
        InputStreamReader isr = new InputStreamReader(fis, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String line;
        while((line = br.readLine()) != null) {
            bw.write(line + "\r\n");
        }
        bw.flush();    // 刷新flush缓冲,字符流需要调用这个方法才可以显示写入的内容
        bw.close();    // 关闭字符输出流,字符流需要调用这个方法才可以显示写入的文件
        br.close();    // 关闭字符输入流
    } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值