aspose file tools*
The moose likes Beginning Java and the fly likes Reading in Multiple Files as Command Line Arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading in Multiple Files as Command Line Arguments" Watch "Reading in Multiple Files as Command Line Arguments" New topic
Author

Reading in Multiple Files as Command Line Arguments

Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
Hi all,

I need to read in multiple files from the command line.

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?

J











Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41865
    
  63
Why would you need the "ar" array, since you already have the "args" array that you can use?


Ping & DNS - my free Android networking tools app
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Yes I wondered about that. But are you suggesting I could do what I proposed? (with args)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

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.
Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
Ok thanks a million!

and will use code tags from now on.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Jeff Verdegan wrote:


I don't think args is array of Files. You mean:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kemal Sokolovic wrote:
I don't think args is array of Files. You mean:


Oops, yes, of course.

Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
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
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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: 41865
    
  63
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.

Edit: Too late...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
So at command line it would look something like??:








--J
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Yes.
Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
Awesome thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading in Multiple Files as Command Line Arguments