简体   繁体   English

从命令行解析作为参数传递给Java的文件

[英]Parsing a file passed as argument to Java from command line

I am trying to follow this link: Parsing arguments to a Java command line program 我试图遵循此链接:将参数解析为Java命令行程序

and use arguments in java to parse some file. 并使用Java中的参数来解析某些文件。

The file I need to feed to the application are, mainly, 2. Let's call them file1 and file2. 我需要提供给应用程序的文件主要是2。让我们将它们称为file1和file2。

I wish I can call the java like: 我希望我可以像这样调用Java:

/usr/bin/java -jar myapplication.jar -f file1

or 要么

/usr/bin/java -jar myapplication.jar -s file2

Never ever at the same time. 永远不会在同一时间。

In that link I posted, I can not understand how to pass an argument, which is a file, to the void main. 在我发布的链接中,我无法理解如何将参数(即文件)传递给void main。

My code before understanding that I need arguments in -f -s format: 在理解我需要-f -s格式的参数之前的代码:

public class myApp {
    public static void main(String[] args) throws IOException, JAXBException, SQLException {
        Database db = new Database();
        if ( args.length == 0 ) {
            System.out.println("No arguments were given.\n");
            db.disconnect();
        }
        else {
            System.out.println("Got the argument.\n");
            File file = new File(args[0]);
            String namefile = file.getName();

            switch (namefile.substring(0, 6)) {
                case "FILE01":
                    System.out.println("Processing file type FILE01: "+namefile);
                    Process_filezeroone program_zeroone = new Process_filezeroone();
                    program_zeroone.process(file, namefile);
                    break;
                case "FILE02":
                    System.out.println("Processing file type FILE02: "+namefile);
                    Process_filezerotwo program_zerotwo = new Process_filezerotwo();
                    program_zerotwo.process(file, namefile);
                    break;
                default:
                    System.out.println("Skipping: "+namefile);
                    db.disconnect();
            }
        }
    }
}

Thank you for any help. 感谢您的任何帮助。

Just do a switch case on the first argument ( -f / -s ). 只需对第一个参数( -f / -s )进行切换即可。

File file;
switch(args[0]){
    case "-f" : 
        file = new File(args[1]);
        //do stuff with file1
        break;
    case "-s" : 
        file = new File(args[1]);
        //do stuff with file2
        break;
    default :
}

It's very simple. 非常简单 Just use java.io.File api to implement this. 只需使用java.io.File api即可实现。

File f = new File(args[0]);

and the use InputStream to manipulate file. 并使用InputStream操作文件。

In the first case you have the same as 在第一种情况下,您具有

myApp.main(new String[] { "-f", "file1" });

and in the second case 在第二种情况下

myApp.main(new String[] { "-s", "file2" });

If you find the arguments confusing I suggest you use your debugger to look at the array or print out the arguments. 如果发现参数令人困惑,建议您使用调试器查看数组或打印出参数。

You can use a switch to switch between them or use a couple of if / else if statements. 您可以使用一个switch在它们之间进行切换,或使用一对夫妇if / else if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM