Java IO 是一套Java用来读写数据(输入和输出)的API。
Java中io系统可以分为Bio,Nio,Aio三种io模型
Bio,同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销
Nio,同步非阻塞,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理。
Aio,异步非阻塞,服务器实现模式为一个有效请求一个线程,客户端的I/O请求都是由OS先完成了再通知服务器应用去启动线程进行处理
Java I/O主要包括如下几个层次,包含三个部分:
1.流式部分――IO的主体部分。
字节流和字符流
2.非流式部分――主要包含一些辅助流式部分的类,如:File类、RandomAccessFile类和FileDescriptor等类;
3.其他类--文件读取部分的与安全相关的类,如:SerializablePermission类,以及与本地操作系统相关的文件系统的类,如:FileSystem类和Win32FileSystem类和WinNTFileSystem类。
IO流的分类
字符流和字节流
流序列中的数据既可以是未经加工的原始二进制数据,也可以是经一定编码处理后符合某种格式规定的特定数据。因此Java中的流分为两种:
1) 字节流:数据流中最小的数据单元是字节
2) 字符流:数据流中最小的数据单元是字符, Java中的字符是Unicode编码,一个字符占用两个字节。
字节流和字符流的区别:
字节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。因此在输出 时,字节流不调用colse()方法时,
信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,信息才输出。要想字符流在未关闭时输出信息,
则需要手动调用flush()方法。
1·读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
2·处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
结论:只要是处理纯文本数据,就优先考虑使用字符流。除此之外都使用字节流。
按照类型和输入输出方向可分为:
输入字节流:InputStream
输出字节流:OutputStream
输入字符流:Reader
输出字符流:Writer
字符流与字节流转换
转换流的特点:
1. 其是字符流和字节流之间的桥梁
2. 可对读取到的字节数据经过指定编码转换成字符
3. 可对读取到的字符数据经过指定编码转换成字节
何时使用转换流?
1. 当字节和字符之间有转换动作时;
2. 流操作的数据需要编码或解码时。
字节流主要用来处理字节或二进制对象,字符流用来处理字符文本或字符串
使用InputStreamReader可以将输入字节流转化为输入字符流
Reader reader = new InputStreamReader(inputStream);
使用OutputStreamWriter可以将输出字节流转化为输出字符流
Writer writer = new OutputStreamWriter(outputStream)
字节流:
输入字节流:InputStream
步骤:
第一步:1:打开流(即创建流)
第二步:2:通过流读取内容
第三步:3:用完后,关闭流资源
案例1.使用 read()方法,一次读取一个字节,读到文件末尾返回-1.
private static void showContent(String path) throws IOException {
// 打开流
FileInputStream fis = new FileInputStream(path);
int len;
while ((len = fis.read()) != -1) {
System.out.print((char) len);
}
// 使用完关闭流
fis.close();
案例2.使用缓冲(提高效率),并循环读取(读完所有内容).
private static void showContent7(String path) throws IOException {
FileInputStream fis = new FileInputStream(path);
byte[] byt = new byte[1024];
int len = 0;
while ((len = fis.read(byt)) != -1) {
System.out.println(new String(byt, 0, len));
}
fis.close();
}
InputStream中的三个基本的读方法
abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。
intread(byte[]?b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。
intread(byte[]?b, int?off, int?len) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
流结束的判断:方法read()的返回值为-1时;readLine()的返回值为null时。
输出字节流:OutputStream
private static void writeTxtFile(String path) throws IOException {
// 1:打开文件输出流,流的目的地是指定的文件
FileOutputStream fos = new FileOutputStream(path,true);
// 2:通过流向文件写数据
byte[] byt = "java".getBytes();
fos.write(byt);
// 3:用完流后关闭流
fos.close();
}
outputStream中的三个基本的写方法
abstract void write(int?b):往输出流中写入一个字节。
void write(byte[]?b) :往输出流中写入数组b中的所有字节。
void write(byte[]?b, int?off, int?len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
输入输出流综合使用——文件拷贝实现
public static void copyFile(String srcPath, String destPath) throws IOException {
// 打开输入流,输出流
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
// 读取和写入信息
int len = 0;
// 使用字节数组,当做缓冲区
byte[] byt = new byte[1024];
while ((len = fis.read(byt)) != -1) {
fos.write(byt, 0, len);
}
// 关闭流
fis.close();
fos.close();
}
字符流:
输入字符流:Reader
public static void main(String[] args) throws Exception{
File file = new File("D:/a.txt");
Reader reader = new FileReader(file);
char[] bytes = new char[(int) file.length()];
reader.read(bytes);
System.out.println(new String(bytes));
reader.close();
}
Reader主要方法:
(1) public int read() throws IOException; //读取一个字符,返回值为读取的字符
(2) public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/
(3) public abstract int read(char cbuf[],int off,int len) throws IOException;
输出字符流:Writer
public static void main(String[] args) throws Exception{
String var = "hai this is a test";
File file = new File("D:/b.txt");
Writer writer = new FileWriter(file);
writer.write(var);
writer.close();
}
主要方法:
(1) public void write(int c) throws IOException; //将整型值c的低16位写入输出流
(2) public void write(char cbuf[]) throws IOException; //将字符数组cbuf[]写入输出流
(3) public abstract void write(char cbuf[],int off,int len) throws IOException; //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流
(4) public void write(String str) throws IOException; //将字符串str中的字符写入输出流
(5) public void write(String str,int off,int len) throws IOException; //将字符串str 中从索引off开始处的len个字符写入输出流
BufferedInputStream(字节读)
在使用InputStream的时候,都是一个字节一个字节的读或写,而BufferedInputStream为输入字节流提供了缓冲区,读数据的时候会一次读取一块数据放到缓冲区里,当缓冲区里的数据被读完之后,输入流会再次填充数据缓冲区,直到输入流被读完,有了缓冲区就能够提高很多io速度
使用方式将输入流包装到BufferedInputStream中
BufferedInputStream bufferedInputStream = newBufferedInputStream(inputStream,1024);
BufferedOutputStream(字节写)
BufferedOutputStream可以为输出字节流 提供缓冲区,作用与BufferedInputStream类似
使用方式将输出流包装到BufferedOutputStream中
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream,1024);
BufferedReader
为输入字符流提供缓冲区,使用方式如下
BufferedReader bufferedReader = new BufferedReader(reader,1024);
BufferedWriter
为输出字符流提供缓冲区,使用方式如下
BufferedWriter bufferedWriter = new BufferedWriter(writer,1024);