Hello and welcome to the Ranch! Please read UseCodeTags.
Yes, that would be the approach, as long as you are supplying path(s) to file(s) as command line arguments. Though there is no need to initialize new String array as you do:
You should get values from args.
The quieter you are, the more you are able to hear.
Jai Gates
Greenhorn
Joined: Jul 11, 2012
Posts: 18
posted
0
Yes I wondered about that. But are you suggesting I could do what I proposed? (with args)
Jai Gates wrote:
I need to read in multiple files from the command line.
You mean file names, right?
My idea on how to do this is simply to String[] ar = new String[args.length];
Then just loop through for (int i = 0; i < ar.length; i++)
{
File file = new File(args[i]);
//procedure on file;
}
Is this correct methodology?
If you mean, "Will it work?" then the answer is, "Try it and see!" You can put in something real yet simple for the "// procedure" comment, such as printing out the File's canonical path, or the results of a test for whether it exists.
If you mean, "Is the overall approach reasonable (ignoring any niggling syntax issues)?" then the answer is yes. One thing you could do so tidy up the code a bit is to use a foreach loop:
[EDIT]
I missed the superfluous "ar" array. Add my voice to those of the others telling you to get rid of it.
Yes, you could do that. But creating an empty array with the same length as another array, just for the purpose of finding the length of that other array, is pointless. And other programmers looking at the code would be distracted, wondering why you did it. So you shouldn't do that.
Yes, if you supply absolute path to the file. In addition you can use exists() method on the File object created to see if the file with the given path exists.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35232
7
posted
0
The path can be any path, relative or absolute. It can even be on a remote machine, should that machine have a drive mounted on the local machine.
Jai Gates wrote:Along those lines, (or perhaps I should post a new thread)
When a file is past via command line it is a String..
When we do a
Will that file be found and fed in even if in another part of the directory than the Projects?
--J
The projects don't matter. If it's an absolute path, it will be found regardless, assuming it's a valid path. If it's a relative path, then what matters is the runtime's notion of current working directory. If you're running from within an IDE (not a good idea for a beginner), then the IDE may default it to something like your home directory or the directory where the IDE is installed, but you should be able to configure it in the Run settings for the project. If you do it on the command line (preferred approach), the working directory will be whatever directory you're sitting in when you launch the JVM.
Jai Gates
Greenhorn
Joined: Jul 11, 2012
Posts: 18
posted
0
So at command line it would look something like??: