Java输入输出流

输入输出流I/O

IO流分类

  1. 按数据流的方向:输入流、输出流

  2. 按处理数据单位:字节流、字符流

  3. 按功能:字节流、处理流

    处理流和字节流应用了Java的装饰者设计模式

    处理流是对字节流的封装,最终的数据处理还是由字节流完成的

  • 在Java中所有数据都是使用流读写的,根据数据流向的不同,分为输入流和输出流两种

  • Input:从输入设备(文件、键盘等)读取到内存中

  • Output:将数据写入到各种设备

  • 键盘就是输入设备、显示器就是输出设备、文件既可以作为输入,也可以作为输出

  • 1字符 = 2字节、1字节byte = 8位bit

    Unicode:一个汉字占两个字节长度、一个英文一个字节

    UTF-8:一个中文3个字节

    有中文肯定是字符流

输入流 输出流
字节流 InputStream OutputStream
字符流 Reader Writer

⚠️:

字节流一般用来处理图像,视频,PPT,word类型的文件

字符流一般用来处理纯文本类型的文件,如TXT等

字节流可以处理纯文本文件,但是字符流不能处理图像视频等非文本类型的文件

  • 字节流与字符流转换

    转换流的作用,文本文件在硬盘中以字节流的形式存储时,通过InputStreamReader读取后转化为字符流给程序处理,程序处理的字符流通过OutputStreamWriter转换为字节流保存。

IO流特性

  1. 先进先出

  2. 顺序存取,可以一个接一个地往流中写入一串字节,读出时也按写入顺序读取一串字节

    RandomAccessFile是随机文件操作,一个独立的类,可以从文件的任意位置进行存取操作

  3. 只读或只写,不能同时具备两个功能。如果又要写入又要读取,要分别提供两个流

IO流常用到的五类一接口

类:File、OutputStream、InputStream、Writer、Reader

接口:Serializable

字节流和字符流的区别

  • 字节流没有缓冲区,是直接输出的,字符流是输出到缓冲区的。在输出时,字节流不调用close()方法时,信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,信息才输出。要想字伏流在未关闭时输出信息,则需要手动调用flush()方法。

  • 读写单位不同

  • 处理对象不同

    只要是处理纯文本数据,优先考虑使用字符流,除此之外都使用字节流。

System类对IO的支持

System.in 标准输入,键盘输入

System.out标准输出,写往显示器

System.err标准错误输出,写往显示器

处理流

BufferedReader,BufferedWriter,BufferedInputStream,BufferedoutputStream

处理流是在节点流的基础之上进行的,带有Buffered的流又称为缓冲流,缓冲流处理文件的输入输出的速度是最快的,所以一般缓冲流的使用比较多。

各种流的eg

/*
BufferedInputStream、BufferedOutputStream缓冲字节流
是为高效率设计的,真正的读写操作还是靠FileOutputStream和FileInputStream
*/
public class IOTest{
  pubic static void write(File file) throws IOException{
    //缓冲字节流
    BufferedOutputStream bis = BufferedOutputStream(new FileOutputStream(file, true));
    //要写入的字符串
    String string = "Shaking Chloe gonna make U moving!";
    //写入文件
    bis.write(string.getBytes());
    //关闭流
    bis.close();
  }
  
  public static String read(File file) throws IOException{
    BufferedInputStream fis = new bufferedInputStream(new FileInputStream(file));
    //一次性取多少字节
    byte[] bytes = new byte[1024];
    //用来接收读取的字节数组
    StringBuilder sb = new StringBuilder();
    //读取到的字节数组长度,为-1时表示没有数据
    int length = 0;
    //循环取数据
    while((length = fis.read(bytes))!= -1){
      //将读取到的内容转换成字符串
      sb.append(new String(bytes, 0, length));
    }
    //关闭流
    fis.close();
    return sb.toString();
  }
}
/*
字符流:InputStreamReader、OutputStreamWriter
字符流适用于文本文件的读写,OutputStreamWriter类其实也是借助FileOutputStream类实现的
即
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true),"UTF-8");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"UTF-8");
*/
/*
字符流便捷类:FileWriter、FileReader
FileWriter fw = new FileWriter(file, true);
FileReader fd = new FileReader(file);
*/
/*
字符缓冲流:BufferedReader、BufferedWriter
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
BufferedReader br = new BufferedReader(new FileReader(file));
*/

IO流对象

File类
public class FileTest{
  public static void main(String[] args) throws IOException{
    File file = new File("../file.txt");
    if(!file.exists()){ //判断文件是否存在
      file.createNewFile(); //不存在则创建文件
    }
    System.out.println(file.getAbsolutePath());//文件的绝对路径名字符串
    System.out.println(file.length());//文件的大小
    System.out.println(file.getAbsoluteFile());//文件的绝对路径名形式
    file.delete();//删除文件或目录
  }
}

IO流方法

字符缓冲流有两个独特的方法:

BufferedWriter类newLine():写入一个行分隔符,这个方法会自动适配所在系统的行分隔符
BufferedReader类readLine():读取一个文本行

posted @ 2021-01-25 16:08  GladysChloe  阅读(170)  评论(0)    收藏  举报