FileInputStream 字节流只能读 Ascli码文件,文件有中文都不能用它读 (会把中文破开来读,变成乱码)
IO流有几种、流怎么分类 1.流按照方向分输入流,输出流
2.流按照传输单位字节流,字符流
3.Java 提供了四个基类字节输出流基类 (OutputStream)
字节输入流基类(InputStream)
字符输出流基类(Reader)
字符输入流基类(Writer)
4.字节流中常用的有文件(FileOutputStream、FileInputStream)
对象(ObjectOutputStream、ObjectInputStream)
二进制(DataOutputStream、DataInputStream)
5.字符流中常用的有文件 (FileReader、FileWriter)
一次读一行的缓冲流(BufferedReader、BufferedWriter)
6.代码实现 FileInputStream 字节文件输入流public static void main(String[] args) {
// InputStream is = new InputStream(); //抽象类不可以被实例化
try {
//文件名需要写绝对路径,路径的分隔符用正反斜杠都可以
// 正斜杠写一个即可,反斜杠需要写两个
// 因为一个反斜杠在字符串中是“转义符”
String name = "E:/io/hello.txt";
//String name = "E:\io\hello";
InputStream is = new FileInputStream(name);//文件输入流
// int d;
// while ((d = is.read() )!=-1){
// System.out.println((char) d);
// }
//创建一个byte数组作为缓冲区
byte [] data = new byte[5];
int n;
while ((n = is.read(data))!= -1){
for (int i = 0; i < n; i++) {
System.out.println((char)data[i] );
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
FileReader 字符文件输入流
public static void main(String[] args) {
String name = "E:\io\hi.txt";
try {
Reader reader = new FileReader(name);
int d;
while ((d = reader.read())!= -1){
System.out.println(d);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectOutputStream 字节对象输出流
public static void main(String[] args) {
//对象输出流 --- 对象持久化磁盘文件
Car car = new Car();
car.setBrand("法拉利");
car.setModel("911");
car.setColor("红色");
String name = "E:\io\car.txt";
try {
//创建底层的字节流对象
OutputStream os = new FileOutputStream(name);
//创建对象输出流,包装底层的字节流对象
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(car);
oos.flush();//输出的时候必须要冲刷
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectInputStream 字节对象输入流
public static void main(String[] args) {
//对象输入流,从磁盘读取文件反序列化还原为对象
String name = "E:\io\car.txt";
try {
InputStream is = new FileInputStream(name);
ObjectInputStream ois = new ObjectInputStream(is);
Car car = (Car) ois.readObject();
System.out.println(car.getBrand());
System.out.println(car.getModel());
System.out.println(car.getColor());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
DataOutputStream 二进制字节文件输出流 DataInputStream 二进制字节文件输入流
public static void main(String[] args) {
//图片输入流
String name = "E:\io\dby.jpg";//图片输入路径,文件必须存在,否则会抛出异常
String outName = "E:\io\dby_copy.jpg";//图片输出路径,文件不可以存在,会自动创建,但目录必须存在
InputStream is = null;
DataInputStream dis = null;
OutputStream os = null;
DataOutputStream dos = null;
//创建底层的字节流
try {
is = new FileInputStream(name);//字节输入流
dis = new DataInputStream(is);//二进制输入流
//创建底层的字节输出流
os = new FileOutputStream(outName);//字节输出流
dos = new DataOutputStream(os);//二进制输出流
//建立字节缓冲数组
byte [] data = new byte[1024];//内存缓冲区
int n;
while ((n = dis.read(data)) != -1){//循环读取图片中的二进制数据到缓冲区
dos.write(data,0,n);//循环把缓冲区中的字节写入磁盘考虑最后一次可能读不满冲区的问题
}
dos.flush();
while (true){}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (dis != null){
dis.close();
}
if (dos !=null){
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
序列化和反序列化
对象从内存中写入磁盘(网络)的过程称为对象的持久化,也称为序列化
从磁盘(网络)中读取文件到内存还原对象的过程称为反序列化



