- 流是一组有起点、终点和顺序的字节集合。
输入流:读文件
输出流:写文件
字节流:数据流中最小的数据单元是字节(byte)。
字符流:数据流中最小的数据单元是字符,java中的字符是Unicode编码,一个字符占两个字节。
import java.io.*;
public class FileDemo {
//字节流 读文件
public static void readFile() throws IOException {
//文件管理类
File file = new File("E:/tmp.txt");
//字节输入流
InputStream in = new FileInputStream(file);
//读取字节数
int size = in.available();
for (int i = 0; i < size; ++i){
System.out.print((char) in.read());
}
System.out.println();
in.close();
}
//字节流 写文件
public static void writeFile() throws IOException {
File file = new File("E:/tmp.txt");
//不存在则创建
if (!file.exists()){
file.createNewFile();
}
//创建字节输出流,追加写入
OutputStream out = new FileOutputStream(file, true);
//要写入的信息
String str = "abc";
out.write(str.getBytes());
//信息写入文件
out.flush();
out.close();
}
//字符流 读文件
public static void readFile2() throws IOException {
File file = new File("E:/tmp.txt");
//创建字节输出流
InputStream in = new FileInputStream(file);
//转化为字符输出流
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
//字符流处理类
BufferedReader br = new BufferedReader(reader);
String str = br.readLine();
while (str != null){
System.out.println(str);
str = br.readLine();
}
//关闭资源
br.close();
reader.close();
in.close();
}
public static void writeFile2() throws IOException {
File file = new File("E:/tmp.txt");
if(!file.exists()){
file.createNewFile();
}
OutputStream out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write("老王");
bw.flush();
bw.close();
writer.close();
out.close();
}
public static void main(String[] args) {
try{
writeFile();
readFile();
writeFile2();
readFile2();
}catch (Exception e){
e.printStackTrace();
}
}
}
File类常用方法
大文件I/O实例
import java.io.*;
public class MediaCopy {
public static void copyMediaFile() throws IOException {
File sourFile = new File("E:/tmp.mp4");
if(!sourFile.exists()){
System.out.println("File is not found.");
return;
}
InputStream in = new FileInputStream(sourFile);
OutputStream out = new FileOutputStream(new File("E:/tmp2.mp4"));
byte[] bytes = new byte[1024];
int size;
while((size = in.read(bytes)) != -1){
out.write(bytes, 0, size);
out.flush();
}
out.close();
in.close();
}
public static void main(String[] args) {
try {
copyMediaFile();
}catch (Exception e){
e.printStackTrace();
}
}
}
序列化与反序列化
- 序列化:将对象转化为特定格式的字符串。
- 反序列化:将特定格式的字符串转化为对象。
- 序列化ID用来检验版本一致性。
- 先让类继承 Serializable接口,并设置序列化id作为类的成员变量
import java.io.Serializable;
public class User implements Serializable {
//序列化ID
private static final long serialVersionUID = 2021121301142583248L;
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
'}';
}
}
- 使用Object流实现I/O
import java.io.*;
public class Serial {
//序列化
public static void serialize(Object object) throws IOException {
File file = new File("E:/tmp.txt");
if(!file.exists()){
file.createNewFile();
}
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
//对象序列化
out.writeObject(object);
out.flush();
out.close();
}
public static Object deserialize(String path) throws IOException, ClassNotFoundException {
File file = new File(path);
if(!file.exists()){
System.out.println("File is not found.");
return null;
}
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
Object object = in.readObject();
in.close();
return object;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
User user = new User("xiaoming");
//序列化
serialize(user);
//反序列化
User user2 = (User)deserialize("E:/tmp.txt");
System.out.println(user2);
}
}



