|
|
@ -1,383 +0,0 @@
|
|
|
|
package com.base;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
import java.io.RandomAccessFile;
|
|
|
|
|
|
|
|
import java.nio.channels.FileLock;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.filechooser.FileSystemView;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @描述 文件操作
|
|
|
|
|
|
|
|
* @author chen
|
|
|
|
|
|
|
|
* @date 2016年8月8日
|
|
|
|
|
|
|
|
* @package com.utils
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class FileOperate {
|
|
|
|
|
|
|
|
private final static String localDiskName = "本地磁盘";
|
|
|
|
|
|
|
|
private final static String enlocalDiskName = "Local Disk";
|
|
|
|
|
|
|
|
private final static String CD = "CD";
|
|
|
|
|
|
|
|
private final static String DVD = "DVD";
|
|
|
|
|
|
|
|
public static String usbPath = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 获取全部的外部移动硬盘路径
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static List<String> filterDiskPath() {
|
|
|
|
|
|
|
|
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
|
|
|
|
|
|
|
|
List<String> diskPathName = new ArrayList<String>();
|
|
|
|
|
|
|
|
File[] roots = File.listRoots();
|
|
|
|
|
|
|
|
for (File file : roots) {
|
|
|
|
|
|
|
|
String diskName = fileSystemView.getSystemTypeDescription(file);// 获取磁盘的类型描述信息
|
|
|
|
|
|
|
|
if (diskName.startsWith(localDiskName) || diskName.startsWith(enlocalDiskName) || diskName.contains(CD)
|
|
|
|
|
|
|
|
|| diskName.contains(CD) || diskName.contains(DVD)) // 当磁盘为可移动磁盘时{
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
diskPathName.add(file.getAbsolutePath());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return diskPathName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 创建文件夹
|
|
|
|
|
|
|
|
* @param path
|
|
|
|
|
|
|
|
* 上级目录
|
|
|
|
|
|
|
|
* @param dirName
|
|
|
|
|
|
|
|
* 待创建的目录
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static int createDir(String path, String dirName) {
|
|
|
|
|
|
|
|
File dirFile = new File(path + "\\" + dirName);
|
|
|
|
|
|
|
|
if (!dirFile.exists()) {
|
|
|
|
|
|
|
|
if (!dirFile.mkdirs()) {
|
|
|
|
|
|
|
|
System.out.println("目录不存在,创建失败!");
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 检测数据文件是否完整
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param path
|
|
|
|
|
|
|
|
* 文件绝对路径
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean isFileExists(String path) {
|
|
|
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
|
|
|
if (file.exists())
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 文件是否存在
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param name
|
|
|
|
|
|
|
|
* 文件绝对路径
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean file_dir_exist(String name) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
File file = new File(name);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
|
|
// TODO: handle exception
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 随机生成26以内的数字拼接的字符串(?什么用?)
|
|
|
|
|
|
|
|
* @param length
|
|
|
|
|
|
|
|
* 拼接次数
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String getRandomString(int length) { // length表示生成字符串的长度
|
|
|
|
|
|
|
|
String base = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
|
|
|
int number = random.nextInt(base.length());
|
|
|
|
|
|
|
|
sb.append(base.charAt(number));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 以字符串方式读取文件内容
|
|
|
|
|
|
|
|
* @param fiel_name
|
|
|
|
|
|
|
|
* 文件名
|
|
|
|
|
|
|
|
* @return 字符串
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String read_fileToStr(String fiel_name) {
|
|
|
|
|
|
|
|
String str = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
File file = new File(fiel_name);
|
|
|
|
|
|
|
|
String lineTxt = "";
|
|
|
|
|
|
|
|
InputStreamReader read = new InputStreamReader(new FileInputStream(file), "gbk");// 考虑到编码格式
|
|
|
|
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(read);
|
|
|
|
|
|
|
|
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
|
|
|
|
|
|
str = str + lineTxt;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferedReader.close();
|
|
|
|
|
|
|
|
read.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return "Exception";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 读取文件路径的各级目录,以数组形式返回
|
|
|
|
|
|
|
|
* @param path_filename
|
|
|
|
|
|
|
|
* 文件或文件夹路径
|
|
|
|
|
|
|
|
* @return 数组
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String get_filename_from_path(String path_filename) {
|
|
|
|
|
|
|
|
String[] strings = path_filename.split("\\\\");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return strings[strings.length - 1];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 查找dirPath目录下的所有文件
|
|
|
|
|
|
|
|
* @param dirPath
|
|
|
|
|
|
|
|
* 目录
|
|
|
|
|
|
|
|
* @return 所有文件数组
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static List<String> findAllFilenameFromPath(String dirPath) {
|
|
|
|
|
|
|
|
ArrayList<String> fileAbsolutePaths = new ArrayList<>();
|
|
|
|
|
|
|
|
FileOperate.findFiles(dirPath, fileAbsolutePaths);
|
|
|
|
|
|
|
|
return fileAbsolutePaths;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 删除目录下所有文件包含文件夹
|
|
|
|
|
|
|
|
* @param dirPath
|
|
|
|
|
|
|
|
* 目录
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean delAllFileByDir(String dirPath) {
|
|
|
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
|
|
|
File file = new File(dirPath);
|
|
|
|
|
|
|
|
// 判断目录或文件是否存在
|
|
|
|
|
|
|
|
if (!file.exists()) { // 不存在返回 false
|
|
|
|
|
|
|
|
return flag;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
// 判断是否为文件
|
|
|
|
|
|
|
|
if (file.isFile()) { // 为文件时调用删除文件方法
|
|
|
|
|
|
|
|
return deleteFile(dirPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else { // 为目录时调用删除目录方法
|
|
|
|
|
|
|
|
return deleteDirectory(dirPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 写文件
|
|
|
|
|
|
|
|
* @param content
|
|
|
|
|
|
|
|
* 文件内容
|
|
|
|
|
|
|
|
* @param filePathName
|
|
|
|
|
|
|
|
* 文件绝对路径
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void writeFile(List<Object> content, String filePathName) {
|
|
|
|
|
|
|
|
if (null == content) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
FileWriter writer = new FileWriter(filePathName);
|
|
|
|
|
|
|
|
for (Object one : content) {
|
|
|
|
|
|
|
|
writer.write(one.toString());
|
|
|
|
|
|
|
|
writer.write("\r\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
|
|
// TODO: handle exception
|
|
|
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 追加写入写文件
|
|
|
|
|
|
|
|
* @param content
|
|
|
|
|
|
|
|
* @param filePathName
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void appendWriteFile(String content, String filePathName){
|
|
|
|
|
|
|
|
File file = new File(filePathName);
|
|
|
|
|
|
|
|
if (!file.exists() || !file.isFile()) {
|
|
|
|
|
|
|
|
ArrayList<Object> list = new ArrayList<>();
|
|
|
|
|
|
|
|
list.add("系统异常日志\n");
|
|
|
|
|
|
|
|
writeFile(list, filePathName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 打开一个随机访问文件流,按读写方式
|
|
|
|
|
|
|
|
RandomAccessFile randomFile = new RandomAccessFile(filePathName, "rw");
|
|
|
|
|
|
|
|
// 文件长度,字节数
|
|
|
|
|
|
|
|
long fileLength = randomFile.length();
|
|
|
|
|
|
|
|
// 将写文件指针移到文件尾。
|
|
|
|
|
|
|
|
randomFile.seek(fileLength);
|
|
|
|
|
|
|
|
// randomFile.writeBytes(content);
|
|
|
|
|
|
|
|
//解决中文乱码
|
|
|
|
|
|
|
|
randomFile.write(content.getBytes());
|
|
|
|
|
|
|
|
randomFile.close();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 锁定文件
|
|
|
|
|
|
|
|
* @param fileName
|
|
|
|
|
|
|
|
* 文件绝对路径
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean islockFile(String fileName) {
|
|
|
|
|
|
|
|
Object result = null;
|
|
|
|
|
|
|
|
boolean isSuccess = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = tryLock(fileName);
|
|
|
|
|
|
|
|
if (null == result) {
|
|
|
|
|
|
|
|
isSuccess = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
isSuccess = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return isSuccess;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 删文件夹
|
|
|
|
|
|
|
|
* @param dirPath
|
|
|
|
|
|
|
|
* 文件夹路径
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static boolean deleteDirectory(String dirPath) {
|
|
|
|
|
|
|
|
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
|
|
|
|
|
|
|
|
if (!dirPath.endsWith(File.separator)) {
|
|
|
|
|
|
|
|
dirPath = dirPath + File.separator;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
File dirFile = new File(dirPath);
|
|
|
|
|
|
|
|
// 如果dir对应的文件不存在,或者不是一个目录,则退出
|
|
|
|
|
|
|
|
if (!dirFile.exists() || !dirFile.isDirectory()) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean flag = true;
|
|
|
|
|
|
|
|
// 删除文件夹下的所有文件(包括子目录)
|
|
|
|
|
|
|
|
File[] files = dirFile.listFiles();
|
|
|
|
|
|
|
|
for (int i = 0; i < files.length; i++) {
|
|
|
|
|
|
|
|
// 删除子文件
|
|
|
|
|
|
|
|
if (files[i].isFile()) {
|
|
|
|
|
|
|
|
flag = deleteFile(files[i].getAbsolutePath());
|
|
|
|
|
|
|
|
if (!flag)
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} // 删除子目录
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
flag = deleteDirectory(files[i].getAbsolutePath());
|
|
|
|
|
|
|
|
if (!flag)
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flag)
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// 删除当前目录
|
|
|
|
|
|
|
|
if (dirFile.delete()) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 删除文件
|
|
|
|
|
|
|
|
* @param filePath
|
|
|
|
|
|
|
|
* 文件路径
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static boolean deleteFile(String filePath) {
|
|
|
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
|
|
|
File file = new File(filePath);
|
|
|
|
|
|
|
|
// 路径为文件且不为空则进行删除
|
|
|
|
|
|
|
|
if (file.isFile() && file.exists()) {
|
|
|
|
|
|
|
|
file.delete();
|
|
|
|
|
|
|
|
flag = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return flag;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 查找dirPath目录下的所有文件
|
|
|
|
|
|
|
|
* @param dirPath
|
|
|
|
|
|
|
|
* 目录
|
|
|
|
|
|
|
|
* @param fileAbsolutePaths
|
|
|
|
|
|
|
|
* 所有文件数组
|
|
|
|
|
|
|
|
* @return 所有文件数组
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static List<String> findFiles(String dirPath, List<String> fileAbsolutePaths) {
|
|
|
|
|
|
|
|
File file = new File(dirPath);
|
|
|
|
|
|
|
|
// 是文件时
|
|
|
|
|
|
|
|
if (!file.isDirectory()) {
|
|
|
|
|
|
|
|
String path = file.getAbsolutePath();
|
|
|
|
|
|
|
|
fileAbsolutePaths.add(path);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 是文件夹时
|
|
|
|
|
|
|
|
else if (file.isDirectory()) {
|
|
|
|
|
|
|
|
String[] filelist = file.list();
|
|
|
|
|
|
|
|
if (null != filelist) {
|
|
|
|
|
|
|
|
int fileNum = filelist.length;
|
|
|
|
|
|
|
|
for (int i = 0; i < fileNum; i++) {
|
|
|
|
|
|
|
|
String tempPath = dirPath + "\\" + filelist[i];
|
|
|
|
|
|
|
|
findFiles(tempPath, fileAbsolutePaths);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileAbsolutePaths;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能 锁定文件
|
|
|
|
|
|
|
|
* @param fileName 文件绝对路径
|
|
|
|
|
|
|
|
* @return 锁
|
|
|
|
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@SuppressWarnings("resource")
|
|
|
|
|
|
|
|
private static FileLock tryLock(String fileName) throws IOException {
|
|
|
|
|
|
|
|
File lockF = new File(fileName); // 要锁的文件
|
|
|
|
|
|
|
|
lockF.deleteOnExit(); // 指定在退出时释放锁
|
|
|
|
|
|
|
|
RandomAccessFile file = new RandomAccessFile(lockF, "rws"); // 指定要锁的文件
|
|
|
|
|
|
|
|
FileLock res = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
res = file.getChannel().tryLock(); // 试图取得文件的锁
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) { // 文件被其它线程锁时抛出此异常
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|