• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query in code of displaying a text file

 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


code cited from java complete reference

i have two questions
A. Where should the file "TEST.TXT" be kept ??
B. Why is args[0] given as an argument


Thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see the answer in the comments specified above the class.

/* Display a text file.
To use this program, specify the name
of the file that you want to see.
For example, to see a file called TEST.TXT,
use the following command line.
java ShowFile TEST.TXT
*/
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies i missed to answer (A) - you can have it any where and should send the correct path as the command line argument to the program...

say java ShowFile C:/MyFolder/TEST.TXT
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that example really use try-catch to test whether you have no arguments?
 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats what !! i am confused about this line


can anyone help ??
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which bit are you confused about? That is straighforward enough.
FileInputStream "the next variable will be of type FileIn...."
fin "I am going to call that variable fin."
= "take whatever is on the right of this operator and give that value to the name on the left"
new "an operator which tells the JVM there will be a new object"
FileInputStream(...) "the object will be of type FileIn... and the JVM is to call its constructor with the arguments in the ()"
new "same as before"
File(...) "the object will be of type File, and the JVM is to call its constructor with the arguments in the ()"
args[0] "the first word you wrote after the name of the class when you went to the command line."
; "End of this statement: go and look for the next statement."

If it is args[0] you were confused about, try here.
 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i had doubt in args[0], well thanks a lot Ritchie, the link is informative.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic