IO
- 用于处理数据传输
- 操作以“流"的方式进行
- java.io包
- 输入流:外部数据->程序
- 输出流; 程序->存储设备
流的分类
- 单位不同:字节流(8bit):二进制文件,字符流(字符):文本文件
- 流向不同:输入流,输出流
- 角色不同:节点流,处理流/包装类
处理流/包装流类中包含了节点流,封装节点流
文件操作
创建文件对象相关构造器和方法
-
new File(String pathname) //根据路径构建一个File对象
// 指定路径加文件名 String fileName = "e:\\new01.txt"; File file = new File(fileName); try { file.createNewFile(); System.out.println("文件创建成功"); } catch (IOException e) { e.printStackTrace(); } -
new File(File parent,String child) //根据父目录文件+子路径构建
// File(File parent, String child) File fileParent = new File("e:\\"); String child = "new02.txt"; File file = new File(fileParent, child); try { file.createNewFile(); System.out.println("文件创建成功!"); } catch (IOException e) { e.printStackTrace(); } -
new File(String parent,String child) //根据父目录+子路径构建
// File(string ,string) String Parent = "e:\\"; String child = "new03.txt"; File file = new File(Parent, child); try { file.createNewFile(); System.out.println("文件创建成功!"); } catch (IOException e) { e.printStackTrace(); } } -
createNewFile() 将文件写入磁盘中,真正创建文件
try { file.createNewFile(); System.out.println("文件创建成功!"); } catch (IOException e) { e.printStackTrace(); }
常用方法
getName、getAbsolutePath、getParent、length、exists、IsFile、isDirectory
//获取文件对象
File file = new File("e:\\new01.txt");
//获取文件名称
System.out.println(file.getName());
//获取文件绝对路径
System.out.println(file.getAbsoluteFile());
// 获取文件父目录
System.out.println(file.getParent());
//获取文件大小(按字节来算)
System.out.println(file.length());
// 文件是否存在
System.out.println(file.exists());
// 是否是一个文件
System.out.println(file.isFile());
// 是否是一个目录
System.out.println(file.isDirectory());
目录的操作和文件删除
mkdir()创建一级目录,mkdirs()创建多级目录,delete 删除目录
拷贝文件
public static void main(String[] args) {
//创建文件输入流
String filePath = "e:\\0724002.jpeg";
String newFilePath = "e:\\Eason.jpeg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int readLen = 0;
try {
fileInputStream = new FileInputStream(filePath);
fileOutputStream = new FileOutputStream(newFilePath);
//读写文件
while ((readLen = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, readLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
拷贝文本
public static void main(String[] args) {
//写入文件
//读取文件并输出
String filePath = "e:\\红楼梦.txt";
String newFilePath = "e:\\E.txt";
FileWriter fileWriter = null;
FileReader fileReader = null;
try {
char[] chars = new char[1024];
fileReader = new FileReader(filePath);
fileWriter = new FileWriter(newFilePath);
int charLen = 0;
while ((charLen = fileReader.read(chars)) != -1) {
fileWriter.write(chars, 0, charLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对象流
ObjectOutputStream和ObjectInputStream 两者都为包装流/处理流
序列化和反序列化
- 序列化->保存值和数据类型
- 反序列化->恢复值和数据类型
- 需要实现接口
- Serializable 推荐该接口,该接口是标记接口,没有方法
- Exteenalizable
ObjectOutputStream
序列化
public static void main(String[] args) throws IOException {
String filePath = "e:\\object.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
//写入数据
oos.writeInt(100);
oos.writeBoolean(true);
oos.writeChar('1');
oos.writeByte(1000);
oos.writeDouble(1.1);
//写入字符串
oos.writeUTF("你好啊");
//写入对象
oos.writeObject(new Dog("小明",100));
oos.close();
System.out.println("结束");
}
class Dog implements Serializable{
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
ObjectInputStream
反序列
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filePath = "e:\\object.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
//读取顺序要与存入顺序一致
/*
oos.writeInt(100);
oos.writeBoolean(true);
oos.writeChar('1');
oos.writeByte(1000);
oos.writeDouble(1.1);
//写入字符串
oos.writeUTF("你好啊");
*/
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readByte());
System.out.println(ois.readDouble());
System.out.println(ois.readUTF());
//读取对象时,要将其类设为公共类或者是同一个包下面的类,否则会报错
// 使用对象方法时,需要向下转型
Object o = ois.readObject();
o = (Dog) o;
System.out.println(o);
ois.close();
}
注意事项
- 读写顺序一致
- 序列化和反序列化对象,需要实现Serializable
- 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
- 表示该对象的版本,若有修改,则视为该类的升级版
- 序列化对象时,默认将里面所有属性都进行序列化,除了static或transient修饰的成员
- 序列化对象时,要求里面属性的类型也需要实现序列化接口
- 序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化