0

I am trying to follow this link: Parsing arguments to a Java command line program

and use arguments in java to parse some file.

The file I need to feed to the application are, mainly, 2. Let's call them file1 and file2.

I wish I can call the java like:

/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.

My code before understanding that I need arguments in -f -s format:

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.

Community
  • 1
  • 1
aPugLife
  • 989
  • 2
  • 14
  • 25
  • Although I do not completely understand what your problem is, perhaps you should have a look into Apache Commons CLI: https://commons.apache.org/proper/commons-cli/ Also, you shouldn't use underscores in method and variable names. – Florian Albrecht Oct 17 '16 at 12:28
  • 2
    Passing or parsing? They're very different things. – byxor Oct 17 '16 at 12:28
  • Parse a file passed through args, I think this is correct (?) - OK I am having a look at that link, thanks. Thanks for the hint too – aPugLife Oct 17 '16 at 12:30
  • all arguments are dropped in as elements of the array `args` (as the name may have implied...) – Timothy Truckle Oct 17 '16 at 12:37

3 Answers3

3

Just do a switch case on the first argument (-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 :
}
Turamarth
  • 2,282
  • 4
  • 25
  • 31
  • Oh god I have not thought of that! Seen from here looks incredibly easy. Alright I'll try this way and see if can help me! – aPugLife Oct 17 '16 at 12:36
0

It's very simple. Just use java.io.File api to implement this.

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

and the use InputStream to manipulate file.

mutou
  • 51
  • 1
  • 5
  • I know you can not comment because lower than 50 points, BUT if you read the code, I am already using this code. Thing is, I need to switch from this one to the one with dashes and letters: -f (for example). I'm sorry but your answer does not help – aPugLife Oct 17 '16 at 12:34
0

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I don't know if I understood correctly, but reading other answers I guess I was misreading the meaning of args[]. Beside this, I don't know where to use the code you suggested (or better, the notation myApp.main I have not seen anywhere ) forgive me, I only need java for this project only – aPugLife Oct 17 '16 at 12:43
  • @Nihvel the code is showing you what the JVM does when you run the code above. There is nothing special about the array of strings you get passed to you. You can check them the same way you are already doing, e.g. with `switch` nothing special. It's not clear where the confusion lies. – Peter Lawrey Oct 17 '16 at 12:46
  • 1
    Okay, now I understood it! Thanks for explaining! It does not fully help me because I'm not skilled with java. A grandma-proof answer, in my case, was better accepted. Anyhow, got it. thanks! – aPugLife Oct 17 '16 at 12:54