Java基础 IO流笔记

I/O 框架

流的概念

内存与存储设备之间传输数据的通道

流的分类

按方向【重点】

  • 输入流:将<存储设备>中的内容读到<内存>中

  • 输出流:将<内存>中的内容写到<存储设备>中

按单位

  • 字节流:以字节为单位,可以读写所有数据

  • 字符流:以字符为单位,只能读写文本数据

按功能

  • 节点流:具有实际传输数据的读写功能

  • 过滤流:在节点流的基础之上增强功能

字节流

字节流的父类(抽象类)

//InputStream 字节输入流
public int read(){}
public int read(byte[] b){}
public int read(byte[] b, int off, int len){}
​
// OutputStream 字节输出流
public void write(int n){}
public void write(byte[] b){}
public void write(byte[] b, int off, int len){}

文件字节流

文件输入流

psvm(String[] args) throws Exception{
  // 1 创建FileInputStream 并指定文件路径
  FileInputStream fis = new FileInputStream("d:\\abc.txt");
  // 2 读取文件
  // fis.read();
  // 2.1单字节读取
  int data = 0;
  while((data = fis.read()) != -1){
    sout((char)data);
  }
  // 2.2 一次读取多个字节
  byte[] buf = new byte[3]; // 大小为3的缓存区
  int count = fis.read(buf); // 一次读3个
  sout(new String(buf));
  sout(count);
  int count2 = fis.read(buf); // 再读3个
  sout(new String(buf));
  sout(count2);
  
  // 上述优化后
  int count = 0;
  while((count = fis.read(buf)) != -1){
    sout(new String(buf, 0, count));
  }
  
  // 3 关闭
  fis.close();
}

文件输出流

psvm(String[] args) throws Exception{
  // 1 创建文件字节输出流
  FileOutputStream fos = new FileOutputStream("路径", true);// true表示不覆盖 接着写 
    // 2 写入文件
  fos.write(97);
  fos.write('a');
  // String string = "hello world";
  fos.write(string.getByte());
  // 3 关闭
  fos.close();
} 

图片复制案例

// 1 创建流
// 1.1 文件字节输入流
FileInputStream fis = new FileInputStream("路径");
// 1.2 文件字节输出流
FileInputStream fos = new FileOutpuStream("路径");
// 2 边读边写
byte[] buf = new byte[1024];
int count = 0;
while((count = fis.read(buf)) != -1){
  fos.write(buf, 0, count);
}
// 3 关闭
fis.close();
fos.close();

字节缓冲流

缓冲流:BufferedInputStream/ BufferedOutputStream
  • 提高IO效率,减少访问磁盘次数

  • 数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close

// 使用字节缓冲流 读取 文件
psvm(String[] args) throws Exception{
  // 1 创建BufferedInputStream
  FileInputStream fis = new FileInputStream("路径");
  BufferedInputStream bis = new BufferedInputStream(fis);
  // 2 读取
  int data = 0;
  while((data = bis.read()) != -1){
    sout((char)data);
  }
  // 用自己创建的缓冲流
  byte[] buf = new byte[1024];
  int count = 0;
  while((count = bis.read(buf)) != -1){
    sout(new String(buf, 0, count));
  }
  
  // 3 关闭
  bis.close();
}
// 使用字节缓冲流 写入 文件
psvm(String[] args) throws Exception{
  // 1 创建BufferedInputStream
  FileOutputStream fos = new FileOutputStream("路径");
  BufferedOutputStream bis = new BufferedOutputStream(fos);
  // 2 写入文件
  for(int i = 0; i < 10; i ++){
    bos.write("hello".getBytes());// 写入8k缓冲区
    bos.flush(); // 刷新到硬盘
  }
  // 3 关闭
  bos.close();
}

对象流

ObjectOutputStream / ObjectInputStream
  • 增强了缓冲区功能

  • 增强了读写8种基本数据类型和字符串的功能

  • 增强了读写对象的功能

    • readObject() 从流中读取一个对象

    • writeObject(Object obj) 向流中写入一个对象

使用流传输对象的过程称为序列化、反序列化

在使用流传输对象(进行序列化和反序列化)时,必须要让对象实现Serializable接口

public class Student implements Serializable

序列化与反序列化

序列化

// 使用objectoutputStream实现序列化
psvm(String[] args){
  // 1. 创建对象流
  FileOutputStream fos = new FileOutputStream("d:\\st.bin");
  ObjectOutputSream oos = new objectOutputSream(fos);
  // 2. 序列化(写入操作)
  Student zhangsan = new Student("zs", 20);
  oos.WriteObject(zhangsan);
  // 3. 关闭
  oos.close();
  sout("序列化完毕");
}

