Replace all links' names with other names
Author
Zhou Renjian
Create@
2004-12-02 11:18
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
/**
* @author Janyckee Jozz
*/
public class RenameAll {
public static void deleteFolder(String folderPath) {
File folder = new File(folderPath);
if (folder.exists() && folder.isDirectory()) {
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteFolder(files[i].getAbsolutePath());
} else {
files[i].delete();
}
}
folder.delete();
}
}
public static void copyFolder(String srcPath, String destPath, String camName) {
File srcFolder = new File(srcPath);
if (srcFolder.exists() && srcFolder.isDirectory()) {
File destFolder = new File(destPath.replaceAll("mofei", camName));
if (destFolder.exists() && destFolder.isDirectory()) {
return ;
} else {
destFolder.mkdir();
}
File[] files = srcFolder.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
copyFolder(files[i].getAbsolutePath(), new File(destFolder,
files[i].getName()).getAbsolutePath(), camName);
} else {
copyFile(files[i], new File(destFolder, files[i].getName()), camName);
}
}
}
}
public static String readContent(Reader reader) throws IOException {
char[] buf = new char[1024];
StringBuffer strBuffer = new StringBuffer();
int readLength = 0;
while (readLength != -1) {
readLength = reader.read(buf);
if (readLength != -1) {
strBuffer.append(buf, 0, readLength);
}
}
return strBuffer.toString();
}
public static String readFileAll(String filePath) {
try {
FileReader reader = new FileReader(filePath);
return readContent(reader);
} catch (IOException e) {
System.out.println("Error in reading " + filePath);
return "";
}
}
private static void copyFile(File srcFile, File destFile, String camName) {
String path = srcFile.getAbsolutePath().toLowerCase();
String newDestFileName = destFile.getAbsolutePath().replaceAll("mofei", camName);
if (path.endsWith(".png") || path.endsWith(".gif")
|| path.endsWith(".jpg") || path.endsWith(".jpeg")
|| path.endsWith(".db") || path.endsWith(".project")) {
try {
FileInputStream fi = new FileInputStream(srcFile);
FileOutputStream fo = new FileOutputStream(newDestFileName);
byte[] buf = new byte[1024];
int readLength = 0;
while (readLength != -1) {
readLength = fi.read(buf);
if (readLength != -1) {
fo.write(buf, 0, readLength);
}
}
fo.close();
fi.close();
} catch (Exception e) {
System.out.println("Error in copying " + srcFile.getAbsolutePath() + " to " + newDestFileName);
}
} else {
String content = readFileAll(srcFile.getAbsolutePath());
content = content.replaceAll("mofei", camName);
try {
FileWriter writer = new FileWriter(newDestFileName);
writer.write(content);
writer.close();
} catch (IOException e) {
System.out.println("Error in copying " + srcFile.getAbsolutePath() + " to " + newDestFileName);
}
}
}
public static void main(String[] args) {
deleteFolder("/home/zhourj/joz/eclipse/workspace/cam-rename-all/dist");
copyFolder("/home/zhourj/joz/eclipse/workspace/cam-news", "/home/zhourj/joz/eclipse/workspace/cam-rename-all/dist", "cam");
}
}