在Java面试中File的笔试题很多。出镜率比较高的是递归遍历文件夹。其次在有的项目里,文件过大,需要进行文件分块后再进行合并。也写了一个简单的操作。遍历文件夹顺便也取出文件夹里最大文件以及最小文件。记录一下。码上
package com.macro.mall.demo.test.io;
import java.io.*;
public class FileTest {
private static long minLength = Integer.MAX_VALUE;
private static long maxLength = 0;
private static File minFile;
private static File maxFile;
public static void main(String[] args) {
File file = new File("e:\\else");
fileTraverse(file);
System.out.println("最大文件:" + maxFile.getName() + "大小为:" + maxLength);
System.out.println("最小文件:" + minFile.getName() + "大小为:" + minLength);
File splitFile = new File("d:\\pic\\basic\\split.png");
splitFile(splitFile);
File mergeFile = new File("d:\\pic\\test\\merge.png");
mergeFile(mergeFile);
File srcFile = new File("e:\\aa");
File targetFile = new File("e:\\bb");
folderCopy(srcFile, targetFile);
}
private static void fileTraverse(File file) {
File[] files = file.listFiles();
for (File childFile : files) {
if (childFile.isFile()) {
if (childFile.length() < minLength && childFile.length() > 0) {
minLength = childFile.length();
minFile = childFile;
} else if (childFile.length() > maxLength) {
maxLength = childFile.length();
maxFile = childFile;
}
} else {
fileTraverse(childFile);
}
}
}
private static void folderCopy(File srcFolder, File targetFolder) {
try {
if (!targetFolder.exists()) {
targetFolder.mkdir();
}
File[] srcFiles = srcFolder.listFiles();
for (File srcFile : srcFiles) {
File targetFile = new File(targetFolder.getAbsoluteFile() + "/" + srcFile.getName());
if (!srcFile.isFile()) {
targetFile.mkdir();
folderCopy(srcFile, targetFile);
} else {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(targetFile);
byte[] bytes = new byte[1024 * 8];
int len = fis.read(bytes);
while (len != -1) {
fos.write(bytes, 0, len);
len = fis.read(bytes);
}
fis.close();
fos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void splitFile(File file) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len, index = 0;
while ((len = fis.read(bytes)) != -1) {
fos = new FileOutputStream("d:\\pic\\test\\merge-" + (index++) + ".png");
fos.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fis != null;
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
assert fos != null;
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void mergeFile(File file) {
FileInputStream fis = null;
FileOutputStream fos = null;
int index = 0;
try {
fos = new FileOutputStream(file);
while (true) {
String name = file.getName().substring(0, file.getName().lastIndexOf("."));
File childFile = new File(file.getParent(), name + "-" + (index++) + ".png");
if (!childFile.exists()) {
System.out.println("合并完毕!");
break;
}
fis = new FileInputStream(childFile);
byte[] bytes = new byte[(int) childFile.length()];
fis.read(bytes);
fis.close();
fos.write(bytes);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fos != null;
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}