流的概念
电流 水流 人流
流: 字节的序列
- 流的最小单位 是字节
- 序列的不同组成的内容也不同
学习4个抽象类
InputStream OutputStream 字节流
InputStream和OutputStrean在使用上基本上对应的
FileinputStream -----FileOutputStream
Reader Writer ---字符流
流的体系图:
继承体系
InputStream 就是一个抽象类
不能直接创建对象 ----通过创建子类对象来进行实例化
ByteArrayInputStream 字节数组
FileInputStream 文件
ObjectInputSream 对象
DataInputStream 二进制数据
字符字节,输入输出流的概念
字符流和字节流
字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。 字节流和字符流的区别:
- 读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
- 处理对象不同:字节流能处理所有类型的数据(如字符、图片、avi音视频等),而字符流只能处理字符类型的数据。
结论:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。
输入流和输出流
对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。
流的使用
一.字节流
1.输入字节流InputStreamIO --->FileInputStream
2:输出字节流OutputStream --->FileOutputStream
public class IOTest1 {
public static void main(String[] args) throws IOException {
File target = new File("mydir1/mydir2/a.jpg");
if(target.exists()){
System.out.println("该文件已存在");
}else{
target.createNewFile();
}
FileInputStream is =
new FileInputStream("D:\\2.jpg");
FileOutputStream os = new FileOutputStream(target);
int i = 0;
while(i!=-1){
i = is.read();
os.write(i);
}
is.close();
os.close();
}
}
public class IOTest1 {
public static void main(String[] args) throws IOException {
File target = new File("mydir1/mydir2/a.txt");
if(target.exists()){
System.out.println("该文件已存在");
}else{
target.createNewFile();
}
FileInputStream is =
new FileInputStream("D:\\2.txt");
FileOutputStream os = new FileOutputStream(target);
byte [] bRet = new byte[1024];
int i = 0;
while((i = is.read(bRet))!=-1){
System.out.println(i);
os.write(bRet,0,i);
}
is.close();
os.close();
}
}
二:字符流
1:字符输出流Writer --->FileWriter
File srcFile = new File("io"+File.separator+"a.txt");
//2、创建字符输出流对象
Writer out = new FileWriter(srcFile);
//3、具体的 IO 操作
/***
* void write(int c):向外写出一个字符
* void write(char[] buffer):向外写出多个字符 buffer
* void write(char[] buffer,int off,int len):把 buffer 数组中从索引 off 开始到 len个长度的数据写出去
* void write(String str):向外写出一个字符串
*/
//void write(int c):向外写出一个字符
out.write(65);
//void write(char[] buffer):向外写出多个字符 buffer
out.write("王二麻".toCharArray());//将王二麻 写入 a.txt 文件中
//void write(char[] buffer,int off,int len)
out.write("王二麻".toCharArray(),0,2);//将王二麻 写入a.txt文件中
//void write(String str):向外写出一个字符串
out.write("王二麻");//将 王二麻 写入 a.txt 文件中
//4、关闭流资源
/***
* 注意如果这里有一个 缓冲的概念,如果写入文件的数据没有达到缓冲的数组长度,那么数据是不会写入到文件中的
* 解决办法:手动刷新缓冲区 flush()
* 或者直接调用 close() 方法,这个方法会默认刷新缓冲区
*/
out.flush();
out.close();
2:字符输入流Reader --->FileReader
File srcFile = new File("io"+File.separator+"a.txt");
//2、创建字符输出流对象
Reader in = new FileReader(srcFile);
//3、具体的 IO 操作
/***
* int read():每次读取一个字符,读到最后返回 -1
* int read(char[] buffer):将字符读进字符数组,返回结果为读取的字符数
* int read(char[] buffer,int off,int len):将读取的字符存储进字符数组 buffer,返回结果为读取的字符数,从索引 off 开始,长度为 len
*
*/
//int read():每次读取一个字符,读到最后返回 -1
int len = -1;//定义当前读取字符的数量
while((len = in.read())!=-1){
//打印 a.txt 文件中所有内容
System.out.print((char)len);
}
//int read(char[] buffer):将字符读进字符数组
char[] buffer = new char[10]; //每次读取 10 个字符
while((len=in.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
//int read(char[] buffer,int off,int len)
while((len=in.read(buffer,0,10))!=-1){
System.out.println(new String(buffer,0,len));
}
//4、关闭流资源
in.close();
三:字节流字符流之间的转换
1: InputStreamReader是字节流同向字符流的桥梁,如果不指定字符编码,该解码过程将使用系统默认的字符编码: 如GBK:
2:OutputStream用于将写入的字符编码成字节后写入一个字节流。
public class InputSteamReaderTest {
public static void main(String[] args) throws IOException {
//读取的目标
String path="D:\\2.txt";
//写入目标路径
File target = new File("mydir1/mydir2/a.txt");
// FileInputStream fis = new FileInputStream(path);
//
// InputStreamReader isr = new InputStreamReader(fis);
//
// BufferedReader br = new BufferedReader(isr);
BufferedReader br =
new BufferedReader(
new InputStreamReader(
new FileInputStream(path)));
BufferedWriter bw =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(target)));
String result = "";
StringBuffer sb = new StringBuffer();
while((result = br.readLine())!=null){
// 写的操作
bw.write(result);
bw.newLine();
sb.append(result+"\n");
}
System.out.println(sb.toString());
bw.flush();
br.close();
bw.close();
}
}
四:缓冲流
缓冲流也称为处理流,对文件或者其他目标频繁的操作,效率低,性能差。缓冲流目的是提高程序读取和写出的性能。缓冲流也分为字节缓冲流和字符缓冲流。
使用缓冲流的好处是能够更高效的读写信息,原理是先将数据缓冲起来,然后一起写入或者读取出来。
1:缓冲字节输入流BufferedInputStream
2:缓存字节输出流BufferOutputStream
3:缓冲字符输入流 BufferedReader
4:缓冲字符输出流 BufferWriter