io流

概念

  • 设备之间的数据传输

  • 对于程序来说,把数据传输到其他节点,成为输出,反之输入

  • 分类

    • 输出流
      • 字节,实际存储最小单位(8 bit)
      • 字符,utf编码,两个字节为单位(16 bit)
    • 输入流,输出流
      • 同上
  • 读文件步骤 (把大象拿出冰箱需要几步)

    • 打开文件,创建流对象,建立通道
    • 读取文件,通过管道处理数据
    • 关闭输入流,关闭管道
  • 写文件 (把大象关进冰箱)

    • 打开文件
    • 写入
    • 关闭流

基本流

基本流 字节流 字符流
输入流 InputStream(抽象基类) Reader(抽象基类)
输出流 OutputStrenam(抽象基类) Writer(抽象基类)

文件流

文件流 字节流 字符流
输入流 FileInputStream FileReader
输出流 FileOutputStrenam FileWriter
  • FileReader
    • 父类:InputStreamReader
    • 方法(父类)
      • void close() 关闭流
        • Closes the stream and releases any system resources associated with it.
      • String getEncoding() ???
        • Returns the name of the character encoding being used by this stream.
      • int read() 读取一个字节,并返回int
        • Reads a single character.
      • int read(char[] cbuf, int offset, int length) 读取到指定的字符串中,指定开始下表和长度
        • Reads characters into a portion of an array.
      • boolean ready() 是否可读
        • Tells whether this stream is ready to be read.
  • FilteWriter
    • 父类:OutputStreamWriter
    • 方法(父类)
      • void close() 关闭流
        • Closes the stream and releases any system resources associated with it.
      • String getEncoding() **
        • Returns the name of the character encoding being used by this stream.
      • int read() 读一个char转int返回
        • Reads a single character.
      • int read(char[] cbuf, int offset, int length)给指定的字符数组里面读一些字符串,从指定下标,读取指定长度,返回读取了多少个字符
        • Reads characters into a portion of an array.
      • boolean ready() 是否可读
        • Tells whether this stream is ready to be read.
public void copyjava() {
	FileWriter fileWriter = null;
	FileReader fileReader = null;
	
	try {
		fileReader = new FileReader("file");
		fileWriter = new FileWriter("file.bak");
		char [] buff = new char[100];
		int realCount = fileReader.read(buff);
		while (realCount != -1) {
			fileWriter.write(buff, 0, realCount);
			realCount = fileReader.read(buff);
		}
		
	} catch (Exception e) {
		System.out.println(e.getMessage());
	}finally {
		if(fileReader!= null) {
			try {
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(fileWriter != null){
			try {
				fileWriter.close();
			} catch (Exception e2) {
			}
		}
		
	}
}

缓冲流

处理流 字节流 字符流
输入流 BufferedInputStream BufferedReader
输出流 BufferedOutputStrenam BufferedWriter
  • 包装字符(字节流)流,包装类
  • 包装方式,关联
  • fileReader -> BufferedReader
  • fileWriter -> BufferedWriter
  • 可以多层嵌套

对象流

  • ObjectInputStream
  • ObjectutputStrenam
@Test
public void test5() {
	FileOutputStream fos = null;
	BufferedOutputStream bos = null;
	ObjectOutputStream oos = null; // 它是字节流中的辈份最高
	
	try {
		fos = new FileOutputStream("二进制文件");
		bos = new BufferedOutputStream(fos);
		oos = new ObjectOutputStream(bos);
		
		oos.writeInt(15);
		oos.writeBoolean(true);
		oos.writeBoolean(false);
		oos.writeLong(20);
		oos.writeDouble(3.14);
		
		oos.writeUTF("abc我和你xxx好吗"); // 把字符串以UTF8编码方式写入文件
		
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (oos != null) {
			try {
				oos.close();
			} catch (Exception e2) {
			}
		}
	}
}
原文地址:https://www.cnblogs.com/refengqingfu/p/10003871.html