帮你代码优化了一下:import java.util.*;
import java.io.*;public class ReadFileA1 {
  public ReadFileA1() {
  }  public static void main(String args[]){
    ReadFileA1 test = new ReadFileA1();
    String from = ".\\data\\from.txt";
    String to = ".\\data\\to.txt";
    test.writeFile( test.readFile(from) , to );
  }  private boolean writeFile(Vector vctValue, String to){
    try{
      PrintWriter out = new PrintWriter(new FileWriter( to ));
      String line;
      for (int i=0; i<vctValue.size() ; i++){
          line = (String)vctValue.get(i);
          System.out.println( line ) ;
          out.println(line);
      }
      out.flush();
      out.close();
    }catch(IOException e){
      e.printStackTrace();
      return false;
    }
    return true;
  }  private Vector readFile(String strInFile) {
    Vector vctInFile = new Vector() ;
    try {
      //这是建立了一个文件读取对象
      BufferedReader bfrdInFile = new BufferedReader(new FileReader(strInFile));
      String strLine = "";
      int intLoop = 0 ;
      while ((strLine=bfrdInFile.readLine()) != null) {
        strLine = strLine.trim() ;
        if(strLine.startsWith("<")||strLine.equals("")) {
          continue;
        }
        vctInFile.add( intLoop +" : " + strLine ) ;
        intLoop++;
      }
      bfrdInFile.close();
    }catch (Exception e) {
      e.printStackTrace() ;
    }
    return vctInFile;
  }}

解决方案 »

  1.   

    楼主,我只能简单给你介绍一下你朋友说的那个“dir > FILENAME.TXT”方法,
    这个是dos里的命令,完整形式好像是:dir *.txt>filename.txt,是把所有txt文件内容输入到
    filename.txt中,不知道我说的对不对,错了请见谅!!
      

  2.   

    对啊!例如说在C:\data 下面有N个都需要处理的文件,处理方法都是一样的,但是如果把每个文件都带进去的话工作量就太大了,现在是要把这N个文本文件都处理,并且生成新的文件,当然原文件还是要保留!不知道我说明白了没有!
      

  3.   

    /** 帮你写了一个,专门处理某个目录下的文件合并,文件类型自定义**/import java.util.*;
    import java.io.*;public class BatchFileProcess {  private Vector vct = new Vector(); // 保存内容  private String path = null; // 操作目录
      private String file = null; // 新生成文件的(路径和)文件名
      private String filterConditoin = null; //过滤条件
      private boolean append = false; //新生成文件内容是否为追加方式  public BatchFileProcess(){
      }  public void setPath(String path){
        this.path = path;
      }
      public void setFile(String file){
        this.file = file;
      }
      public void setFilterConditoin(String filterConditoin){
        this.filterConditoin = filterConditoin;
      }
      public void setAppend(boolean append){
        this.append = append;
      }  public static void main(String args[]){    if(args==null || args.length<3 || args[0].length()==0 || args[1].length()==0 || args[2].length()==0){
          System.out.println("Usage:\n  java BatchFileProcess  parameterA  parameterB  parameterC\n\nOption:\n"
              +"  parameterA -- The directory that it contains the files that you want to process.\n\n"
              +"  parameterB -- The new file that you want it saved all the contents\n\n"
              +"  parameterC -- The file's filter-condition\n\nRun Example:\n"
              +"  java BatchFileProcess  F:\\yaray  C:\\newFile.txt  .txt");
          return;
        }    BatchFileProcess test = new BatchFileProcess();    test.setPath(args[0]); // 设置需要处理的文件夹
        test.setFile(args[1]); // 设置新生成文件的(路径及)文件名
        test.setFilterConditoin(args[2]); // 设置过滤条件    if(test.checkValid()){
          test.writeFile();
        }
      }  private boolean checkValid(){
        boolean hasTxtFile = false;
        try {
          File file = new File(path);
          if(file.isDirectory()){        // 列出所有符合条件的文件
            File[] flist = file.listFiles( new CustomFileFilter(filterConditoin) );
            for(int i=0; i<flist.length; i++){
              if(new File(flist[i].getAbsolutePath()).isFile()){// 避免空过滤条件
                hasTxtFile = true;
                readFile(flist[i].getAbsolutePath());
              }
            }      }else{
            System.out.println("The parameter isn't a Directory");
          }
        }catch (Exception e) {
          e.printStackTrace();
          return false;
        }
        return hasTxtFile;
      }  private boolean readFile(String fileName) {
        try {      BufferedReader bfrdInFile = new BufferedReader(new FileReader(fileName));
          String strLine = "";
          while ((strLine=bfrdInFile.readLine()) != null) {
            strLine = strLine.trim() ;
            if(strLine.startsWith("<")||strLine.equals("")) {
              continue;
            }
            vct.add( (vct.size()+1) +" : " + strLine ) ;
          }
          bfrdInFile.close();
        }catch (Exception e) {
          e.printStackTrace();
          return false;
        }
        return true;
      }  private boolean writeFile(){
        if(!(new File(file)).isFile()){
          System.out.println("The file is not a file.");
          return false;
        }
        try{
          BufferedWriter out = new BufferedWriter(new FileWriter( file , false));
          String line;
          for (int i=0; i<vct.size() ; i++){
              line = (String)vct.get(i);
              //System.out.println( line ) ;
              out.write(line,0, line.length());
              out.newLine();
              //out.println(line);
          }
          out.flush();
          out.close();
        }catch(IOException e){
          e.printStackTrace();
          return false;
        }
        return true;
      }
      // 文件类型过滤
      class CustomFileFilter implements FilenameFilter{    private String filterCondition;    // 参数:文件的扩展名
        public CustomFileFilter(String condition){
          filterCondition = condition;
        }    public boolean accept(File dir, String name){
          return (name.toLowerCase().endsWith( filterCondition.toLowerCase() ));
        }  }}