修改项目文件夹名称
import java.io.File;
import java.io.IOException;
public class ModifyFileName {
static String OLD_PROJECT_NAME = "gmis";
static String NEW_PROJECT_NAME = "gsnm";
static String dir = "D:\\beifen1\\gsnm-file";
public static void main(String[] args) throws IOException {
recursiveTraversalFolder(dir);
}
public static void recursiveTraversalFolder(String path) {
File folder = new File(path);
if (folder.exists()) {
File[] fileArr = folder.listFiles();
if (fileArr == null || fileArr.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
File newDir = null;
String newName = "";
String fileName = null;
File parentPath = new File("");
for (File file : fileArr) {
if (file.isDirectory()) {
fileName = file.getName();
if (fileName.contains(OLD_PROJECT_NAME)) {
newName = file.getAbsolutePath().replaceAll(OLD_PROJECT_NAME, NEW_PROJECT_NAME);
File newFile = new File(newName);
file.renameTo(newFile);
System.out.println("文件夹修改前:" + file.getAbsolutePath() + ",修改后" + newName);
recursiveTraversalFolder(newFile.getAbsolutePath());
}
recursiveTraversalFolder(file.getAbsolutePath());
} else {
fileName = file.getName();
parentPath = file.getParentFile();
if (fileName.contains(OLD_PROJECT_NAME)) {
newName = fileName.replaceAll(OLD_PROJECT_NAME, NEW_PROJECT_NAME);
newDir = new File(parentPath + "/" + newName);
file.renameTo(newDir);
System.out.println("文件修改前:" + fileName + "修改后:" + newDir);
}
}
}
}
} else {
System.out.println("目录不存在=" + path);
}
}
}
修改文件夹下文件里面的内容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
public class ModifyContent {
static String dir = "D:\\beifen1\\gsnm-file";
static String newStr = "gsnm";
static String oldStr = "gmis";
public static void main(String[] args) {
File f = new File(dir);
print(f, 0, "D:\\beifen1", "D:\\beifen2");
}
public static void print(File f, int len, String sourceDisk, String targetDisk) {
File[] file = f.listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory()) {
print(file[i], len + 1, sourceDisk, targetDisk);
}
File outPath = new File(file[i].getParent().replace(sourceDisk, targetDisk));
File readfile = new File(file[i].getAbsolutePath());
if (!readfile.isDirectory()) {
String filename = readfile.getName();
String absolutepath = readfile.getAbsolutePath();
if (absolutepath.endsWith(".png") ||
absolutepath.endsWith(".ico") ||
absolutepath.endsWith(".jpg") ||
absolutepath.endsWith(".dll") ||
absolutepath.endsWith(".exe") ||
absolutepath.endsWith(".jar") ||
absolutepath.endsWith(".zip") ||
absolutepath.endsWith(".rdb") ||
absolutepath.endsWith(".docx") ||
absolutepath.endsWith(".pdb")
) {
try {
copyFileUsingFileChannels(absolutepath, filename, i, outPath);
} catch (IOException e) {
e.printStackTrace();
}
} else {
readFile(absolutepath, filename, i, outPath);
}
}
}
}
public static void readFile(String absolutepath, String filename,
int index, File outPath) {
try {
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
new FileInputStream(absolutepath), "utf8"));
StringBuffer strBuffer = new StringBuffer();
int line = 0;
for (String temp = null; (temp = bufReader.readLine()) != null;
temp = null) {
line++;
if ((temp.indexOf(oldStr) != -1) &&
(temp.indexOf(oldStr) != -1)) {
temp = temp.replace(oldStr, newStr);
System.out.println(absolutepath + "\t\t\t\t\t\t\t\t\t\t\t" + "第" + line + "行" + "替换" + oldStr + "为" + newStr);
}
strBuffer.append(temp);
strBuffer.append(System.getProperty("line.separator"));
}
bufReader.close();
if (outPath.exists() == false) {
outPath.mkdirs();
}
PrintWriter printWriter = new PrintWriter(outPath + File.separator + filename, "utf8");
printWriter.write(strBuffer.toString().toCharArray());
printWriter.flush();
printWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFileUsingFileChannels(String absolutepath, String filename,
int index, File outPath) throws IOException {
if (outPath.exists() == false) {
outPath.mkdirs();
}
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(absolutepath).getChannel();
outputChannel = new FileOutputStream(outPath.getAbsolutePath() + File.separator + filename).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
}