JAVA IO分类理解与应用
字节流:
字符流:
字节流简介:字节流是由字节组成的,字符流是由字符组成的. Java里字符由两个字节组成.字节流是最基本的,所有的InputStream和OutputStream的子类都是,主要用在处理二进制数据
以stream结尾
字符流简介:按虚拟机的encode来处理,也就是要进行字符集的转化。在从字节流转化为字符流时,实际上就是byte[]转化为String时
以Reader或Writer结尾
【字节流】FileInputStream FileOutputStream :
可用字节数组缓存读取内容
FileInputStream 方法 说明
abstract int read() 从管道中读取1个字节的数据并返回
int read(byte b[]) 从管道中读取b.length长度的数据填充数组b中(下标从0开始的位置),返回实际读到的数据长度
long skip(long n) 跳过指定的长度
int available() 返回有效长度
void close() 关闭管道,释放和管道相联的资源
void mark(int readlimit) 标记
void reset() 重置
boolean markSupported() 返回是否支持标记(支持true、不支持false)
FileOutputStream方法 说明
Abstractvoid write(int b) 从管道中读取1个字节的数据并返回
void write(byte b[]) 从数组b中读出数据(下标从0开始的位置),返回实际读到的数据长度
void write(byte b[], int off, int len) 从数组b中将指定长度(len)的数据读出(下标从off开始的位置),返回实际读到的数据长度
void flush() 刷新缓冲,强迫输出
void close() 关闭管道,释放和管道相联的资源
//创建流对象 FileInputStream fis = new FileInputStream("abc.txt"); FileOutputStream fos = new FileOutputStream("copy.txt"); //创建字节数组读取多个 byte[] arr = new byte[1024]; int len;//储存返回长度 while((len = fis.read(arr)) != -1) { //判断是否读取完毕并把读取的数据赋值给b fos.write(arr);//写入 } fis.close(); fos.close();
【字节流】BufferedInputStream BufferedOutputStream :
缓冲字节流 代替字节数组 需要包裹 FileInputStream FileOutputStream 或InputStream OutputStream 子类
FileInputStream fis = new FileInputStream("abc.txt"); FileOutputStream fos = new FileOutputStream("copy.txt"); BufferedInputStream bis = new BufferedInputStream(fis); //创建缓冲区对象,对输入流进行包装让其变得更加强大 BufferedOutputStream bos = new BufferedOutputStream(fos); int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); }
【字符流】FileReader FileWriter :
可创建字符数组缓存 类似字节流
FileReader:
方法 说明
int read() 从管道中读取1个字符的数据并返回
int read(char b[]) 从管道中读取b…length长度的数据填充数组b中(下标从0开始的位置),返回实际读到的数据长度
long skip(long n) 跳过指定的长度
boolean markSupported() 返回是否支持标记(支持true、不支持false)
void mark(int readAheadLimit) 标记
void reset() 重置
public abstract void close() 关闭管道,释放和管道相联的资源
FileWriter
方法 说明
Abstractvoid write(int b) 从管道中读取1个字节的数据并返回
void write(byte b[]) 从数组b中读出数据(下标从0开始的位置),返回实际读到的数据长度
void write(byte b[], int off, int len) 从数组b中将指定长度(len)的数据读出(下标从off开始的位置),返回实际读到的数据长度
void flush() 刷新缓冲,强迫输出
void close() 关闭管道,释放和管道相联的资源
FileReader fr = new FileReader("abc.txt"); FileWriter fw = new FileWriter("abc1.txt"); //可创建数组缓存 类似字节流 char[] arr = new char[1024]; int len; while((len = fr.read(arr)) != -1) { //将文件上的数据读取到字符数组中 fw.write(arr,0,len); //将字符数组中的数据写到文件上 } //单个字符操作 int c; while((c = fr.read()) != -1) { fw.write(c); } fr.close(); fw.close(); //Writer类中有一个2k的小缓冲区,如果不关流,就会将内容写到缓冲区里,关流会将缓冲区内容刷新,再关闭 }
Writer类中有一个2k的小缓冲区,如果不关流,就会将内容写到缓冲区里,关流会将缓冲区内容刷新,再关闭
【字符流】BufferedReader BufferedWriter:
需要包裹 FileReader FileWriter
BufferedReader的read()方法读取字符时会一次读取若干字符到缓冲区, 然后逐个返回给程序, 降低读取文件的次数, 提高效率。
BufferedReader br = new BufferedReader(new FileReader("abc.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt")); int c; while((c = br.read()) != -1) { bw.write(c); } br.close(); bw.close();
readLine() 和newLine() 方法
String line; while((line = br.readLine()) != null) { //获取行数据 bw.write(line); bw.newLine(); //写出回车换行符 //bw.write("\r\n"); //只适用于windows平台 } br.close(); bw.close();
【序列流】
序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推。
在项目中新建名为a.txt,内容为今日我因华迪而自豪和名为b.txt,内容为明天华迪为我而骄傲的文件。用序列流的方式把他们写入到c.txt中。
public class Demo_SequenceInputStream { public static void main(String[] args) throws IOException { FileInputStream fis1 = new FileInputStream("a.txt"); FileInputStream fis2 = new FileInputStream("b.txt"); SequenceInputStream sis = new SequenceInputStream(fis1, fis2); FileOutputStream fos = new FileOutputStream("c.txt"); int b; while((b = sis.read()) != -1) { fos.write(b); } sis.close(); //sis在关闭的时候,会将构造方法中传入的流对象也都关闭 fos.close(); } }