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.
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 :
}