I/O流基础整理

I/O即数据的输入输出。点这里

而流式分为字节流和字符流
在这里插入图片描述

1 首先认识下 字节流
点这里
InputStream是输入字节流的超类

public abstract class InputStream implements Closeable {
 		public abstract int read() throws IOException;
	    public int read(byte b[]) throws IOException {
        	return read(b, 0, b.length);
        }
        public int read(byte b[], int off, int len) throws IOException {
	//实现细节请看api
		}
		public long skip(long n) throws IOException {}//忽略输入流中的n个字节
	    public int available() throws IOException {
      		  return 0;} 	//返回输入流中可以读取的字节数。
		public void close() throws IOException {}
	    public synchronized void mark(int readlimit) {}//在流中标记一个位置,要与markSupported()连用
	    public synchronized void reset() throws IOException {
        	throw new IOException("mark/reset not supported");
        }
	    public boolean markSupported() {
       		 return false;
        }		
    }

其子类

在这里插入图片描述

FileInputStream 把一个文件作为InputStream,实现对文件的读取操作
ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
StringBufferInputStream:把一个String对象作为InputStream
PipedInputStream:实现了pipe的概念,主要在线程中使用
SequenceInputStream:把多个InputStream合并为一个InputStream

OutputStream是输出字节流的超类
方法:

 public abstract void write(int b) throws IOException;
  public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }
     public void write(byte b[], int off, int len) throws IOException {}
      public void flush() throws IOException {
    }
     public void close() throws IOException {
    }

其子类

FileOutputStream
ByteArrayOutputStream
FilterOutputStream
BufferedOutputStream
DataOutputStream

FileInputStream/FileOutputStream
构造方法(文件路径,文件对象)
FileInputStream

 public FileInputStream(String name)
 public FileInputStream(File file)

FileOutputStream
构造函数

    FileOutputStream(String filePath)
	FileOutputStream(File fileObj) 
	FileOutputStream(String filePath,boolean append)

对比于FileInputStream 增加了FileOutputStream(String filePath,boolean append)构造函数,表示是覆盖方式(false),还是追加方式(true)

例:

	FileInputStream fin = new FileInputStream("hello.txt");
            byte[] buffer = new byte[1024];
            int x = fin.read(buffer,0,buffer.length);
            String str = new String(buffer);
            System.out.println(str);
            System.out.println(x);
            fin.close();

我们可以选用read中的内容
1 一个字节一个字节读取
2 一个固定字节数组的大小读取,如1024
3 跟文件内容大小一样的字节数组读取

ByteArrayInputStream/ByteArrayOutputStream
由于定义读取的字节数组的大小不确定性,因此我们选用动态字节数组流
由于ByteArrayInputStream 中定义了一个byte buf[];当我们的数组大小不够时会进行自动扩充。

ByteArrayOutputStream也会首先创建一个默认的容器量, capacity = 32 = 32b,
每次在写的时候都会去比对capacity是否还够用, 如果不够用的时候, 就重新创建buf的容量, 一直等到内容写完,这些数据都会一直处于内存中.

BufferedInputStream/BufferedOutputStream
缓存字节流

private static int DEFAULT_BUFFER_SIZE = 8192;
protected volatile byte buf[];
protected int pos;
protected int count;
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int size)
public synchronized int read()
public synchronized void mark(int readlimit)
public synchronized void reset()

BufferedOutputStream会首先创建一个默认的容器量, capacity = 8192 = 8KB,
每次在写的时候都会去比对capacity是否还够用, 如果不够用的时候, 就flushBuffer(),
把buf中的数据写入对应的outputStream中, 然后将buf清空, 一直这样等到把内容写完.
在这过程中主要起到了一个数据缓冲的功能.

DataInputStream/DataOutputStream
可以指定基本数据的形式读取
例如:readChar(),readInt()等

 public DataOutputStream(OutputStream out)
 public synchronized void write(byte b[], int off, int len)
 public final void writeBoolean(boolean v)
 public final void writeByte(int v)
 public final void writeShort(int v)
 public final void writeInt(int v) 
 public final void writeDouble(double v)

字符流
字节流只能按字节读取,而字符流可以对数据按字符读取/写入
Reader/Writer 是字符流的超类
在这里插入图片描述
在这里插入图片描述
Reader:

int read():从输入流中读取单个字符,返回所读取的字符数据
int read(char[]ch):从输入流中最多读取ch.length个字符的数据,并将其存储在字符数组ch中,返回实际读取的字符数。
int read(char[]ch,int off,int len):从输入流中最多读取len个字符的数据,并将其存储在字符数组ch中,放入数组时,从数组的off位置开始,返回实际读取的字符数。

FileReader/FileWriter
它们的功能与前面讲过的FileInputStream/FileOutputStream基本类似,只是前者是基于字符流后者是基于字节流,它们都能从文件中读取或者写入数据

public class FileReader extends InputStreamReader {
    public FileReader(String fileName) throws FileNotFoundException {
        super(new FileInputStream(fileName));
    }
    public FileReader(File file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }
    public FileReader(FileDescriptor fd) {
        super(new FileInputStream(fd));
    }

}

InputStreamReader /OutputStreamWriter

输入/输出体系中提供了两个转换流InputStreamReader和OutputStreamWriter,这两个转换流用于实现将字节流转换成字符流,其中InputStreamReader将字节输入流转换成字符输入流,OutputStreamWriter将字节输出流转换成字符输出流。

BufferedReader和BufferedWriter
BufferedReader和BufferedWriter类各拥有8192字符的缓冲区。当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。如果要在缓冲区中数据未满的情况下将数据写入到目的地,需要使用flush()方法,将数据刷新一下。
bufferedReader 还提供了readLine按行读取。

猜你喜欢

转载自blog.csdn.net/qq_31941773/article/details/83692005