IO流的概念
内存与存储设备之间传输数据的通道
IO体系
流的分类
按方向划分:
输入流:将存储设备中的内容读入到内存中
输出流:将内存中的内容写入到存储设备中
按单位划分:
字节流:以字节为单位,可以读写所有数据
字符流:以字符为单位,只能读写文本数据
按功能划分:
节点流:具有实际传输数据的读写功能
过滤流/处理流:在节点流的基础上增强功能
字节流
字节流的父类(抽象类)
字节输入流:InputStream
public int read()
public int read(byte[] b)
public int read(byte[] b,int off,int len)返回byte数组读取的个数
字节输出流:OutputStream
public void write(int n)
public void write(byte[] b)
public void write(byte[] b,int off,int len)
字节流实现类
FileInputStream()
FileOutputStream()
参数:文件或文件路径
字节流的read方法
public int read()throws IOException 返回单个字符的Ascll码值,如果没有输入可用,此方法将阻止,如果达到文件的末尾返回-1,如果发生异常抛出受检异常IOExecption
public int read(byte[] b)throws IOException 返回byte数组读取的个数,如果达到文件的末尾返回-1
public int read(byte[] b, int off, int len) throws IOException 参数 b - 读取数据的缓冲区 参数 off-目标数组b的起始偏移量 参数 len-读取的最大字节数
字节流的代码实现
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamTest {
public static void main(String[] args) throws IOException {
InputStream is1 = new FileInputStream("D:\\Test.txt");
InputStream is2 = new FileInputStream("D:\\Test.txt");
//1.单个字节流读取
int values1 = 0;
while((values1 = is1.read())!=-1){//读到结尾返回-1作为循环结束条件
System.out.print((char)values1);
}
System.out.println();
//2.字节数组读取
byte[] b = new byte[6];
int values2 = 0;
while((values2 = is2.read(b))!=-1){
String str = new String(b,0,values2);
System.out.print(str);
}
//3.关闭流
is2.close();
is1.close();
}
}
实现文件拷贝
import java.io.*;
public class IOTest {
public static void main(String[] args) throws IOException {
//输入流
InputStream is = new FileInputStream("D:\\Test.txt");
//输出流
OutputStream os = new FileOutputStream("D:\\CopyTest.txt");
byte[] barr = new byte[6];
int values = 0;
while((values = is.read(barr))!=-1){
os.write(barr,0,values);
}
//关闭流
os.close();
is.close();
}
}
缓冲字节流
bufferedInputStream
bufferedOutputStream
特点:提高IO效率,减少访问磁盘的次数,数据存储在缓冲区,flush是将存储区的内容写入文件中,也可以直接close
缓冲字节流代码实现
import java.io.*;
public class BuffedIOputStreamTest {
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("D:\\Test.txt");
OutputStream os = new FileOutputStream("D:\\CopyTest.txt");
BufferedInputStream buffis = new BufferedInputStream(is);
BufferedOutputStream buffos = new BufferedOutputStream(os);
//1.缓冲流
for(int value = 0;(value = buffis.read())!=-1;){
buffos.write(value);
}
//2.数组缓冲流
byte[] b = new byte[1024];
for(int value = 0;(value = buffis.read(b))!=-1;){
buffos.write(b,0,value);
}
buffos.close();
buffis.close();
}
}
对象序列化
使用流传输对象到内存的过程叫序列化
从内存读取对象的过程叫反序列化
对象流
ObjectOutputStream
ObjectInputStream
增强了读写8种基本数据类型和字符串功能
增强了读写对象的功能
readObject()
writeObject()
对象序列化必须实现Serializable接口
必须保证其所有属性均可序列化
transient修饰为临时属性,不参与序列化
读取到文件尾部的标志:java.io.EOFException
序列化代码实现
import java.io.Serializable;
public class Student implements Serializable {
private final static long serialVersionUID =1223L;
private String name;
private int age;
private String father;
private String mother;
public Student(String name, int age, String father, String mother) {
this.name = name;
this.age = age;
this.father = father;
this.mother = mother;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", father='" + father + '\'' +
", mother='" + mother + '\'' +
'}';
}
}
import java.io.*;
public class ObjectStreamTest {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("d:\\ObjectTest.txt");
ObjectOutputStream objos = new ObjectOutputStream(os);
objos.writeObject(new Student("小明",18,"小白","小丽"));
objos.writeBoolean(false);
objos.writeUTF("55555");
objos.close();
}
}
class Object1Test{
public static void main(String[] args) throws IOException, ClassNotFoundException {
InputStream is = new FileInputStream("d:\\ObjectTest.txt");
ObjectInputStream objis = new ObjectInputStream(is);
Object obj = objis.readObject();
if(obj instanceof Student){
Student s = (Student)obj;
System.out.println(s);
}
boolean flag = objis.readBoolean();
System.out.println(flag);
String str = objis.readUTF();
System.out.println(str);
objis.close();
}
}
字符流
字符流的父类(抽象类)
字符输入流Reader
public int read()
public int read(char[] c)
public int read(char[] c,int off,int len)
字符输出流Writer
public void write()
public void write(char[] c)
public void write(char[] c,int off,int len)
字符流实现类
FileReader()
FileWriter()
字符流代码实现
import java.io.*;
public class ReaderStream {
public static void main(String[] args) throws IOException {
Reader r = new FileReader("D:\\Test.txt");
Writer w = new FileWriter("D:\\CopyText.txt");
//1.单字符读取和写入
for(int value = 0;(value = r.read())!=-1;){
w.write(value);
}
//2.缓冲数组读取和写入
char[] tmpc = new char[36];
for(int values = 0; (values = r.read(tmpc)) != -1;){
w.write(tmpc,0,values);
}
w.close();
r.close();
}
}
缓冲字符流
BufferedReader
BufferedWriter
PrintWriter
特点:支持输入换行符,可一次写一行
缓冲字符流代码实现
import java.io.*;
public class BufferReaderWriter {
public static void main(String[] args) throws IOException {
Reader r = new FileReader("D:\\Test.txt");
Writer w = new FileWriter("D:\\CopyTest.txt");
//字符缓冲流
BufferedReader buffr = new BufferedReader(r);
BufferedWriter buffw = new BufferedWriter(w);
for(String str = null; (str = buffr.readLine()) != null;){
buffw.write(str);
//换行
buffw.newLine();
}
buffw.close();
buffr.close();
}
}
import java.io.*;
public class PrintWriterTest {
public static void main(String[] args) throws IOException {
Reader r = new FileReader("D:\\Test.txt");
Writer w = new FileWriter("D:\\CopyTest.txt");
BufferedReader buffr = new BufferedReader(r);
PrintWriter pw = new PrintWriter(w);
String str = null;
while((str = buffr.readLine())!=null){
pw.println(str);
}
pw.close();
buffr.close();
}
}
转换流
InputStreamReader
OutputStreamWriter
特点:可将字节流转为字符流,可以设置字符的编码方式
使用步骤:
创建节点流
创建处理流,设置字符编码集
封装过滤流
读写数据
关闭流
import java.io.*;
public class InputStreamWriter {
public static void main(String[] args) throws IOException {
FileInputStream fs = new FileInputStream("D:\\CopyTest.txt");
InputStreamReader isr = new InputStreamReader(fs,"utf-8");
BufferedReader br = new BufferedReader(isr);
FileOutputStream fw = new FileOutputStream("D:\\CopyTest2.txt");
OutputStreamWriter isw = new OutputStreamWriter(fw,"utf-8");
BufferedWriter bw = new BufferedWriter(isw);
int flag = 0;
while((flag = br.read())!=-1){
bw.write(flag);
}
bw.close();
br.close();
}
}