java高级:IO流

1.节点流

字节节点流: 万能流

FileInputStream: 文件字节输入流

FileOutputStream: 文件字节输出流

字符节点流: 对文本文件进行读写: word文档不是文本文件

FileReader :文件字符输入流

FileWriter :文件字符输出流

1.1 FileInputStream: 文件字节输入流

构造方法:

常用方法:

    • voidclose() 关闭此文件输入流并释放与流相关联的任何系统资源。
    • intread() 从该输入流读取一个字节的数据。
      intread(byte[] b) 从该输入流读取最多 b.length个字节的数据为字节数组。
      intread(byte[] b, int off, int len) 从该输入流读取最多 len字节的数据为字节数组。

注意事项:

字节输入流的read()方法如果读到文件末尾, 返回-1

//read() 一次只读一个字节  编码  返回的读到内容编码
//read(byte[] b) 把读到字节缓存到byte数组中 返回的是 读到的字节长度
 public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("d:/hello.txt");//1.创建IO流对象
            int rs = -1;//设一个标志值,如果返回-1则表示文件已读完毕
            byte[] b = new byte[5];
            while((rs=fis.read(b)) != -1){
                //read() 一次只能读一个字节
                //read(byte[] b) 把读到字节缓存到byte数组中 返回的是 读到的字节长度
                //System.out.print((char)rs);//把byte[] 转换为字符串
                System.out.println(rs);
                //String(byte[], offset, len)
                // 从byte[] 数组中指定offset下标开始, 读len个字节
                String s = new String(b,0,rs);
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fis != null){//如果文件不为空则关闭
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1.2FileOutp

utStream: 文件字节输出流

构造函数:

常用方法:

    • voidwrite(byte[] b)b.length个字节从指定的字节数组写入此文件输出流。
      voidwrite(byte[] b, int off, int len)len字节从位于偏移量 off的指定字节数组写入此文件输出流。

注意事项:

如果构造函数中的file参数的文件不存在, 会自动创建文件, 但是如果目录不存在, 无法创建

如果在创建对象时没有指定append参数, 默认值:false,表示不追加,即每次往文件写内容, 先把文件的内容清空,再写(覆盖之前的文件内容),如果手动设置append的参数为true, 表示追加

public static void main(String[] args) {
        FileOutputStream  fos = null;
        try {
            fos = new FileOutputStream("d:/a.txt",true);//1.创建IO对象
            String  str = "\nhello world\n";
            byte[] bytes = str.getBytes();//需要把字符串转换为byte数组
            fos.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fos!=null){//如果文件不为空则关闭
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

1.3 FileReader :文件字符输入流

构造函数:

 

 public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("d:/hello.txt");//1.创建IO流对象
            int rs = -1;//设一个标志值,如果返回-1则表示文件已读完毕
            while((rs = fr.read()) != -1){
                System.out.println((char)rs);
            }
        } catch (
                FileNotFoundException e) {
            e.printStackTrace();
        } catch (
                IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fr != null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1.4FileWriter :文件字符输出流

构造方法:

public static void main(String[] args) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("d:/a.txt",true);//1.创建IO对象
            String  str = "\n你好 世界\n";
            fw.write(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fw!=null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2.缓冲流(处理流)

2.1BufferedReader: 缓冲字符输入流

构造方法:

 

处理流: 单独使用, 一定需要接在其他流上, 节点流

        ObjectOutputStream oos = null;
     FileOutputStream fos = null;
     fos = new  FileOutputStream("stu.txt");
     oos = new ObjectOutputStream(fos);

BufferedWriter 缓冲输出流: 有一个缓冲区: 8KB

write() 写入到缓冲区,

  1. 如果缓冲区满了, 自动把缓冲区的内容写到文件

  2. 如果手动调用flush() , 立即把缓冲区的内容写到文件

  3. BufferedWriter 的close() 方法, 关闭流之前, 先刷新缓冲区

2.2转换流

InputStreamReader : 把一个字节输入流转换为字符输入流

构造方法:

 

OutputStreamWriter: 把一个字节输出流转换为字符输出流

构造方法:

 

2.3打印流

只有输出流, 打印输出流: 额外提供了一个打印方法: print() println()

字节打印流: PrintStream

字符打印流: PrintWriter

PrintStream和PrintWriter提供了重载的print()、println()方法用于多种数据类型的输出。

PrintStream和PrintWriter不会抛出异常,用户通过检测错误状态获取错误信息。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值