流的概述:
什么是流?
流是一组有序的数据序列,将数据从一个地方带到另一个地方。
流的特点:
流是一维的,流是单向的,对应的操作就是单向读取和单向写入。
java IO 流可以概括为“两个对应,一个桥梁”:
两个对应指字节流和字符流的对应,输入流和输出流的对应;
一个桥梁是指字节流到字符流的桥梁;
分别由以下4个抽象类来表示流:
InputStream
OutputStream
Reader
Writer
java中的字节流根据流向的不同,可以分为输入流和输出流;
字节输入流的父类是 InputStream,它是一个抽象类;
字节输出流的父类是 OutputStream,它也是一个抽象类;
字节流可以处理所有文件类型的数据(图片,视频,文本…)
抽象类必然需要它的子类的实现;
如图所示:
下面内容是这四个抽象类的基本使用和介绍:
一.字节流-FileInputStream
FileInputStream:
该流用于从文件读取数据,它的对象可以用关键字new来创建。有多种构造方法可用来创建对象;
1、可以使用字符串类型的文件名来创建一个输入流对象来读取文件:
FileInputStream f=new FileInputStream("D:/SSS/xs.txt");
2.使用一个文件对象来创建一个输入流对象来读取文件:
File f=new File("D:/SSS/xs.txt");
FileInputStream a2= new FileInputStream(f);
FileInputStream字节输入流常用的方法:
方法名称 | 方法描述 |
---|---|
public int read() throws IOException{} | 从FileInputStream对象读取1个字节的数据,返回值即为读取的数据,如果返回-1则代表读取到文件的结尾; |
public int read(Byte b[]) throws IOException{} | 从FileInputStream对象读取b.length个字节的数据,并存入字节数组b中,,返回值为读取字节数量,如果返回-1 则代表已经读取到文件的结尾; |
public int read(Byte b[] , int off, int len) throws IOException{} | 从FileInputStream对象读取指定len个字节的数据,并从字节数组b的off位置存入字节数组b中,返回值为读取字节数量,如果返回-1 则代表已经读取到文件的结尾; |
public void close() throws IOException{} | 关闭此文件输入流并释放与此流有关的所有系统资源。抛出IOException异常; |
代码演示:
读取一个TXT文本中的所有内容
public static void main(String[] args) {
FileInputStream f =null;
//用字符串类型的文件名构建一个FileInputStream
try {
f=new FileInputStream("D:/sss/xs.txt");
byte[] b=new byte[1024]; //每次读取1024字节的数据
int n= -1;
String str="";
while ((n = f.read(b))> -1) {//括号优先级,先给n赋值再进行比较
str = new String(b,0,n); //字符串接收
System.out.println(str); //输出
}
}catch (FileNotFoundException e) {
System.err.println("找不到这个文件");
}catch (IOException e) {
System.err.println("读取文件出现问题");
}finally { //关闭流
try {
f.close();
} catch (IOException e) {
System.out.println("输出流关闭失败!");
}
}
}
二.字节流-FileOutputStream
FileOutputStream:
该类用来创建一文件并向文件中写数据。如果该流在打开文件进行输出前,目标文件不存在,那么该流会创建该文件;
有两个构造方法可以创建这个 FileOutputStream对象。
1、使用字符串类型的文件名来创建一个输出流对象:
FileOutputStream out = new FileOutputStream("D:/SSS/2.txt");
2.使用一个文件对象来创建一个输出流对象来写文件:
File file =new File("D:/SSS/2.txt");
FileOutputStream out = new FileOutputStream(file);
代码演示:
在本文文件中写入一句话:
public static void main(String[] args) {
FileOutputStream out=null;
try {
out = new FileOutputStream("D:/sss/xs1.txt");//当然还有.png图像 .zip压缩文件等等...只要是电脑二进制能识别的都可以写;
String name = "无限接近死亡,才能领悟生存的真谛";
out.write(name.getBytes());
} catch (FileNotFoundException e) {
System.out.println("文件找不到!");
}catch (IOException e) {
System.out.println("写入错误!!");
}finally {
try {
if(out!=null)
out.close();
} catch (IOException e) {
System.out.println("字节输出流关闭失败哦");
}
}
}
把一个文本的内容写入另一个文本中:
public static void main(String[] args) {
FileInputStream duInput=null;
FileOutputStream xieOutput=null;
try {
//输入和输出
duInput =new FileInputStream("D:/sss/xs.txt");
xieOutput =new FileOutputStream("D:/sss/xs2.txt");
byte[] b= new byte[1024];
int n= -1;
String str="";
while ((n = duInput.read(b)) > -1) {
xieOutput.write(b,0,n);
}
} catch (FileNotFoundException e) {
System.err.println("文件找不到");
}catch (IOException e) {
System.out.println("读取文件异常!");
}finally {
try {
if(duInput!=null)
duInput.close();
} catch (IOException e) {
System.err.println("流关闭失败");
}
try {
if(xieOutput!=null)
xieOutput.close();
} catch (IOException e) {
System.out.println("流关闭失败");
}
}
}
字符流-Reader与Writer
Reader是一个字符输入流,以char为单位读取
Writer是一个字符输出流,以char为单位读取
为了提高字符流读写的效率,引入了缓冲机制,进行字符批量的读写,提高了单个字符读写的效率。
BufferedReader用于加快读取字符的速度,BufferedWriter用于加快写入的速度。
BufferedReader和BufferedWriter类各拥有8192个字符的缓冲区。
当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并放满缓冲区,而之后若使用read0方法,会先从缓冲区 中进行读取。
如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。
如果缓冲区中的数据满了,才会一次对目的地进行写出。
字符流-BufferedReader
BudderedReader
是为了提供读的效率而设计的一个包装类,它可以包装字符流。
可以从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组、和行的高效读取。
构造方法:
BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
BufferedReader(Reader in,int sz) 创建一个使用指定大小输入缓冲区的缓冲字符输入流。
常用方法:
方法名称 | 方法描述 |
---|---|
int read() | 读取单个字符 |
int read(char[] cbuf , int off , int len) | 将字符读入数组的某一部分 |
String readLine() | 读取一个文本行 |
long skip(long n) | 跳过字符 |
boolean ready() | 判断此流是否已准备好被读取 |
void close() | 关闭该流并释放与之关联的所有资源 |
void mark(int readAheadLimit) | 标记流中当前的位置 |
void reset() | 将流重置到最新的标记 |
代码演示:
//读取文本文件中的所有内容并输出
public static void main(String[] args) {
BufferedReader br = null;
try {
FileReader fr =new FileReader("D:/sss/xs2.txt");
br = new BufferedReader(fr);
String str="";
while ((str = br.readLine()) !=null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
System.out.println("找不到文件!");
}catch(IOException e){
System.err.println("字符流读取出错!");
}finally {
try {
if(br != null)
br.close();
} catch (Exception e2) {
System.out.println("字符流关闭错误!");
}
}
}
字符输出流-BufferedWriter
构造方法:
BufferedWriter(Writer out)
创建一个字符缓冲输出流,使用默认大小的输出缓冲区;
BufferedWriter(Writer out , int sz)
创建一个字符缓冲输出流,使用给定大小的输出缓冲区;
BufferedWriter字符输出流常用的方法:
方法名称 | 方法描述 |
---|---|
void write(int c) | 写入单个字符 |
void write(char[] cbuf , int off , int len) | 写入字符数组的某一部分 |
void write(String s , int off , int len | 写入字符串的某一部分 |
void write(String s) | 写入字符串 |
void newLine() | 写入一个行分隔符 |
void close() | 关闭此流,但要先刷新它 |
void flush() | 刷新该流的缓冲 |
代码演示:
//在文本中写入两行话:
public static void main(String[] args) {
BufferedWriter bw = null;
try {
FileWriter fw =new FileWriter("D:/sss/xs3.txt");
bw =new BufferedWriter(fw);
bw.write("书山有路勤为径,学海无涯苦作舟");//写入
bw.newLine(); //换行
bw.write("学而时习之,不亦乐乎"); //写入
} catch (FileNotFoundException e) {
System.out.println("文件找不到");
}catch (IOException e) {
System.out.println("字符输出流异常!");
}finally {
try {
if(bw != null)
bw.close();
} catch (IOException e2) {
System.err.println("字符输出流关闭异常");
}
}
}
用字符缓冲类把一个文本的内容写入另一个文本中:
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
//读
FileReader fr =new FileReader("D:/sss/xs2.txt");
br = new BufferedReader(fr);
//写
FileWriter fw =new FileWriter("D:/sss/xs3.txt");
bw =new BufferedWriter(fw);
String str="";
while ((str = br.readLine()) !=null) {
bw.write(str);
bw.newLine(); //行分隔符
}
//刷新
bw.flush();
} catch (FileNotFoundException e) {
System.out.println("找不到文件!");
}catch(IOException e){
System.err.println("字符流读取出错!");
}finally {
try {
if(br != null)
br.close();
if(bw != null)
bw.close();
} catch (Exception e2) {
System.out.println("字符流关闭错误!");
}
}
}