反序列化

// 使用ObjectInputSteam实现反序列化(读取重构对象)
psvm(String[] args){
  // 1. 创建对象流
  FileInputStream fis = new FileInputStream("d:\\stu.bin");
  ObjectInputStream ois = new ObjectInputStream(fis);
  // 2. 读取文件(反序列化)
  Student s = (Student)ois.readObject();
  // 3. 关闭
  ois.close();
  sout("执行完毕");
  sout(s.toString());  
}

注意事项

​1. 某个类要想序列化必须实现Serializable接口
​
2. 序列化类中对象属性要求实现Serializable接口
​
3. 序列化版本号ID,保证序列化的类和反序列化的类是同一个类
​
4. 使用transient修饰属性,这个属性就不能序列化
​
5. 静态属性不能序列化
​
6. 序列化多个对象,可以借助集合来实现
​
   

字符编码

ISO-8859-1      收录除ASCII外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号
​
UTF-8           针对Unicode码表的可变长度字符编码
​
GB2312          简体中文
​
GBK             简体中文、扩充
​
BIG5            台湾、繁体中文
//当编码方式和解码方式不一致时,就会出现乱码
//一般使用UTF-8

字符流

// 传统字节流读取
psvm(String[] args){
  // 1. 创建FileInputStream对象
  FileInputSteam fis = new FileInputStream("路径");
  // 2. 读取
  int data = 0;
  while((data = fis.read()) != -1){
    sout((char)data); 
  }
  // 3. 关闭
  fis.close();
}
// 使用字节流读取一个含有中文的txt文件,会乱码,因为一个中文是由3个字节组成的,而字节流是逐个字节读取
 //    此时就要用到字符流

字符流的父类(抽象类)

reader 字符输入流

  • public int read(){}

  • public int read(char[] c){}

  • public int read(char[] b, int off, int len){}

Writer 字符输出流

  • public void write(int n){}

  • public void write(String str){}

  • public void write(char[] c){}

// 1. 创建FileReader 文件字符输入流
FileReader fr = new FileReader("..");
// 2. 读取
// 2.1 单个字符读取
int data = 0;
while((data = fr.read()) != -1){
  sout((char)data);// 读取一个字符
}
char[] buf = new char[2];// 字符缓冲区读取
int count = 0;
while((count = fr.read(buf) != -1)){
  sout(new String(buf, 0, count));
}
// 3. 关闭
fr.close();
// 1. 创建FileWriter对象
FileWriter fw = new FileWriter("..");
// 2. 写入
for(int i = 0; i < 10; i ++){
  fw.write("写入的内容");
  fw.flush();
}
// 3. 关闭
fw.close();
sout("执行完毕");

(案例)使用上述内容进行文本文件复制

不能复制图片或二进制文件,使用字节流可以复制任意文件

字符流是有编码的,字节流没有编码,所以不能复制图片或二进制文件
psvm(String[] args) throws Exception{
  // 1. 创建
  FileReader fr = new FileReader("...");
  FileWriter fw = new FileWriter("...");
  // 2. 读写
  int data = 0;
  while((data = fr.read()) != -1){
    fw.write(data);
    fw.flush();
  }
  // 3. 关闭
  fw.close();
  fr.close();
}

字符缓冲流

//     BufferedReader / BufferedWriter
1.高效读写
​
2.支持输入换行符
​
3.可一次写一行读一行
psvm(String[] args) throws Exception{
  // 创建缓冲流
  FileReader fr = new FileReader("..");
    BufferedReader br = new BufferedReader(fr);
  // 读取
  // 1. 第一种方式
  char[] buf = new char[1024];
  int count = 0;
  while((count = br.read(buf)) != -1){
    sout(new String(buf, 0, count));
  }
  // 2. 第二种方式 一行一行读取
  String line = null;
  while((line = br.readLine()) != null){
    sout(line);
  }
  
    // 关闭
  br.close();
}
psvm(String[] args){
  // 1. 创建BufferedWriter对象
  FileWriter fw = new FileWriter("..");
  BufferedWriter bw = new BufferedWriter(fw);
  // 2. 写入
  for(int i = 0; i < 10; i ++){
    bw.write("写入的内容");
    vw.newLine(); // 写入一个换行符
    bw.flush();
  }
  // 3. 关闭
  bw.close(); // 此时会自动关闭fw
}

PrintWriter

封装了print() / println() 方法 支持写入后换行

