小弟在写一个文件过滤的程序,基于结构化的比较。现在有一个Parser可以重用,在Readme中发现使用方法。
用command prompt编译完之后,执行命令如下
Java + Parser名字(也就是编译文件的名字) + 文件的绝对路径 可以把文件内容按照自己的设计过滤成另外一个文件。但是,我现在想稍微改变一下这个Java命令。我设计的是 Java + Parser名字(编译文件的名字) + 文件的绝对路径1 + 文件的绝对路径2,这样,一下子就能过滤两个文件。我想请问大家,上述方法如何实现?

解决方案 »

  1.   

    反编译一下那个Parser类,看看main方法怎么写的,然后自己写个类,直接把它的main方法搬过来
      

  2.   

    public static void main(final String[] args){
        for(String arg : args)
            Parser.main(new String[]{arg});
    }
      

  3.   


    parser类中关于main方法args的捕捉是这样的,
    但是我自己试着实现了,我增加了另外一个infile和outfile用于储存另外一个文件路径的命令,但是不成功,
    详细代码如下××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××public static void main(String[] args) 
    {
    String inFile = null; /* name of the input file */
    String outFile = null; /* name of the output file */
    String includeDir = null; 
    String inFile2 = null; //
    String outFile2 = null;
    String includeDir2 = null;    
    try {
         
                 /* process the command line options */
         /* there is input file */
         if (args.length == 1 && !args[0].equals("-h")) {
           inFile = args[0];
           //inFile2 = args[0]
         
         }   
         /* there are input and output files */
         else if (args.length == 3 && args[0].equals("-o")) {
           outFile = args[1];
          // outFile2 = args[1]
           
                           
          // inFile = args[2];
          // inFile2 = args[2];
         }
         /* there is an input file and an include directory */
         else if (args.length == 3 && args[0].equals("-I")) {
           inFile = args[2];
           includeDir = args[1];
         }
         /* there are input and output files, and an include directory */
         else if (args.length == 5 && args[0].equals("-o") && args[2].equals("-I")) {
           outFile = args[1];
           includeDir = args[3];
           inFile = args[4];
         }
         /* there are input and output files, and an include directory */
         else if (args.length == 5 && args[2].equals("-o") && args[0].equals("-I")) {
           outFile = args[3];
           includeDir = args[1];
           inFile = args[4];
         }
         else { /* print the help message and exit */ 
    help_print();
    return;
         }    {
           File file = new File(inFile);
           File file2 = new File(inFile2);
           if (!file.isFile()) {
              System.err.print("Error: cannot find an input file \""
                     + inFile + "\"\n");
              return;
           }
           if (!file.canRead()) {
              System.err.print("Error: cannot read an input file \""
                     + inFile + "\"\n");
              return;
           }         if(!file2.isFile()) {
           
           System.err.print("Error: cannot find an input file2 \""
                                     + inFile + "\"\n");
            return;
            }
           
            if(!file2.canRead())
            {
            System.err.print("Error: cannot read an input file2 \""
            + inFile + "\"\n");
            return;
           
            }
         }
    ×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××8
      

  4.   


    Parser.main(new String[]args{);这句话是干啥的?调用?
      

  5.   

    你上面不是这样调用的
    那么在另一个类里就可以这样调用:
    Parser.main(new String[]{文件的绝对路径});
    举个简单的例子:
    T1.java
    public class T1{
        public static void main(String[] args) {
    System.out.println(args[0].hashCode());
        }
    }
    T2.java
    public class  T2 {
        public static void main(String[] args) {
    for(String arg :args)
        T1.main(new String[]{arg});
        }}
    java T1 a
    java T2 a b c
      

  6.   

    命令行参数解析 请 Google “java parsing command line”
    自己手工处理代码太混乱了。