java实现文件的创建
2011-05-09 15:24
357 查看
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressWarnings("unused")
public class createFile {
/**
* @param args
*/
// 创建目标文件
@SuppressWarnings("static-access")
public static boolean createFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
System.out.println("目标文件已经存在" + filePath);
System.err.println("日志信息:目标文件已经存在"+filePath+"日志日期:"+new SimpleDateFormat("yyyy/MM/dd:HH:mm:ss").format(new Date()));
return false;
}
// 判断文件是否为目录
if (filePath.endsWith(file.separator)) {
System.out.println("目标文件不能为目录!");
}
if (!file.getParentFile().exists()) {// 判断目标文件所在的目录是否存在
System.out.println("目标文件所在的目录不存在,准备创建!");
if (!file.getParentFile().mkdirs()) {// 判断创建目录是否成功
System.out.println("创建目标文件所在的目录失败!");
return false;
}
}
try {
// 创建文件
if (file.createNewFile()) {
System.out.println("创建文件成功:" + filePath);
return true;
} else {
System.out.println("创建文件失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("创建文件失败!" + e.getMessage());
return false;
}
}
// 创建目录
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
System.out.println("创建目录失败!" + dir.getPath());
return false;
}
if (destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
if (dir.mkdirs()) {
System.out.println("创建目录成功!" + destDirName);
return true;
} else {
System.out.println("创建目录失败!");
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 (Exception e) {
System.out.println("创建临时文件失败:"+e.getMessage());
return null;
}
}else{
File dir=new File(dirName);
if (!dir.exists()) {
if (createFile.createDir(dirName)) {
System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");
return null;
}
}
try {
tempFile=File.createTempFile(prefix, suffix,dir);
return tempFile.getCanonicalPath();
} catch (Exception e) {
e.getStackTrace();
System.out.println("创建临时文件失败!"+e.getMessage());
return null;
}
}
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
createFile create = new createFile();
create.createFile("d://create//temp.txt");
create.createDir("d://create2//");
String prefix="temp";
String suffix=".txt";
for (int i = 0; i < 10; i++) {
System.out.println("您创建的临时文件为:"+create.createTempFile(prefix, suffix, "d://create2//"));
}
}
}
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressWarnings("unused")
public class createFile {
/**
* @param args
*/
// 创建目标文件
@SuppressWarnings("static-access")
public static boolean createFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
System.out.println("目标文件已经存在" + filePath);
System.err.println("日志信息:目标文件已经存在"+filePath+"日志日期:"+new SimpleDateFormat("yyyy/MM/dd:HH:mm:ss").format(new Date()));
return false;
}
// 判断文件是否为目录
if (filePath.endsWith(file.separator)) {
System.out.println("目标文件不能为目录!");
}
if (!file.getParentFile().exists()) {// 判断目标文件所在的目录是否存在
System.out.println("目标文件所在的目录不存在,准备创建!");
if (!file.getParentFile().mkdirs()) {// 判断创建目录是否成功
System.out.println("创建目标文件所在的目录失败!");
return false;
}
}
try {
// 创建文件
if (file.createNewFile()) {
System.out.println("创建文件成功:" + filePath);
return true;
} else {
System.out.println("创建文件失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("创建文件失败!" + e.getMessage());
return false;
}
}
// 创建目录
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
System.out.println("创建目录失败!" + dir.getPath());
return false;
}
if (destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
if (dir.mkdirs()) {
System.out.println("创建目录成功!" + destDirName);
return true;
} else {
System.out.println("创建目录失败!");
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 (Exception e) {
System.out.println("创建临时文件失败:"+e.getMessage());
return null;
}
}else{
File dir=new File(dirName);
if (!dir.exists()) {
if (createFile.createDir(dirName)) {
System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");
return null;
}
}
try {
tempFile=File.createTempFile(prefix, suffix,dir);
return tempFile.getCanonicalPath();
} catch (Exception e) {
e.getStackTrace();
System.out.println("创建临时文件失败!"+e.getMessage());
return null;
}
}
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
createFile create = new createFile();
create.createFile("d://create//temp.txt");
create.createDir("d://create2//");
String prefix="temp";
String suffix=".txt";
for (int i = 0; i < 10; i++) {
System.out.println("您创建的临时文件为:"+create.createTempFile(prefix, suffix, "d://create2//"));
}
}
}
相关文章推荐
- Web项目中使用java Struts2实现Zip、xml文件的动态创建和下载(Zip文件中动态存在多个xml文件)
- Java实现创建目录和文件
- java创建一个类实现读取一个文件中的每一行显示出来
- JAVA基础初探(十二)Map接口及其常用实现类(HashMap)、File类详解(概述、创建、删除、重命名、文件属性读取/设置、遍历文件夹)
- 用java实现自动创建文件及文件夹
- java实现对hdfs文件系统的上传,下载,删除,创建文件夹的操作演示
- Java Struts2 POI创建Excel文件并实现文件下载
- Java笔记之io——递归创建与删除多层文件夹,字符流实现文件复制
- Java Struts2 POI创建Excel文件并实现文件下载
- 怎样用Java实现创建文件并把文件复制到特定系统盘里
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- java后台创建压缩包&使用浏览器下载文件(批量下载实现)
- java创建SFTP通道实现文件的上传与下载
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- 【java常见问题】——如何实现文件的创建?
- java简单实现文件的创建 读写
- JAVA实现单个文件,目录以及临时文件的创建工具类
- java实现文件路径的创建
- JAVA---实现文件路径的创建
- java 实现hadoop的hdfs文件的上传下载删除创建