简介
文件操作相关类的分类及继承关系:
image.png
什么是流?
流是一个抽象的概念,统一管理数据的写入和读取,开发者只需要将内存里面的数据写入Stream流里面, 或者从流里面读取数据。
I / O流概念
流的方向:参考自己的内存空间
输出流:从内存空间将数据写到外部设备(磁盘、光盘、硬盘) 如:将数据写入文件
输入流:将外部数据写到内存中 如:将数据从文件读取到内存中来
字节流和字符流的概念及区别
字节流:
1.字节流操作的基本单元为字节
2.字节流读取的时候,读到一个字节就返回一个字节
3.字节流默认不使用缓冲区
4.字节流通常用于处理二进制数据,实际上它可以处理任意类型的数据,但它不支持直接写入或读取Unicode码元
5.主要用于读取图片,MP3,AVI视频文件
字符流:
1.字符流操作的基本单元为Unicode码元
2.字符流使用了字节流读到一个或多个字节,如读取中文时,就会一次读取2个字节
3.字符流使用缓冲区
4.字符流通常处理文本数据,它支持写入及读取Unicode码元
5.只要是处理纯文本数据,就要优先考虑使用字符流
目录
1.创建File对象
2.使用字符流写入、读取数据
3.使用字节流操作
4.对象的文件操作
详解
1.创建File对象
import java.io.File;
import java.io.IOException;
public class MyClass {
public static void main(String[] args) throws IOException {
// 创建文件的完整路径
String path = "C:\\Users\\Inuyasha\\Desktop\\yzl.txt";
// 创建File对象,将文件路径传过来
File file = new File(path);
//判断文件是否存在,不存在就创建一个
if (file.exists() == false){
System.out.println("文件不存在");
file.createNewFile();
}
}
}
2.使用字符流写入、读取数据
完成上一步操作后
1.使用FileWriter、FileReader
// 使用字符流写入、读取数据
// 写入
Writer writer = new FileWriter(file);
char[] outputText = {'文','件','操','作'};
writer.write(outputText);
writer.close();
// 读取
Reader reader = new FileReader(file);
char[] inputText = new char[4];
reader.read(inputText);
System.out.println(inputText);
inputText.clone();
输出结果:文件操作
到目录文件查看内容:文件操作
2.使用BufferedWriter、BufferedReader
// 使用字符流写入、读取数据
// 写入
Writer writer = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
String outputText = "文件操作";
bufferedWriter.write(outputText);
bufferedWriter.flush();
bufferedWriter.close();
writer.close();
// 读取
Reader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String text = bufferedReader.readLine();
System.out.println(text);
reader.close();
bufferedReader.close();
结果与上相同,因为使用了Buffer,当文件够大时,第二种比第一种效率更高
3.使用字节流操作
1.使用FileInputStream、FileOutputStream,对图片进行复制粘贴
// 使用字节流文件操作 对图片进行复制粘贴
//1.源文件的路径
String sourcePath = "C:\\Users\\Inuyasha\\Desktop\\p.jpg";
//2.目标文件的路径
String desPath = "C:\\Users\\Inuyasha\\Desktop\\p2.jpg";
//创建文件的输入输出流
FileInputStream inputStream = new FileInputStream(sourcePath);
FileOutputStream outputStream = new FileOutputStream(desPath);
//定义byte数组 作为数据传递的载体
byte[] data = new byte[1024];
//记录每次数据的长度
int count = 0;
//判断是否读到数据 直到读完为止
while((count = inputStream.read(data)) != -1){
//读到了数据 将数据一次 1kb 的写入目标文件
outputStream.write(data,0,count);
}
inputStream.close();
outputStream.close();
2.使用BufferedInputStream、BufferedOutputStream对视频进行操作
对文件进行复制粘贴
// 使用字节流文件操作 对视频进行复制粘贴
//1.源文件的路径
String sourcePath = "C:\\Users\\Inuyasha\\Desktop\\p.mov";
//2.目标文件的路径
String desPath = "C:\\Users\\Inuyasha\\Desktop\\p2.mov";
//创建文件的输入输出流
FileInputStream fis = new FileInputStream(sourcePath);
BufferedInputStream inputStream = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(desPath);
BufferedOutputStream outputStream = new BufferedOutputStream(fos);
//定义byte数组 作为数据传递的载体
byte[] data = new byte[1024];
//记录每次数据的长度
int count = 0;
//判断是否读到数据 直到读完为止
while((count = inputStream.read(data)) != -1){
//读到了数据 将数据一次 1kb 的写入目标文件
outputStream.write(data,0,count);
}
fis.close();
fos.close();
inputStream.close();
outputStream.close();
使用Buffered操作,效率高于第一种
4.对象的文件操作
先创建 一个Person类
注意:需要进行文件操作的对象,其类必须实现Serializable接口;有内部类的话,内部类也要实现Serializable接口
import java.io.Serializable;
public class Person implements Serializable {
String name;
int age;
}
在将对象写入、读取
public class MyClass {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 创建文件的完整路径
String path = "C:\\Users\\Inuyasha\\Desktop\\yzl.txt";
// 创建File对象,将文件路径传过来
File file = new File(path);
//判断文件是否存在,不存在就创建一个
if (file.exists() == false){
System.out.println("文件不存在");
file.createNewFile();
}
//使用 Object I / O 对对象进行操作
//写入
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
Person xw = new Person();
xw.name = "xw";
xw.age = 18;
objectOutputStream.writeObject(xw);
objectOutputStream.close();
//读取
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
Person person = (Person)objectInputStream.readObject();
System.out.println("name = " + person.name + " age = " + person.age);
objectInputStream.close();
}
}
输出结果:name = xw age = 18
学习心得
文件操作在Java中占着重要地位,大一些的数据保存都是吏部开文件操作的。文件操作的问题一般都是出在路径的不正确,只要确保路径的正确性,其他方法的使用出错几率倒是不大。同时需要分清什么时候该用什么类,根据需求灵活调用不同的类和方法。
image.png