一:IO流的分类
- 按流向:
- 输入流
- 输出流
- 按传输数据:
- 字节流:以二进制形式传输数据,可以处理一切数据,包括文本内容,图片,音频以及视频,但是在处理Unicode字符时,可能会出现乱码,需手动转码
- 字符流:以字符形式传输数据,只能处理纯文本内容,可以自动转码
二:字节输入流
读取数据的步骤
1、创建数据源对象
2、建立管道(输入流)连接到数据源
3、通过管道(输入流)读取数据
4、关闭管道(输入流)
FileInputStream使用到的一些API
close() 关闭流
int read() 读取一个字节的数据
int read(byte[] b) 读取该数组长度的数据放入byte数组的缓冲区b中,并返回实际读取到的数据长度,如果数据读
完了,则返回-1
public static void test() {
//1、创建数据源对象
File src = new File("D:\\File\\a.txt");
//2、创建输入流管道
InputStream input = null;
try {
input = new FileInputStream(src);
//3、读取数据
//定义字节数组存放读取的数据
byte[] buff = new byte[20];
//定义变量来接收读取都的数据的长度
int readLen = 0;
//int read(byte[] b):读取该数组长度的数据放入byte数组的缓冲区b中,并返回实际读
//取到的数据长度,如果数据读完了,则返回-1.
while((readLen = input.read(buff))!=-1) {
//转换数据,以gbk读取防止乱码
String data = new String(buff, 0, readLen,"gbk");
System.out.println("读取的数据:"+data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//4、关闭管道
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三:字节输出流
输出数据的步骤
1、创建数据源(目的地)对象
2、建立管道(输出流)连接到目的地
3、得到数据
4、通过管道(输出流)写出数据到目的地
5、刷新管道(输出流)(把缓冲区的数据写出到目的地)
6、关闭管道(输出流)
FileOutputStream使用到的一些API
close() 关闭流
write(int b) 写出一个字节数据
write(byte[] b) 写出字节数组的所有的数据
write(byte[] b,int off,int len) 写出字节数组从下标off开始长度为len的数据到目的地
private static void test() {
//1、创建目的地的File对象
File aim = new File("D:/File/xue.txt");
//2、创建输出流管道连接到目的地
OutputStream output = null;
try {
output = new FileOutputStream(aim);
//3、获取 写出的数据
String data = "薛之谦_男_35岁_段子手_心愿是:世界和平";
//将字符串转换为字节数组
byte[] writeData = data.getBytes("gbk");
//4、写出数据
output.write(writeData);
//5、手动刷新管道
output.flush();
System.out.println("数据写出完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭输出流管道
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四:复制:
输入流读取文件数据再通过输出流写出数据到目的地
public static void copy() {
//1、创建数据源和目的地的File对象
File src = new File("src/com/cdsxt/day17/OutputStream/FIle/b.txt");//相对路
//径:相对于项目的根目录
File aim = new File("src/com/cdsxt/day17/OutputStream/FIle/copy_b.txt");
//2、创建输入流和输出流管道
InputStream input = null;
OutputStream output = null;
try {
//输入流对象
input = new FileInputStream(src);
//输出流对象
output = new FileOutputStream(aim);
//3、读取数据
byte[] buff = new byte[20];
int readLen = 0;//实际读取的数据长度
//循环读取数据
while((readLen = input.read(buff))!=-1) {
//4、直接将读取的数据写出到目的地
output.write(buff, 0, readLen);
}
//5、刷新管道
output.flush();
System.out.println("文件复制完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭管道:先打开的后关闭,后打开的先关闭。
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
五:字符输入流Reader
以字符为单位进行操作,专门用于文本的处理。自动进行编码解码,且使用Java文件当前默认的字符集
字符输入流常用的一些API:
close(); 关闭流,释放资源
int read(); 读取单个字符
int read(char[] ch) 读取多个字符,将读取内容放入字符数组并返回实际读取到的数据长度,如果数据读完了,则返 回-1
private static void test() {
//1、创建数据源的File对象
File file = new File("D:\\File\\reader.txt");
//2、创建字符输入流管道
Reader reader = null;
try {
reader = new FileReader(file);
//3、读取数据:按照Java文件默认的字符集进行编码。
//定义字符数组来存放数据
char[] buff = new char[50];
int readLen = 0;//实际读取数据的长度
//循环读取数据
while((readLen = reader.read(buff))!=-1) {
//转换为字符串
String data = new String(buff, 0, readLen);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
六:字符输出流Writer
字符输出流常用的一些API:
close(); 关闭流,释放资源
flush(); 刷新管道,输出缓冲数据
write(int c); 写出单个字符
write(String str); 写出字符串
write(char[] ch); 写出字符数组
private static void test() {
//1、创建目的地的File对象
File aim = new File("src/com/cdsxt/day17/Writer/writer.txt");//相对路径:相对于项目的根目录
//2、创建字符输出流管道
Writer writer = null;
try {
writer = new FileWriter(aim);
//3、获取写出的数据
String data = "周星驰_男_55岁_香港_演员_代表作:大话西游";
//4、写出数据
writer.write(data);//采用和Java文件默认的字符集
//5、刷新管道
writer.flush();
System.out.println("文件写出完成");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
七:缓冲流
Java中的缓冲流本身不具有IO流的读写功能,只是在别的流上加上缓冲功能以此提高效率
输入缓冲区:
通过缓冲流读取数据时,实际上并不是直接去数据源读取数据,而是从缓冲区中取获取
缓冲流会先将数据缓存起来,当缓存区存满之后才一次性读取数据 可以减少读取数据源的操作,提高效率
输出缓冲区:
通过缓冲流输出数据时,实际上并没有直接输出到目的地,而是先把数据存储到缓冲区
什么时候把数据从缓冲区输出到目的地?
自动输出:当缓冲区存满之后会自动输出到目的地。
手动输出:通过flush刷新,手动将缓冲区的数据刷出到目的地。关闭流时会自动调用flush。
字节缓冲流BufferedInputStream :带缓冲区的字节输入流
构造器:
BufferedInputStream(InputStream in) 创建BufferedInputStream对象,缓冲区默认大小为8192字节
BufferedInputStream(InputStream in,int size) 可以设置缓冲区大小size(单位字节)
public static void test2() {
//1、创建数据源和目的地的File对象
File src = new File("E:\\Tools\\Java\\eclipse_oxygen_64bit\\eclipse_64.zip");//相对路径:相对于项目的根目录
File aim = new File("E:\\Tools\\Java\\eclipse_oxygen_64bit\\copy_eclipse_64.zip");
//2、创建缓冲字节输入输出流对象
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
//创建缓冲字节输入流对象
//设置缓冲区大小为10kb,如果不设置则默认为8192字节
input = new BufferedInputStream(new FileInputStream(src),10*1024);
//创建缓冲字节输出流对象
output = new BufferedOutputStream(new FileOutputStream(aim), 10*1024);
//3、读取数据
//定义字节数组存放读取的数据
byte[] buff = new byte[1024];
int readLed = 0;
System.out.println("文件复制中。。。。");
//循环读取数据
while((readLed=input.read(buff))!=-1) {
//4、将读取到的数据写出
output.write(buff, 0, readLed);
}
//5、手动刷新管道:将缓冲的数据刷出到目的地
output.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(output!=null) {
output.close();
}
if(input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符缓冲输入流BufferedReader:带缓冲区的字符输入流
构造器同上
新增功能:readLine() 按行读取;可以一行一行的读取
字符缓冲输出流BufferedWriter 带缓冲区的字符输出流
构造器同上
新增功能:newLine() 换行
private static void test() {
//1、创建数据源和目的地对象
File src = new File("src/com/cdsxt/day17/BufferedStream/bufferedReader.txt");
File aim = new File("src/com/cdsxt/day17/BufferedStream/copy_bufferedReader.txt");
//2、创建缓冲字符输入输出流
BufferedReader reader = null;
BufferedWriter writer = null;
try {
//缓冲区默认大小8192字节
reader = new BufferedReader(new FileReader(src));
writer = new BufferedWriter(new FileWriter(aim));
//3、读取数据
//按行读取:定义字符串接收读取的数据
String data = null;
//循环读取数据:reader.readLine()读取一行数据,读取完后返回null
while((data = reader.readLine())!=null) {
//打印下数据
System.out.println(data);
//4、写出数据到目的地
writer.write(data);
//手动换行
writer.newLine();
}
//5、刷新管道
writer.flush();
System.out.println("文件复制完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer !=null) {
writer.close();
}
if(reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}