要求將html页面中所有连接到png文件的文件名中的空格去掉,换成下划线“_”,是不是java语言都无所谓,谢谢

解决方案 »

  1.   

    不是一行字符串,是处理一个文件里面的所有内容,比如说文件中有一句话:
    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;
        }
    }
      

  2.   

    用正则,打开一个文件就替换一下,外部循环文件列表。不知道你文件中的数据有没有什么规律,不然还要判断png图片名的空格前的数据是路径还是其他东西。
      

  3.   


    我正则不好,所以就用了比较笨的字符串替换,文件基本上没什么规律,就是吧png文件名中的空格换掉,路径中没有空格,并不是所有png文件名都有空格的