用java遍历修改文件?求轻快好代码。在c:/path/目录下有n个文件,现在我要遍历其中 所有 文件名以.xml为后缀的xml文件,把xml文件内末尾的回车符号(如果有回车符的话)删除。 例如有文件如下:a.xml 
-------------------------------------- 
<?xml version="1.0" encoding="GBK"?> 
<LordData> 
<LordDataHeader> 
<MessageType>God </MessageType> 
</LordDataHeader> 
<LordDataBody> 
<DATA> 
<UserArea> 
<![CDATA[ 
<List> <Row> 
<name>Tom </name> 
<age>11 </age> 
</Row> 
</List>]]> 
</UserArea> 
</DATA> 
</LordDataBody> 
</LordData> -------------------------------------- 那么, 
删除换行符号后如下: 
-------------------------------------- 
<?xml version="1.0" encoding="GBK"?> 
<LordData> 
<LordDataHeader> 
<MessageType>God </MessageType> 
</LordDataHeader> 
<LordDataBody> 
<DATA> 
<UserArea> 
<![CDATA[ 
<List> <Row> 
<name>Tom </name> 
<age>11 </age> 
</Row> 
</List>]]> 
</UserArea> 
</DATA> 
</LordDataBody> 
</LordData> 
-------------------------------------- 
求高手给藕一份代码哦。