支持数据原样打印
psvm(String[] args){
  // 1 创建打印流
  PrintWriter pw = new PrintWriter("..");
  // 2 打印
  pw.println(12);
  pw.println(true);
  pw.println(3.14);
  pw.println('a');
  // 3 关闭
  pw.close();
}

转换流

桥转换流 InputStreamReader / OutputStreamWriter

可将字节流转换为字符流

可设置字符的编码方式

psvm(String[] args) throws Exception{
  //1.创建字节流对象
  FileInputStream fis = new FisInputStream("..");
  //2.创建InputStreamReader对象
  InputStreamReader isr = new InputStreamReader(fis, "utf-8");
  //3. 读取文件
  int data = 0;
  while((data = isr.read()) != -1){
    sout((char)data);
  }
  // 4.关闭
  isr.close();
}
psvm(String[] args) throws Exception{
  // 1 创建OutputStreamReader对象
  FileOutputStream fos = new FisOutputStream("..");
  OutputStreamWRITER osw = new OutputStreamReader(fos, "utf-8");
  // 2 写入
  for(int i = 0; i < 10; i ++){
    osw.write("写入内容");
    osw.flush();
  }
  // 3 关闭
  osw.close();
}

File类

概念:代表物理盘符中的一个文件或者文件夹

/*
File类的使用
1. 分隔符
2. 文件操作
3. 文件夹操作
*/
public class Demo{
  psvm(String[] args){
    separator();
  }
  // 1. 分隔符
  public static void separator(){
    sout("路径分隔符" + File.pathSeparator);
    sout("名称分隔符" + File.separator);
  }
  // 2. 文件操作
  public static void fileOpen(){
    // 1. 创建文件
    if(!file.exists()){ // 是否存在
        File file = new File("...");
        boolean b = file.creatNewFile();
    }
    
    // 2. 删除文件
    // 2.1 直接删除
    file.delete(); // 成功true
    // 2.2 使用jvm退出时删除
    file.deleteOnExit();
    
    // 3. 获取文件信息
    sout("获取绝对路径" + file.getAbsolutePaht());
    sout("获取路径" + file.getPath());
    sout("获取文件名称" + file.getName());
    sout("获取夫目录" + file.getParent());
    sout("获取文件长度" + file.length());
    sout("文件创建时间" + new Date(file.lashModified()).toLocalString());
    
    // 4. 判断
    sout("是否可写" + file.canWrite());
    sout("是否是文件" + file.isFile());
    sout("是否隐藏" + file.isHidden());
  }
  
  
  // 文件夹操作
  public static void directoryOpe() throws Exception{
    // 1. 创建文件夹
    File dir = new File("...");
    sout(dir.toString());
    if(!dir.exists()){
      //dir.mkdir(); // 只能创建单级目录
      dir.mkdirs(); // 创建多级目录
    }
    
    // 2. 删除文件夹
    // 2.1 直接删除
    dir.delete(); // 只能删除最底层空目录
    // 2.2 使用jvm删除
    dir.deleteOnExit();
    
    // 3. 获取文件夹信息
        sout("获取绝对路径" + dir.getAbsolutePaht());
    sout("获取路径" + dir.getPath());
    sout("获取文件名称" + dir.getName());
    sout("获取夫目录" + dir.getParent());
    sout("获取文件长度" + dir.length());
    sout("文件夹创建时间" + new Date(dir.lashModified()).toLocalString());
    
    // 4. 判断
    sout("是否可写:" + file.canWrite());
    sout("是否是文件夹" + dir.isFile());
    sout("是否隐藏" + dir.isHidden());
    
    // 5. 遍历文件夹
    File dir2 = new File("...");
    String[] files = dir2.list();
    for(String string : files){
      sout(string);
    }
    
    // FileFilter接口的使用
    
    File[] files2 = dir2.listFiles(new FileFilter()){
      
      @Override
      public boolean accept(File pathname){
        if(pathname.getName().endsWith(".jpg")){
          return true;
        }
        return false;
      }
    });
    for(File file : files2){
      sout(file.getName());
    }
    
  }
}

递归遍历文件夹

psvm(String[] args){
  listDir(new File("d:\\myfiles"));
}
public static void listDir(File dir){
  File[] files = dir.listFiles();
  sout(dir.getAbsolutePath());
  if(files != null && files.length > 0){
    for(File file : files){
      if(file.isDirectory()){
        listDir(file); // 递归
      }else {
        sout(file.getAbsolutePath());
      }
    }
  }
}

递归删除文件夹

