IO其他流、从字节数组读取相关的流等
文章目录
节点流
数组的长度有限,数据量不会很大
输入流 操作与 文件输入流一致
文件内容不用太大
1、文件内容 --程序–> 字节数组
- 文件输入流 字节数组输出流
2、字节数组 --程序–> 文件
- 文件输出流 字节数组输入流
字节数组 字节 节点流
输入流 ByteArrayInputStream
read(byte[] b, int off, int len) + close()
输出流 ByteArrayOutputStream
write(byte[] b, int off, int len) + toByteArray()
不要使用多态?
Code
package com.iotest.others;
import java.io.*;
public class Demo01ByteArray {
public static void main(String[] args) {
try {
read(write());
} catch (IOException e) {
System.out.println("error");
}
}
public static void read(byte[] src) throws IOException {
// 数据源传入
// 选择流
InputStream is = new BufferedInputStream(
new ByteArrayInputStream(
src
)
);
// 操作
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len=is.read(flush))) {
System.out.println( new String(flush,0,len));
}
is.close();
}
public static byte[] write() throws IOException {
// 目的地
byte[] dest;
// 不同点
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 操作写出
String msg = "操作与文件输入流操作一致";
byte[] info = msg.getBytes();
bos.write(info,0,info.length);
// 获取数据
dest = bos.toByteArray();
// 释放资源
bos.close();
return dest;
}
}
文件内容不用太大
1、文件内容 --程序–> 字节数组
- 文件输入流 字节数组输出流
2、字节数组 --程序–> 文件
- 文件输出流 字节数组输入流
package com.iotest.others;
import java.io.*;
public class Demo02ByteArray {
public static void main(String[] args) throws IOException {
byte[] data = getBytesFromFile(
"D:/Code/Java/IO/src/main/resources/test.png"
);
toFileFromByteArray(data,"D:/Code/Java/IO/src/main/resources/2test.png");
//System.out.println(new String(data));
}
/**
* 文件内容 --程序--> 字节数组
* @return
*/
public static byte[] getBytesFromFile(String srcPath) throws IOException {
// 创建文件源
File src = new File(srcPath);
// 创建目的地
byte[] dest = null;
// 选择流
// 文件输入流
InputStream is = new BufferedInputStream(new FileInputStream(src));
// 字节数组输出流 不能使用多态、
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//操作 不断读取文件 写出到字节数组流中
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = is.read(flush))) {
bos.write(flush, 0, len);
}
bos.flush();
// 获取资源
dest = bos.toByteArray();
is.close();
return dest;
}
/**
* 字节数组 --程序--> 文件
* @param src
* @param destPath
*/
public static void toFileFromByteArray(byte[] src,String destPath) throws IOException{
// 创建源
// 创建目的地
File dest = new File(destPath);
//选择流
//字节数组输入流
InputStream is = new BufferedInputStream(new ByteArrayInputStream(src));
//文件输出流
OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
//操作 不断读取字节数组
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = is.read(flush))) {
os.write(flush, 0, len);
}
os.flush();
// 释放资源
os.close();
is.close();
}
}
处理流
基本类型 + String 保留数据 + 类型
输入流:DataInputStream readXxx
输出流:DataOutputStream writeXxx
java.io.EOFException 没有读取到相关的内容
读取顺序和写入不一致,可能存在数据问题
Code
从文件
public static void main(String[] args) throws IOException {
write("D:/Code/Java/IO/src/main/resources/2.txt");
read("D:/Code/Java/IO/src/main/resources/2.txt");
}
/**
* 数据+类型输出到文件
* @param destPath
* @throws IOException
*/
public static void write(String destPath) throws IOException {
double point = 2.6;
long num = 199L;
String str = "hello world";
// 创建源
File dest = new File(destPath);
// 选择流
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(dest)
)
);
// 操作 写出的顺序 为读取做准备
dos.writeDouble(point);
dos.writeLong(num);
dos.writeUTF(str);
dos.flush();
dos.close();
}
/**
* 从文件读取数据+类型
* @param destPath
*/
public static void read(String destPath) throws IOException {
File src = new File(destPath);
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(src)
)
);
// 操作 读取顺序与写出一直 必须存在才能读取
double num1 = dis.readDouble();
long num2 = dis.readLong();
String str2 = dis.readUTF();
System.out.println(str2);
}
从字节数组
public static void main(String[] args) throws IOException {
byte[] data = write();
read(data);
}
/**
* 数据+类型输出到字节数组中
* @return
* @throws IOException
*/
public static byte[] write() throws IOException {
// 目标数组
byte[] dest = null;
double point = 2.6;
long num = 199L;
String str = "hello world";
// 选择流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
bos
)
);
// 操作 写出的顺序 为读取做准备
dos.writeDouble(point);
dos.writeLong(num);
dos.writeUTF(str);
dos.flush();
dest = bos.toByteArray();
dos.close();
return dest;
}
/**
* 从字节数组读取数据+类型
* @param src
*/
public static void read(byte[] src) throws IOException {
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new ByteArrayInputStream(src)
)
);
// 操作 读取顺序与写出一直 必须存在才能读取
double num1 = dis.readDouble();
long num2 = dis.readLong();
String str2 = dis.readUTF();
dis.close();
System.out.println(str2);
}
引用类型(对象)保留数据 + 类型
反序列化 输入流:ObjectInputStream readObject()
序列化 输出流:ObjectOutputStream writeObject()
注意:
1)先序列化后反序列化,反序列化顺序必须与序列化一直
2)不是所有的对象都可以序列化,必须实现java.io.Serializable
不是所有的属性都需要序列化,transient
- 新增方法不能发生多态
Code
package com.iotest.others;
import java.io.*;
public class Demo05Object {
public static void main(String[] args) throws IOException, ClassNotFoundException {
seri("D:/Code/Java/IO/src/main/resources/2.txt");
read("D:/Code/Java/IO/src/main/resources/2.txt");
}
/**
* 序列化
* @param destPath
* @throws IOException
*/
public static void seri(String destPath) throws IOException {
Employee emp = new Employee("aaa", 5000);
File dest = new File(destPath);
// 选择流
ObjectOutputStream dos = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(dest)
)
);
// 操作 写出的顺序 为读取做准备
dos.writeObject(emp);
dos.flush();
dos.close();
}
public static void read(String destPath) throws IOException, ClassNotFoundException {
File src = new File(destPath);
ObjectInputStream dis = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(src)
)
);
// 操作 读取顺序与写出一直 必须存在才能读取
Object obj = dis.readObject();
if (obj instanceof Employee){
Employee emp =(Employee)obj;
System.out.println(emp.getName());
System.out.println(emp.getSalary());
}
dis.close();
}
}