commons-io
是Apache Commons项目的一个模块,提供了一系列处理I/O(输入/输出)操作的工具类和方法。它旨在简化Java I/O编程,并提供更多的功能和便利性。
读取文件内容为字符串
String path="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt";
System.out.println(FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8));
读取文件内容为List
String path="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt";
List list = FileUtils.readLines(new File(path), StandardCharsets.UTF_8);
获取文件属性
String path="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt";
System.out.println(FilenameUtils.getBaseName(path));
System.out.println(FilenameUtils.getExtension(path));
文件操作
复制文件
String path="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt";
String targetPath="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test_copy.txt";
File file = new File(path);
File targetFile = new File(targetPath);
FileUtils.copyFile(file, targetFile);
移动文件
String path="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt";
String targetPath="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test_copy.txt";
File file = new File(path);
File targetFile = new File(targetPath);
FileUtils.moveFile(file, targetFile);
安静地删除文件
String path="C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt";
File file = new File(path);
FileUtils.deleteQuietly(file);
获取指定目录下特定扩展名的所有文件
String path = "C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources";
String[] extensions = new String[]{"jpg", "png", "avi"};
File file = new File(path);
List fileList = (List) FileUtils.listFiles(file, extensions, true);
for (File f : fileList) {
System.out.println("找到文件:" + f.getAbsolutePath());
流操作
复制流
InputStream inputStream = null;
OutputStream outputStream = null;
inputStream = new FileInputStream("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt");
outputStream = new FileOutputStream("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\target.txt");
IOUtils.copy(inputStream, outputStream);
System.out.println("文件复制成功");
} catch (IOException e) {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
输入流转换为字节数组
InputStream inputStream = null;
inputStream = new FileInputStream("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt");
byte[] bytes = IOUtils.toByteArray(inputStream);
String content = new String(bytes, "UTF-8");
System.out.println(content);
} catch (IOException e) {
IOUtils.closeQuietly(inputStream);
文件过滤器
并逻辑过滤器
FileFilter filter1 = FileFilterUtils.suffixFileFilter(".txt");
FileFilter filter2 = FileFilterUtils.sizeFileFilter(1024);
FileFilter combinedFilter = FileFilterUtils.and((IOFileFilter) filter1, (IOFileFilter) filter2);
File dir = new File("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources");
File[] files = dir.listFiles(combinedFilter);
for (File file : files) {
System.out.println(file.getName());
或逻辑过滤器
FileFilter filter1 = FileFilterUtils.suffixFileFilter(".txt");
FileFilter filter2 = FileFilterUtils.sizeFileFilter(1024);
FileFilter combinedFilter = FileFilterUtils.or((IOFileFilter) filter1, (IOFileFilter) filter2);
File dir = new File("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources");
File[] files = dir.listFiles(combinedFilter);
for (File file : files) {
System.out.println(file.getName());
文件后缀过滤器
FileFilter filter = FileFilterUtils.suffixFileFilter(suffix);
File dir = new File("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources");
File[] files = dir.listFiles(filter);
for (File file : files) {
System.out.println(file.getName());
字节操作
输入流中读取数据到字节数组
byte[] buffer = new byte[1024];
InputStream input = new FileInputStream("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test.txt");
int bytesRead = IOUtils.read(input, buffer);
IOUtils.closeQuietly(input);
System.out.println("实际读取的字节数: " + bytesRead);
System.out.println("读取的数据: " + new String(buffer, 0, bytesRead));
将字节数组写入输出流
byte[] data = "Hello, world!".getBytes();
OutputStream output = new FileOutputStream("C:\\Users\\zhang\\Desktop\\myyii\\demo\\src\\main\\resources\\test1.txt");
IOUtils.write(data, output);
IOUtils.closeQuietly(output);
目录操作
强制创建目录
File directory = new File("path/to/directory");
FileUtils.forceMkdir(directory);