解决方案 »

  1.   

    哦,</LordData> 这里原来有个回车的,只是看不见,你可以看到下一行↓是空行啊。
                       
    -------------------------------------- 
      

  2.   

    1:取所有的.xml文件可以使用filter过滤,这样比较方便。
    2:删除最后的回车符号,可以使用BufferedReader读取,把最后的回车忽略掉就可以了。
      

  3.   

    1 遍历代码
    2 删除尾部空行代码
    第一个可以去我的网站搜索 遍历, 有现成的代码
    第二个可以读取文件到字符串,然后trim,去掉前后空白,再保存回去!
      

  4.   

    import java.io.*;
    public class Test{
        public static void main (String[] args) {
         changeXML("d:/test/");    
       }
          
        static void changeXML(String path){
            File pa=new File(path);
            if(!pa.exists()){
                System.out.println("路径:"+path+" 不存在");
                return;
            }else{
                if(!pa.isDirectory()){
                    System.out.println(path+"不是目录,请用正确的路径.");
                    return;
                }
            }
            String[] fileName =pa.list(new FilenameFilter(){
                public boolean accept(File  dir, String name){
                 return name.matches(".+\\.[x|X][m|M][l|L]");            
                }
            });
            for(String fName:fileName){
             try{
             BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path+fName)));
             StringBuilder sb=new StringBuilder();
             String line=null;
             while((line=br.readLine())!=null){
             if((line.trim()).length()!=0)
             sb.append(line.trim()+"\n");
             }
             br.close();
             BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+fName)));
             bw.write(sb.toString());
             bw.close();
             }catch(Exception e){
             e.printStackTrace();
             }
            }
        }
    }用RadomAccessFile不成功,只好用5楼的办法了.过中间的空行也给去掉了,楼主看行不行.
      

  5.   

    1.遍历没啥花样,用FileFilter
    2.如果要速度快,首推 Java NIO 里操作本地文件的内存映射类 MappedByteBuffer
      

  6.   

    import java.io.*;
    public class Test{
        public static void main (String[] args) {
         changeXML("d:/test/");    
       }
          
        static void changeXML(String path){
            File pa=new File(path);
            if(!pa.exists()){
                System.out.println("路径:"+path+" 不存在");
                return;
            }else{
                if(!pa.isDirectory()){
                    System.out.println(path+"不是目录,请用正确的路径.");
                    return;
                }
            }
            String[] fileName =pa.list(new FilenameFilter(){
                public boolean accept(File  dir, String name){
                 return name.matches(".+\\.[x|X][m|M][l|L]");            
                }
            });
            for(String fName:fileName){
             System.out.println(fName);
             try{
             BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path+fName)));
             StringBuilder sb=new StringBuilder();
             String line=null;
             while((line=br.readLine())!=null){
             if((line.trim()).length()!=0)
                  // http://topic.csdn.net/u/20090608/22/b4cbf61a-3b75-4350-be68-a65db006f361.html?74104
                  //根据上面的贴子,行尾用"\r\n"
             sb.append(line.trim()+"\r\n"); 
             }
             br.close();
             BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+fName)));
             //sb.delete(sb.length()-2,sb.length());  //这句的功能是把最后的换行去掉。看你的意思了。
             bw.write(sb.toString());
             bw.close();
             }catch(Exception e){
             e.printStackTrace();
             }
            }
        }
    }
    加注释的地方稍改了一点点。
      

  7.   


    用RadomAccessFile应该可以直接改原文件,原来想数一个文件中最后的空白字符数,然后把文件的长度改短,这样效率比较高,但没有实现.
      

  8.   

    大虫子,你的代码有了xml文件里若是有了中文运行后 就会变成乱码啊,怎么办?你用xml编辑器编写一个带中文节点的xml文件测一下试试看:
      

  9.   

               BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+fName),"UTF-8"));//改这句.
      

  10.   

    import java.io.*;
    public class XMLTailHandler{
        public static void main (String[] args) {
         deleteWhiteChar2("c:/test/");    
       }
          
        static void deleteWhiteChar(String path){          //用了BufferedReader读和BufferedWriter写,效率不高.会对文件内容有影响。
         File pa=new File(path);
            if(!checkPath(pa)){
             return;
            }
            String[] fileName =pa.list(new FilenameFilter(){
                public boolean accept(File  dir, String name){
                 return name.matches(".+\\.[x|X][m|M][l|L]");            
                }
            });
            for(String fName:fileName){
             System.out.println(fName);
             try{
             BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path+fName)));
             StringBuilder sb=new StringBuilder();
             String line=null;
             while((line=br.readLine())!=null){
             if((line.trim()).length()!=0)
                  // http://topic.csdn.net/u/20090608/22/b4cbf61a-3b75-4350-be68-a65db006f361.html?74104
                  //根据上面的贴,行尾用"\r\n"
             sb.append(line.trim()+"\r\n"); 
             }
             br.close();
             BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+fName),"UTF-8"));
             //sb.delete(sb.length()-2,sb.length());  //这句的功能是把最后的换行去掉。看你的意思了。
             bw.write(sb.toString());
             bw.close();
             }catch(Exception e){
             e.printStackTrace();
             }
            }
        }
        static void deleteWhiteChar2(String path){      //用RandomAccessFile,只对文件尾部修改,不会改文件内容.效率也比较高。
         File pa=new File(path);
         if(!checkPath(pa)){
             return;
            }
            String[] fileName =pa.list(new FilenameFilter(){
                public boolean accept(File  dir, String name){
                 return name.matches(".+\\.[x|X][m|M][l|L]");            
                }
            });
             for(String fName:fileName){
             System.out.println(fName);
             try{
             RandomAccessFile raf=new RandomAccessFile(path+fName,"rw");
             long pos=raf.length()-1;
             raf.seek(pos);
             int ch=raf.read();
             while(Character.isWhitespace(ch)) {
             pos--;
             raf.seek(pos);
             ch=raf.read();
             }
             raf.setLength(pos+1);
             raf.close();
             }catch(Exception e){
             e.printStackTrace();
             }
            }
        }
        static boolean checkPath(File pa){
            if(!pa.exists()){
                System.out.println("路径:"+pa.getPath()+" 不存在");
                return false;
            }else{
                if(!pa.isDirectory()){
                    System.out.println(pa.getPath()+"不是目录,请用正确的路径.");
                    return false;
                }
            }
            return true;
        }
    }
      

  11.   

    bigbug搞出许多littlebug来,不好意思