62,614
社区成员
发帖
与我相关
我的任务
分享
package file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ChangePNGLinkName {
private static final String absoluteBufferPath = "/extends/changename/buffer.html";
public static void main(String args[]) {
System.out.println("start***************");
ChangePNGLinkName m = new ChangePNGLinkName();
try {
m.changePNGLinkName("/extends/changename/data/");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("over***************");
}
private void mkBufferFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(
absoluteBufferPath));
String in;
while ((in = br.readLine()) != null) {
if (in.contains(".png")) {
in = doString(in);
}
bw.write(in);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
private String doString(String source) {
int indexPNG = source.indexOf(".png");
String lastString = source.substring(indexPNG+4);
String pngName = source.substring(source.substring(0, indexPNG)
.lastIndexOf("/"), indexPNG);
if (pngName.contains(" ")) {
pngName = replace(pngName, " ", "_");
pngName = pngName + ".png";
if (lastString.contains(".png")){
lastString = doString(lastString);
}
source = source.substring(0, source.substring(0, indexPNG)
.lastIndexOf("/"))
+ pngName + lastString;
}else{
if (lastString.contains(".png")){
lastString = doString(lastString);
source = source.substring(0,indexPNG+4) + lastString;
}
}
return source;
}
private void changePNGLinkName(String path) throws IOException {
File root = new File(path);
if (root == null)
return;
String[] l = root.list();
if (l == null || l.length == 0)
return;
File[] f = root.listFiles();
for (int i = 0; i < l.length; i++) {
if (!f[i].isFile()) {
if (l[i] == "image")
continue;
changePNGLinkName(path + "/" + l[i]);
} else {
if (l[i].contains(".html")) {
if (l[i].equals("chapter01.html") || l[i].equals("chapter02.html") || l[i].equals("chapter03.html") || l[i].equals("chapter04.html"))
{
changeSingleFile(f[i].getAbsolutePath());
}
}
}
}
}
private void changeSingleFile(String path) throws IOException {
mkBufferFile(path);
File f = new File(path);
File newF = new File(absoluteBufferPath);
f.delete();
newF.renameTo(f);
}
private String replace(String strSource, String strFrom, String strTo) {
if (strSource == null) {
return null;
}
int i = 0;
if ((i = strSource.indexOf(strFrom, i)) >= 0) {
char[] cSrc = strSource.toCharArray();
char[] cTo = strTo.toCharArray();
int len = strFrom.length();
StringBuffer buf = new StringBuffer(cSrc.length);
buf.append(cSrc, 0, i).append(cTo);
i += len;
int j = i;
while ((i = strSource.indexOf(strFrom, i)) > 0) {
buf.append(cSrc, j, i - j).append(cTo);
i += len;
j = i;
}
buf.append(cSrc, j, cSrc.length - j);
return buf.toString();
}
return strSource;
}
}