This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes command line arguments for a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "command line arguments for a file" Watch "command line arguments for a file" New topic
Author

command line arguments for a file

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i have an file to read and i have given the file name inside the FileInputStream and i want to give the file name during run time like this

compile
javac FileRead.java
run
java FileRead 1.txt and the contents inside the file should be displayed

the coding is

what are all the necessary changes to be made for giving the input text file name during run time?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
You can create a prompt to request a file name. You can use a file chooser. Lots of ways to do it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The code on line 12 is not doing what the comment on lines 10 and 11 say.

The args array that is passed to the main method (line 4) contains the command line parameters.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya.. but how to use those arguments in the coding instead of giving the file name instead?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Line this?
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i'm getting an error like this for the code what i'm using
error


can you tell why i'm getting this error?
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
can any one make my code reor free and get the output in the file? or can you send an error free code for java?please any one help me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
deepika deepi wrote:can any one make my code reor free and get the output in the file? or can you send an error free code for java?
No. That’s not how we work.
deepika deepi wrote:please any one help me
We are trying to help you. You asked how to get the file name from the command line and I sent you a link. Did you read it? You didn’t tell us what error you are suffering.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Please ShowSomeEffort.

Did you click the link that Campbell posted? The tutorial shows very clearly how to use the command line arguments.

Look at line 14 of your code. You're not using the command line arguments here.

The command line arguments are what is passed to the main method in the args array.

So, what do you think you need to change in line 14 so that the program is going to use the first command line argument there?
 
 
subject: command line arguments for a file
 
Similar Threads
Regarding reading a file in a jsp
Vectors within vectors
Vectors within vectors
Why do i get NoClassDefFound error??
read and write from txt file