复习了下Java IO流知识,使用一个工具类拷贝文件。后续如果复习有时间的话,复习下使用Java窗体来操作
public final class FileUtil {
private final static String EMPTY_STRING = "";
private static FileInputStream fis;
private static FileOutputStream fos;
private static BufferedInputStream bis;
private static BufferedOutputStream bos;
private final static byte[] b = new byte[1024 * 1024];
private static int i = -1;
private static String sourceDirector;
private static boolean isInit = false;
/**
* 如果不指定文件地址 默认拷贝当前地址
*
* @param sourcePath 源文件地址
*/
public static void copyAndSave(String sourcePath) {
copyAndSave(sourcePath, new File("").getAbsolutePath());
}
/**
* 如果是目录文件 拷贝该目录下所有文件
* 如果是普通文件 拷贝该文件
*
* @param sourcePath 源文件地址
* @param savePath 文件保存地址
*/
public static void copyAndSave(String sourcePath, String savePath) {
File file = new File(sourcePath);
// 如果文件存在就遍历
if (file.exists()) {
// 首次加载初始化目录 如果是目录获取源文件目录 如果是文件 不处理
initDirectory(file);
// 文件拷贝
createFile(file, savePath);
// 对于目录需要遍历
if (file.isDirectory()) {
File[] files = file.listFiles();
assert files != null;
for (File f : files) {
// 拷贝目录下的文件
createFile(f, savePath);
if (f.exists() && f.isDirectory()) {
// 递归遍历该目录所有文件
copyAndSave(f.getAbsolutePath(), savePath);
}
}
}
}
}
private static boolean isEmpty(String str) {
return str == null || EMPTY_STRING.equals(str.trim());
}
/**
* 创建文件
*
* @param file 源文件
* @param savePath 保存地址
*/
private static void createFile(File file, String savePath) {
if (file.isFile() && !isEmpty(file.getName())) {
if (sourceDirector != null) {
savePath = savePath + "//" + getSourceDirector(file.getAbsolutePath());
} else {
savePath = savePath + "//" + file.getName();
}
copyFile(file.getAbsolutePath(), savePath);
}
}
/**
* 文件拷贝
*
* @param sourcePath 源文件地址
* @param copyPath 拷贝之后保存的文件地址
*/
private static synchronized void copyFile(String sourcePath, String copyPath) {
//
File sourceFile = new File(sourcePath);
File copyFile = new File(copyPath);
if (sourceFile.exists() && sourceFile.isFile()) {
// 创建文件目录
File parentFile = copyFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
try {
fis = new FileInputStream(sourceFile);
fos = new FileOutputStream(copyFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
while ((i = bis.read(b)) != -1) {
bos.write(b, 0, i);
}
bos.flush();
System.out.printf("文件保存地址=%s/n", copyPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static String getSourceDirector(String sourcePath) {
String path = null;
try {
path = sourceDirector + sourcePath.split(sourceDirector, 2)[1];
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
/**
* 首次加载初始化 获取目录
*
* @param file 源文件
*/
private static void initDirectory(File file) {
if (!isInit && file.isDirectory()) {
sourceDirector = file.getName();
isInit = true;
System.out.println("源文件目录=" + sourceDirector);
}
}
public static void main(String[] args) {
String sourcePath = "D://desktop//Learn//Demo//src//resource//image//boom1.png";
String savePath = "D://desktop//test";
FileUtil.copyAndSave(sourcePath, savePath);
}
}
目前测试了拷贝图片文件、文本文件、和目录文件,均正常。后续使用过程中出现bug在修改。
本来想添加一个拷贝文件花费时间统计,后面怕麻烦算了,因为目录文件需要递归遍历。
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/268172.html