You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.8 KiB
98 lines
2.8 KiB
package com.insigma.utils;
|
|
|
|
import java.io.*;
|
|
|
|
/**
|
|
* (FileUtil 类)
|
|
*
|
|
* @author zhangxianwei
|
|
* @since 16:23 2022/4/18
|
|
*/
|
|
public class FileUtil {
|
|
|
|
public static void replaceLine(String path, String keyword, String replaceToStr) {
|
|
String temp;
|
|
try {
|
|
File file = new File(path);
|
|
FileInputStream fis = new FileInputStream(file);
|
|
InputStreamReader isr = new InputStreamReader(fis);
|
|
BufferedReader br = new BufferedReader(isr);
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
// 保存该行前面的内容
|
|
while ((temp = br.readLine()) != null) {
|
|
if (temp.trim().startsWith(keyword)) {
|
|
buf = buf.append(replaceToStr);
|
|
} else {
|
|
buf = buf.append(temp);
|
|
}
|
|
buf = buf.append(System.getProperty("line.separator"));
|
|
}
|
|
|
|
br.close();
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
PrintWriter pw = new PrintWriter(fos);
|
|
pw.write(buf.toString().toCharArray());
|
|
pw.flush();
|
|
pw.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 按照路径创建目录
|
|
* @param path 路径名称
|
|
* @return
|
|
*/
|
|
public static boolean mkDirectory(String path) {
|
|
boolean bool;
|
|
try {
|
|
File file = new File(path);
|
|
if (!file.exists()) {
|
|
bool = file.mkdirs();
|
|
} else {
|
|
bool = false;
|
|
}
|
|
} catch (Exception e) {
|
|
bool = false;
|
|
}
|
|
return bool;
|
|
}
|
|
|
|
/**
|
|
* 删除整个文件夹里的内容
|
|
*
|
|
* @param path
|
|
* @throws Exception
|
|
*/
|
|
public static void delAllFile(String path) {
|
|
File file = new File(path);
|
|
if (!file.exists()) {
|
|
return;
|
|
}
|
|
if (!file.isDirectory()) {
|
|
return;
|
|
}
|
|
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 + File.separatorChar + tempList[i]);
|
|
// 再删除空文件夹
|
|
File folderPath = new File(path + File.separatorChar + tempList[i]);
|
|
folderPath.delete();
|
|
}
|
|
}
|
|
file.delete(); //删除自身文件夹
|
|
}
|
|
}
|
|
|