• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

command line arguments for a file

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a prompt to request a file name. You can use a file chooser. Lots of ways to do it.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya.. but how to use those arguments in the coding instead of giving the file name instead?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line this?
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic