java创建文件和目录
来源:互联网 发布:剑三如何导入捏脸数据 编辑:程序博客网 时间:2024/06/18 00:00
import java.io.File;
import java.io.IOException;
public class CreateFileUtil {
/**
* 创建目录
* @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;
}
}
/**
* 创建文件
* @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 + "失败!" + e.getMessage());
return false;
}
}
/**
* 创建临时文件
* @param prefix
* @param suffix
* @param dirName
* @return
*/
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));
}
}
}
import java.io.IOException;
public class CreateFileUtil {
/**
* 创建目录
* @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;
}
}
/**
* 创建文件
* @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 + "失败!" + e.getMessage());
return false;
}
}
/**
* 创建临时文件
* @param prefix
* @param suffix
* @param dirName
* @return
*/
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));
}
}
}
阅读全文
0 0
- java 创建文件和目录
- java 创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- Java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- java创建文件和目录
- 第一人称控制器-自己写哒=3= 噗噗~~
- Jsoup选择器的用法(转)
- android之与xml数据解析
- java对象克隆
- 数字滚动特效
- java创建文件和目录
- js复习笔记day8
- VS ---- 无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"
- [LeetCode]112. Path Sum
- 基于 RxJava 打造的多线程下载工具, 支持断点续传, 下载管理等
- (1)、JEasyUI 之datagrid的Combobox显示textField值的问题
- WebRTC应用中如何检测回音
- 深度学习领域PyTorch项目-git源码整理
- 形态学处理