目录
一. 文件管理
在 Java 中,文件管理是一个重要的方面,它允许我们对文件和目录进行操作,包括读取、写入、创建、删除等。
文件读取:
可以使用 FileReader
、 BufferedReader
等类来读取文件的内容。例如:
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine())!= null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
文件写入:
通过 FileWriter
、 BufferedWriter
类来实现文件的写入操作。比如:
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("This is a test line.");
} catch (IOException e) {
e.printStackTrace();
}
文件创建和删除:
使用 File
类来创建和删除文件或目录。
File file = new File("newFile.txt");
try {
if (file.createNewFile()) {
System.out.println("File created successfully.");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
// 删除文件
if (file.delete()) {
System.out.println("File deleted successfully.");
} else {
System.out.println("Failed to delete the file.");
}
文件属性获取:
能够获取文件的名称、大小、修改时间等属性。
File file = new File("example.txt");
System.out.println("File name: " + file.getName());
System.out.println("File size: " + file.length());
文件管理在许多应用中都非常关键,比如数据存储、日志记录、配置文件处理等。例如,在一个日志系统中,程序需要不断地将日志信息写入到文件中;在一个文件备份工具中,需要遍历目录并复制或移动文件。
二. I/O流
I/O(输入/输出)流是 Java 中用于处理数据输入和输出的重要机制。
输入流(InputStream):用于从数据源读取数据。常见的输入流类如 FileInputStream
用于从文件读取数据,BufferedInputStream
提供缓冲功能以提高读取效率。
try (FileInputStream fis = new FileInputStream("input.txt")) {
int data;
while ((data = fis.read())!= -1) {
// 处理读取的数据
}
} catch (IOException e) {
e.printStackTrace();
}
输出流(OutputStream):用于将数据写入到目的地。例如 FileOutputStream
用于向文件写入数据,BufferedOutputStream
用于提供缓冲以优化写入性能。
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
fos.write("Hello, World!".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
字符流(Reader/Writer):处理字符数据。FileReader
和 FileWriter
是常见的字符流类,适用于处理文本文件。
try (FileReader fr = new FileReader("text.txt")) {
int character;
while ((character = fr.read())!= -1) {
// 处理字符
}
} catch (IOException e) {
e.printStackTrace();
}
try (FileWriter fw = new FileWriter("newText.txt")) {
fw.write("Some text");
} catch (IOException e) {
e.printStackTrace();
}
缓冲流(BufferedReader/BufferedWriter 、BufferedInputStream/BufferedOutputStream):通过内部缓冲区减少实际的 I/O 操作次数,提高性能。
I/O 流在各种场景中都有广泛应用,比如:
- 从文件读取配置信息。
- 将程序的运行结果写入日志文件。
- 在网络通信中发送和接收数据。
例如,一个简单的文件复制程序可以使用 I/O 流来实现:
try (
FileInputStream fis = new FileInputStream("source.txt");
FileOutputStream fos = new FileOutputStream("destination.txt")
) {
int byteRead;
byte[] buffer = new byte[1024];
while ((byteRead = fis.read(buffer))!= -1) {
fos.write(buffer, 0, byteRead);
}
} catch (IOException e) {
e.printStackTrace();
}
三. java.io常用方法
File
类的常用方法:
exists()
:检查文件或目录是否存在。isDirectory()
:判断是否为目录。isFile()
:判断是否为文件。length()
:获取文件的长度(字节数)。createNewFile()
:创建新文件。mkdir()
:创建单级目录。mkdirs()
:创建多级目录。delete()
:删除文件或空目录。
FileInputStream
和 FileOutputStream
的常用方法:
read()
:从输入流中读取一个字节的数据。read(byte[] b)
:将数据读入字节数组。write(int b)
:向输出流写入一个字节。write(byte[] b)
:将字节数组写入输出流。
BufferedInputStream
和 BufferedOutputStream
的常用方法:
read()
:从缓冲输入流中读取一个字节。read(byte[] b)
:将数据读入字节数组。write(int b)
:向缓冲输出流写入一个字节。write(byte[] b)
:将字节数组写入缓冲输出流。
FileReader
和 FileWriter
的常用方法:
read()
:读取一个字符。read(char[] c)
:将字符读入字符数组。write(int c)
:写入一个字符。write(char[] c)
:写入字符数组。
BufferedReader
和 BufferedWriter
的常用方法:
readLine()
:读取一行文本。write(String s)
:写入字符串。
例如,使用 File
类判断文件是否存在:
File file = new File("example.txt");
if (file.exists()) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
}
使用 FileOutputStream
写入数据:
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
fos.write("Hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
使用 BufferedReader
读取文件的一行数据:
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line = reader.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
四. 文件复制的示例代码
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyExample {
public static void main(String[] args) {
String sourceFile = "source.txt";
String destinationFile = "destination.txt";
copyFile(sourceFile, destinationFile);
}
public static void copyFile(String sourcePath, String destinationPath) {
try (FileInputStream fis = new FileInputStream(sourcePath);
FileOutputStream fos = new FileOutputStream(destinationPath)) {
int byteRead;
byte[] buffer = new byte[1024];
while ((byteRead = fis.read(buffer))!= -1) {
fos.write(buffer, 0, byteRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}