系列文章目录
文件IO操作之文件内容操作
前言
承接上文,文件IO的操作主要是文件系统的操作和文件内容的操作。在本文中将着重讲解文件内容的操作。
流(Stream)
文件内容的操作分为读文件、写文件、打开文件、关闭文件等操作。
引入流的概念,主要是对文件内容的操作如同水流一样灵活,无论是读取文件还是写入文件,它的频率和次数等因素都可以控制。
在Java中,操作文件内容的流主要分为两种:字节流和字符流。
字节流以**字节为单位读写数据,专门用于二进制文件。InputStream和OutputStream两种类可以进行操作。
字符流以字符为单位读写数据,专门用于文本文件。**Reader和Writer两种类可以进行读写操作。
字节输入流(InputStream)
理论上,InputStream类中提供的方法就可以让我们对文件进行操作了。但是我们可以通过源码得知,InputStream类是实现了Closeable的抽象类,是无法被实例化的。
public abstract class InputStream implements Closeable
对于这种情况,Java标准库提供了InputStream类的子类,具体实现了InputStream中的方法。他就是FileInputStream。
FileInputStream
在该类中,提供了三种构造方法,通过传入的参数:字符串表示的文件路径/File对象
通过向上转型new一个FileInputStream对象之后,需要抛出FileNotFoundException异常,该异常是IOException的子类。
public static void main(String[] args) {
try {
//向上转型
InputStream inputStream = new FileInputStream("./test.txt");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
打开路径上的对应文件,如果打开成功,接下来就可以开始读取文件的内容。通过方法read来读取。
read方法中,提供了两种不同的版本。
- 无参数,一次读取一个字节,读取到的内容通过返回值表示,当内容读取完之后就会返回数值-1表示结束。
- 以数组byte[]作为参数,接收读取到的数据。read会尽可能把数组空间填满,最后返回int类型数值n。通过读取数组中的元素获取文件内容更多字节编码。
代码案例如下
public static void main(String[] args) throws IOException {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("./test.txt");
} finally {
inputStream.close();//在执行完之后关闭文件
}
}
在文件打开之后,会在PCB中的“文件描述符表”中添加一个元素,如果不关闭文件,日积月累下来会导致文件操作的许多问题,因此关闭文件的操作是非常必要的。
每次在finally后面进行文件操作过于繁琐,于是提供了我们在try中进行操作,把流对象的创建写到try里,当代码执行完成之后就会自动调用inputStream中的close操作。
try(InputStream inputStream = new FileInputStream("paper.png")) {
while (true) {
byte[] buffer = new byte[1024];
int n = inputStream.read(buffer);
if (n == -1) {
break;
}
for (int i = 0; i < n; i++) {
System.out.printf("%x ",buffer[i]);
}
}
}
字节输出流(OutPutStream)
在输出过程中,主要执行的是写操作。与InputStream类似,它也提供了三个write的方法。同样是无参和数组的方式进行写操作。这里就不再赘述。
对文件进行写操作,通过写方式打开文件的时候,会将文件原有内容清空。在write方法中传入的参数为字符的ASCII码值。
public static void main(String[] args) throws IOException {
OutputStream outputStream = new FileOutputStream("./test.txt");
outputStream.write(97);
}
在运行上面的代码之前,txt文件中的内容为你好世界,运行完之后,文件内容变成了a
原因:默认的写方式打开,会清空原有的内容,而使用“追加写”方式就不会出现这个问题。
如何使用追加写
在FileOutputStream中提供了追加写的判定方式。
通过修改append参数的Boolean值就可以进行追加写操作了。代码案例如下所示
public static void main(String[] args) throws FileNotFoundException {
try(OutputStream outputStream = new FileOutputStream("./test.txt",true)) {
byte[] buffer = new byte[] {97,98,99,100};
outputStream.write(buffer);
}catch (IOException e) {
e.printStackTrace();
}
}
字符输入流(Reader)
字符流和字节流的基本流程都差不多。
关于输入流Reader,与字节输入流相同的是抽象类。同样的,Java标准库提供了Reader的子类FileReader。
FileReader同样是read方法,操作与InputStream一致。这里不再赘述,附上代码案例。
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("./test.txt");
while (true) {
char[] chars = new char[1024];
int n = reader.read(chars);
if (n == -1) {
break;
}
for (int i = 0; i < n; i++) {
System.out.printf("%c",chars[i]);
}
}
}
字符输出流(Writer)
Writer与OutputStream一样,实现方法的子类为FileWriter。
try(Writer writer = new FileWriter("./test.txt",true)) {
String str = "hello hello";
writer.write(str);
}catch (IOException e) {
e.printStackTrace();
}
注意:Writer同样需要进行追加写操作才会不覆盖原有内容。
关键词查询匹配
通过所要查找的关键词SearchWord和查找的目录路径path,找到符合关键词的文件。
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;
public class Demo08 {
//通过关键词查找路径下的类似文件
public static void main(String[] args) {
//1.输入要查找的文件名
Scanner scanner = new Scanner(System.in);
System.out.println("输入要搜索的文件名:");
String SearchWord = scanner.next();
//2.输入文件目录
System.out.println("输入目录:");
String path = scanner.next();
//3.排查path是否为目录
File Dir = new File(path);
System.out.println(Dir.getAbsolutePath());
if (!Dir.isDirectory()) {
System.out.println("该路径非法!");
return;
}
//4.在确定了路径位目录之后,开始排查目录中的文件
SearchFile(SearchWord,Dir);
}
private static void SearchFile(String SearchWord, File Dir) {
File[] files = Dir.listFiles();
if (Dir == null){
return;
}
// for (File file : files) {
// System.out.println(file.getAbsolutePath());
// }
for (File f : files) {
if (f.isFile()) {
String fileName = f.getName();
if (fileName.contains(SearchWord)) {
File file = new File(fileName);
System.out.println("找到匹配结果:" + file.getPath());
}
} else if (f.isDirectory()) {
SearchFile(SearchWord,f);
}
}
}
}
总结
在文件内容操作的过程中,主要有以下几个核心步骤。
- 文件打开、文件路径的构造。
- 文件内容的操作。
- 关闭文件。
对于关闭文件我们可以通过try()来提高编码的效率。
文件IO操作的源码☞文件IO操作