public static void deleteDir(File dir){
  File[] files = dir.listFiles();
  if(files != null && files.length > 0){
    for(File file : files){
      if(file.idDirectory()){
        deleteDir(file); // 递归
      }else{
        // 删除文件
        sout(file.getAbsolutePath() + "删除" + file.delete());
      }
    }
  }
}

Properties:属性集合

特点:
​
1.存储属性名和属性值
​
2.属性名和属性值都是字符串类型
​
3.没有泛型
​
4.和流有关
package test.IO;
​
import java.io.*;
import java.util.Properties;
import java.util.Set;
​
/*
  Properties集合的使用
 */
public class t18 {
    public static void main(String[] args) throws IOException {
        //1.创建集合
        Properties properties=new Properties();
        //2.添加数据
        properties.setProperty("username","zhangshan");
        properties.setProperty("age","20");
​
        System.out.println(properties.toString());
        //3.遍历
        /*
            1.keyset
            2.entryset
            3.StringPropertyNames()
         */
        Set<String> pronames = properties.stringPropertyNames();
        for(String pro:pronames){
            System.out.println(pro+"======="+properties.getProperty(pro));
        }
        //4.和流有关的方法
        // 1 -----list方法------
        PrintWriter pw=new PrintWriter("d:\\print.txt");
        properties.list(pw);
        pw.close();
​
        // 2 -----store方法,保存---------
        FileOutputStream fos=new FileOutputStream("d:\\store.properties");
​
        properties.store(fos,"注释");
        fos.close();
​
        // 3 -----load方法,加载----------
        Properties properties2=new Properties();
        FileInputStream fis=new FileInputStream("d:\\store.properties");
        properties2.load(fis);
        fis.close();
        System.out.println(properties2.toString());
    }
}
​

BIO,NIO,AIO 有什么区别?

  • 简答

    • BIO:Block IO 同步阻塞式 IO,就是我们平常使用的传统 IO,它的特点是模式简单使用方便,并发处理能力低。
    • NIO:Non IO 同步非阻塞 IO,是传统 IO 的升级,客户端和服务器端通过 Channel(通道)通讯,实现了多路复用。
    • AIO:Asynchronous IO 是 NIO 的升级,也叫 NIO2,实现了异步非堵塞 IO ,异步 IO 的操作基于事件和回调机制。
  • 详细回答

    • BIO (Blocking I/O): 同步阻塞I/O模式,数据的读取写入必须阻塞在一个线程内等待其完成。在活动连接数不是特别高(小于单机1000)的情况下,这种模型是比较不错的,可以让每一个连接专注于自己的 I/O 并且编程模型简单,也不用过多考虑系统的过载、限流等问题。线程池本身就是一个天然的漏斗,可以缓冲一些系统处理不了的连接或请求。但是,当面对十万甚至百万级连接的时候,传统的 BIO 模型是无能为力的。因此,我们需要一种更高效的 I/O 处理模型来应对更高的并发量。
    • NIO (New I/O): NIO是一种同步非阻塞的I/O模型,在Java 1.4 中引入了NIO框架,对应 java.nio 包,提供了 Channel , Selector,Buffer等抽象。NIO中的N可以理解为Non-blocking,不单纯是New。它支持面向缓冲的,基于通道的I/O操作方法。 NIO提供了与传统BIO模型中的 SocketServerSocket 相对应的 SocketChannelServerSocketChannel 两种不同的套接字通道实现,两种通道都支持阻塞和非阻塞两种模式。阻塞模式使用就像传统中的支持一样,比较简单,但是性能和可靠性都不好;非阻塞模式正好与之相反。对于低负载、低并发的应用程序,可以使用同步阻塞I/O来提升开发速率和更好的维护性;对于高负载、高并发的(网络)应用,应使用 NIO 的非阻塞模式来开发
    • AIO (Asynchronous I/O): AIO 也就是 NIO 2。在 Java 7 中引入了 NIO 的改进版 NIO 2,它是异步非阻塞的IO模型。异步 IO 是基于事件和回调机制实现的,也就是应用操作之后会直接返回,不会堵塞在那里,当后台处理完成,操作系统会通知相应的线程进行后续的操作。AIO 是异步IO的缩写,虽然 NIO 在网络操作中,提供了非阻塞的方法,但是 NIO 的 IO 行为还是同步的。对于 NIO 来说,我们的业务线程是在 IO 操作准备好时,得到通知,接着就由这个线程自行进行 IO 操作,IO操作本身是同步的。查阅网上相关资料,我发现就目前来说 AIO 的应用还不是很广泛,Netty 之前也尝试使用过 AIO,不过又放弃了。
全部评论

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务