求一段文件处理代码 要求將html页面中所有连接到png文件的文件名中的空格去掉,换成下划线“_”,是不是java语言都无所谓,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不是一行字符串,是处理一个文件里面的所有内容,比如说文件中有一句话:asdfasdf adf;lkh;asd abc/ad/cz adf.png;;asdfknasdf/a/b/c/d/adasd an.pngiaher;要得就是將cz adf.png,adasd an.png转变成cz_adf.png,adasd_an.png.而且文件中不止一句这样的话,都要处理,大约有几百个这样的文件吧。当时比较急,现在我已经能java解决了,就看看大家有什么比较好的方法了。我的代码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; }} 用正则,打开一个文件就替换一下,外部循环文件列表。不知道你文件中的数据有没有什么规律,不然还要判断png图片名的空格前的数据是路径还是其他东西。 我正则不好,所以就用了比较笨的字符串替换,文件基本上没什么规律,就是吧png文件名中的空格换掉,路径中没有空格,并不是所有png文件名都有空格的 JFileChooser能不能记住上次访问的路径 用for循环写出一个字符串+数字的字符串,怎么做可以实现? switch case中的变量赋值问题 关于URLConnection RequestProperty的问题 请教类的对象提取session数据的问题 谁能帮我看看(《java编程思想》里的例子) 多个版本的jre切换 Websphere3.5的一个BUG.希望有人反对.多谢.. tomcat + Apache SOAP 的问题! JAVA操作注册表 对输入输出流和文件操作实在有些弄不清楚。 一行语句太长了怎么办
asdfasdf adf;lkh;asd abc/ad/cz adf.png;;asdfknasdf/a/b/c/d/adasd an.pngiaher;
要得就是將cz adf.png,adasd an.png转变成cz_adf.png,adasd_an.png.
而且文件中不止一句这样的话,都要处理,大约有几百个这样的文件吧。
当时比较急,现在我已经能java解决了,就看看大家有什么比较好的方法了。我的代码
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;
}
}
我正则不好,所以就用了比较笨的字符串替换,文件基本上没什么规律,就是吧png文件名中的空格换掉,路径中没有空格,并不是所有png文件名都有空格的