100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 工具类的方法怎么引用_Java实用工具类:File工具类方法学习 可创建目录及文件...

工具类的方法怎么引用_Java实用工具类:File工具类方法学习 可创建目录及文件...

时间:2023-12-01 09:00:12

相关推荐

工具类的方法怎么引用_Java实用工具类:File工具类方法学习 可创建目录及文件...

开发项目过程中,会用到很多工具类,今天分享一个Java中File操作工具类,可以帮你节约时间,提高开发效率。

File工具类

public class CreateFileUtil {

public static boolean createFile(String destFileName) {

File file = new File(destFileName);

if(file.exists()) {

System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");

return false;

}

if (destFileName.endsWith(File.separator)) {

System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");

return false;

}

//判断目标文件所在的目录是否存在

if(!file.getParentFile().exists()) {

//如果目标文件所在的目录不存在,则创建父目录

System.out.println("目标文件所在目录不存在,准备创建它!");

if(!file.getParentFile().mkdirs()) {

System.out.println("创建目标文件所在目录失败!");

return false;

}

}

//创建目标文件

try {

if (file.createNewFile()) {

System.out.println("创建单个文件" + destFileName + "成功!");

return true;

} else {

System.out.println("创建单个文件" + destFileName + "失败!");

return false;

}

} catch (IOException e) {

e.printStackTrace();

System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());

return false;

}

}

public static boolean createDir(String destDirName) {

File dir = new File(destDirName);

if (dir.exists()) {

System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");

return false;

}

if (!destDirName.endsWith(File.separator)) {

destDirName = destDirName + File.separator;

}

//创建目录

if (dir.mkdirs()) {

System.out.println("创建目录" + destDirName + "成功!");

return true;

} else {

System.out.println("创建目录" + destDirName + "失败!");

return false;

}

}

public static String createTempFile(String prefix, String suffix, String dirName) {

File tempFile = null;

if (dirName == null) {

try{

//在默认文件夹下创建临时文件

tempFile = File.createTempFile(prefix, suffix);

//返回临时文件的路径

return tempFile.getCanonicalPath();

} catch (IOException e) {

e.printStackTrace();

System.out.println("创建临时文件失败!" + e.getMessage());

return null;

}

} else {

File dir = new File(dirName);

//如果临时文件所在目录不存在,首先创建

if (!dir.exists()) {

if (!CreateFileUtil.createDir(dirName)) {

System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");

return null;

}

}

try {

//在指定目录下创建临时文件

tempFile = File.createTempFile(prefix, suffix, dir);

return tempFile.getCanonicalPath();

} catch (IOException e) {

e.printStackTrace();

System.out.println("创建临时文件失败!" + e.getMessage());

return null;

}

}

}

public static void main(String[] args) {

//创建目录

String dirName = "D:/work/temp/temp0/temp1";

CreateFileUtil.createDir(dirName);

//创建文件

String fileName = dirName + "/temp2/tempFile.txt";

CreateFileUtil.createFile(fileName);

//创建临时文件

String prefix = "temp";

String suffix = ".txt";

for (int i = 0; i < 10; i++) {

System.out.println("创建了临时文件:"

+ CreateFileUtil.createTempFile(prefix, suffix, dirName));

}

//在默认目录下创建临时文件

for (int i = 0; i < 10; i++) {

System.out.println("在默认目录下创建了临时文件:"

+ CreateFileUtil.createTempFile(prefix, suffix, null));

}

}

}

以上,是我实际项目中用过的File工具类,仅供参考,有什么好的方法,可以评论区交流。

我是一名码龄的程序员,在这里会分享实在干货,让你少走弯路,成就精彩人生。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。