Java 创建文件夹及文件
2016-12-03 19:04
417 查看
Java 创建文件夹及文件
转载来自:blog.csdn.net/kobi521/article/details/8814387
import java.io.File; import java.io.IOException; public class CreateFileUtil { /** * 創建文件在即將創建的目錄内 * * @param destFileName * @return */ 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 + "失败!"); return false; } } /** * 創建 dir * * @param destDirName * @return */ 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; } } /** * 創建 temp file * * @param prefix * @param suffix * @param dirName * @return */ public static String CreateTempFile(String prefix, String suffix, String dirName) { File tempFile = null; try { if (dirName == null) { // 在默认文件夹下创建临时文件 tempFile = File.createTempFile(prefix, suffix); return tempFile.getCanonicalPath(); } else { File dir = new File(dirName); // 如果临时文件所在目录不存在,首先创建 if (!dir.exists()) { if (!CreateFileUtil.CreateDir(dirName)) { System.out.println("创建临时文件失败,不能创建临时文件所在目录!"); return null; } } // 臨時文件創建 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 = "c:/test/test0/test1/test11"; CreateFileUtil.CreateDir(dirName); // 创建文件 String fileName = dirName + "/test2/testFile.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)); } } }
转载来自:blog.csdn.net/kobi521/article/details/8814387
相关文章推荐
- java 对文件的删除、剪切、移动和创建文件夹操作
- java 创建文件 创建文件目录 创建文件夹
- java创建和删除文件夹、文件
- Java创建文件夹及文件实例代码
- java创建文件文件夹,删除空文件夹,删除文件夹以及所有文件
- Java创建、重命名、删除文件和文件夹
- java 创建文件夹,读写文件
- java 在linux 下创建文件及文件夹 权限
- Java创建文件夹与创建文件
- Java创建文件夹及文件
- java创建文件夹,文件;删除文件夹,文件
- java创建文件夹,文件;删除文件夹,文件
- java 创建文件夹和文件
- java 创建文件夹 创建文件 判断文件夹是否存在。。。。。
- 创建文件夹和文件的java代码范例
- java创建和删除文件夹及文件
- Java 创建文件、文件夹以及临时文件
- java 对文件的删除、剪切、移动和创建文件夹操作
- Java创建文件夹及文件
- Java学习之创建和删除文件夹&文件(转)