创建文件、删除文件、创建目录、删除目录
该文件是完整的小程序,可以直接使用
<span style="font-size:18px;">package com.yangjf.service.impl;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
/*
* 向文件中写入数据
* */
public class WriterDataToFile {
static String fileUrl = "D:/upload.txt";
public static void setDate(final String conent, String url) {
if (url == null || "".equals(url)) {
CreateFile(fileUrl);
} else {
CreateFile(url);
}
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileUrl, true)));
out.write(conent + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
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;
}
}
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;
try {
if (dirName == null) {
// 在默认文件夹下创建临时文件
tempFile = File.createTempFile(prefix, suffix);
return tempFile.getCanonicalPath();
} else {
File dir = new File(dirName);
// 如果临时文件所在目录不存在,首先创建
if (!dir.exists()) {
if (!WriterDataToFile.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 boolean deleteDir(File dir) {
if(dir==null){
return true;
}
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
* 删除目录中的.svn目录
*
* @param f
*/
public static void delDotsvn(File f) {
File[] files = f.listFiles();
if (files != null)
for (File file : files)
if (".svn".equals(file.getName()))
del(file);
else
delDotsvn(file);
}
/**
* 删除文件或目录
*/
public static void del(File file) {
File[] files = file.listFiles();
if (files != null)
for (File f : files)
del(f);
file.delete();
}
//生成jar 包
public static void generateJar(String targetPath,String javaClassPath) throws FileNotFoundException, IOException {
System.out.println("*** --> 开始生成jar包...");
String targetDirPath = targetPath.substring(0, targetPath.lastIndexOf("/"));
File targetDir = new File(targetDirPath);
if (!targetDir.exists()) {
targetDir.mkdirs();
}
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream(targetPath), manifest);
writeClassFile(new File(javaClassPath), target,javaClassPath);
target.close();
System.out.println("*** --> jar包生成完毕。");
}
private static void writeClassFile(File source, JarOutputStream target,String javaClassPath) throws IOException {
BufferedInputStream in = null;
try {
if (source.isDirectory()) {
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty()) {
if (!name.endsWith("/")) {
name += "/";
}
name = name.substring(javaClassPath.length());
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile : source.listFiles())
writeClassFile(nestedFile, target,javaClassPath);
return;
}
String middleName = source.getPath().replace("\\", "/").substring(javaClassPath.length());
JarEntry entry = new JarEntry(middleName);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true) {
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
} finally {
if (in != null)
in.close();
}
}
public static void main(String[] args) {
//WriterDataToFile.CreateFile("E:\\upload\\dk_download\\20160914\\对公_LDPF_201609140049.xlsx");
//临时文件
//WriterDataToFile.deleteDir(new File("F:\\workSpaceTomorrowPower05\\quartz-05\\src\\main\\java\\net\\chrone\\tpsdk\\quartz\\util\\jdzfutil"));
}
}
</span>