File utils
花阳辉
2023-12-01
package com.test.common.util.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* 文件工具类,提供常见的文件、文件名操作
* @author Lonyel
*/
public class FileUtils {
/**
* 取得文件名称(不含路径信息)
*
* @param fileName
* @return 文件名称
*/
public static String extractFileName(String fileName) {
int pos = fileName.lastIndexOf('/');
String result = fileName;
if (pos > -1) {
result = fileName.substring(pos + 1);
}
pos = fileName.lastIndexOf('\\');
if (pos > -1) {
result = fileName.substring(pos + 1);
}
return result;
}
/**
* 取得文件后缀
*
* @param fileName
* @return 返回文件后缀
*/
public static String extractFileExt(String fileName) {
int pos = fileName.lastIndexOf('.');
if (pos > -1) {
return (fileName.substring(pos + 1));
} else
return ("");
}
/**
* 创建临时文件
*
* @param tempPath
* @param prefix
* @param suffix
* @return 返回临时文件文件的全名
*/
public static String getTempFile(String tempPath, String prefix,
String suffix) {
try {
if (prefix == null || "".equals(prefix.trim())) {
prefix = "tmp";
}
if (prefix.trim().length() < 3) {
char[] d = { '0' };
prefix = prefix
+ String.copyValueOf(d, 0, 3 - prefix.trim().length());
}
File tempf = File
.createTempFile(prefix, suffix, new File(tempPath));
return (tempf.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
/**
* 创建临时目录
*
* @param tempPath
* @param prefix
* @param suffix
* @return
*/
public static String getTempPath(String tempPath, String prefix,
String suffix) {
try {
if (prefix == null || "".equals(prefix.trim())) {
prefix = "tmp";
}
if (suffix == null) {
suffix = "";
}
Long i = 0L;
while (true) {
String p = tempPath + "/" + prefix + i.toString() + suffix;
File f = new File(p);
if (!f.exists()) {
f.mkdirs();
return f.getName();
}
i++;
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 根据文件获取上一级目录名
*/
public static String getParentFileName(File f) {
try {
return f.getParentFile().getName();
} catch (Exception e) {
return "";
}
}
/**
* 文件拷贝
* @param fromFile
* @param toFile
* @return
* @throws Exception
*/
public static boolean copyFile(String fromFile, String toFile)
throws Exception {
OutputStream os = null;
InputStream is = null;
try {
os = new FileOutputStream(toFile);
is = new FileInputStream(fromFile);
byte[] buf = new byte[4096];
while (true) {
int cnt = is.read(buf);
if (cnt > -1) {
os.write(buf, 0, cnt);
}
if (cnt < buf.length) {
break;
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
os.close();
is.close();
}
}
/**
* 获取不带文件类型的文件名
*/
public static String getFileNameWithoutType(String fileName) {
try {
int pos = fileName.lastIndexOf(".");
String fileNameWithoutType = fileName.substring(0, pos);
return fileNameWithoutType;
} catch (Exception e) {
return "";
}
}
/**
* 根据文件名获取文件类型(大写)
*/
public static String getFileTypeByFileNameWithUpperCase(String fileName) {
try {
int pos = fileName.lastIndexOf(".");
String fileType = fileName.substring(pos + 1);
return fileType.toUpperCase();
} catch (Exception e) {
return "";
}
}
/**
* 根据文件名获取文件类型
*/
public static String getFileTypeByFileName(String fileName) {
try {
int pos = fileName.lastIndexOf(".");
String fileType = fileName.substring(pos + 1);
return fileType;
} catch (Exception e) {
return "";
}
}
/**
* 删除目录或者文件
*
* @param dir
* @return
*/
public static boolean deleteFileOrDirectory(String fileOrDir) {
File file = new File(fileOrDir);
if (file.isFile()) {
return file.delete();
} else {
String[] fileNames = file.list();
if (fileNames != null) {
for (String fileName : fileNames) {
if (!deleteFileOrDirectory(file.getAbsolutePath() + "/"
+ fileName)) {
return false;
}
}
}
return file.delete();
}
}
/**
* 备份目录
*
* @param resdir 需备份的目录 exp:'c:/1/'
* @param todir 备份到的目录 exp:'c:/bak/'
*/
public static void bakFileDir(String resdir, String todir) {
File rdir = new File(resdir);
if (!rdir.isDirectory()) {
return;
}
todir = todir + rdir.getName() + "/";
File tdir = new File(todir);
if (!tdir.exists()) {
tdir.mkdirs();
}
File[] files = rdir.listFiles();
if (files.length > 0) {
String resfile = null;
String tofile = null;
for (File file : files) {
if (file.isFile()) {
resfile = file.getAbsolutePath();
tofile = todir + file.getName();
try {
copyFile(resfile, tofile);
} catch (Exception e) {
e.printStackTrace();
}
} else if (file.isDirectory()) {
bakFileDir(file.getPath(), todir);
}
}
}
}
public static void createInitCfg(String loginName,String licence,String Services,String fileName){
FileOutputStream fis = null;
PrintStream ps = null;
File expFile = new File(fileName);
try {
fis = new FileOutputStream(expFile);
ps = new PrintStream(fis);
ps.println("[WebService]");
ps.println("url=http://"+Services+"service/MyServices");
ps.println("userName="+loginName);
ps.println("password="+licence);
ps.flush();
}catch(Exception e){
e.printStackTrace();
}finally {
if (ps != null) {
ps.close();
}
if (fis != null) {
try {
fis.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
}
}
/**
* 删除文件夹
* @param folderPath
*/
@SuppressWarnings("unused")
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除指定文件夹下所有文件
* @param path 文件夹完整绝对路径
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
flag = true;
}
}
return flag;
}
/**
* 创建此抽象路径名指定的目录,包括创建必需但不存在的父目录
* @param path
*/
public static void createDirectory(String path){
if(path != null && !"".equals(path)){
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
}
}
}