Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading in Multiple Files as Command Line Arguments

 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you need the "ar" array, since you already have the "args" array that you can use?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I wondered about that. But are you suggesting I could do what I proposed? (with args)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks a million!

and will use code tags from now on.
 
Paul Clapham
Sheriff
Pie
Posts: 20959
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:


I don't think args is array of Files. You mean:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:
I don't think args is array of Files. You mean:


Oops, yes, of course.

 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So at command line it would look something like??:








--J
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic