wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes 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 "Command line arguments" Watch "Command line arguments" New topic
Author

Command line arguments

Andrei Dicker
Greenhorn

Joined: Aug 18, 2001
Posts: 10
Hi,
I'm not sure this is possible, but is there a way to write a program that accepts a file name as a command line argument. Then open the file and display its contents to the console. That is a text file?
Thanks,
AD
------------------
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yes you can.
This is my first test on io . this class takes an textfile as an argument and tests if the textfile exists , if youo can read from it and if it's not an directory. It does not check for any path. It just check in the directory you're in. This is just a sample. The code can be improved and you may want to use another reader.
Andrei Dicker
Greenhorn

Joined: Aug 18, 2001
Posts: 10
Hi Mathias P.W Nilsson,
First of all, thanks for the quick response. Maybe I'm doing something wrong here. When I attempt to run the program I continue to get the "Please run with an argument" response.
When I type in what I believe to be the correct path, I get a series of numbers.
Do I just type in the file name after the prompt? Example please.
Thanks again,
AD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is a quick example as I said before. yes , use java test your_text_file.txt. your_text_file.txt must exist ni the directory that your java file lies. Yes, it just prints numbers. Use java api to get the correct reader! I read my first pages on IO just as I posted my replay. The post was to prove that it is possible to take an txt file as an argument and print it's contens.
Hope this helps! Please post some code with a real writer.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The previous code had an error (reader.read() was discarded in comparison) so I'll provide an alternative.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for your replay David!
Can you or anybody else explain why reader.read was an error. I know that the code didn't achive what I wanted but it wasn't a compile or runtime error. How does one know what writer, printer to use?
Regards Mathias
Andrei Dicker
Greenhorn

Joined: Aug 18, 2001
Posts: 10
Hi David Garland,
I just got the chance to try out your code. Nice job. I like the way you've used the InputStream rather than Reader. Yesterday, I wasn't so sure. Thanks alot. You guys are good!
AD
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Mathias,
Your use of 'reader.read' was an error in one way, and less efficient than optimal in another. The error can be seen here:

You discard the first character you read and write the second without checking it! What you probably meant was some variation of 'while ((c = reader.read()) != -1)' and 'System.out.write(c)'.
The efficiency issue is that you are reading one character at a time. By doing the copying in bulk with the .read(char[]) or .read(char[],int,int) you would spend fewer clock cycles comparing numbers to 1 and save a lot of method calls. That is how the BufferedReader/BufferedInputStream classes work -- they read data in bulk and store it in memory until you need it.
Reader vs. InputStream
The Reader/Writer classes are meant to manipulate and en/decode characters and files in all sorts of encodings (ascii, UTF-8, UTF-16, Unicode, etc.) The stream classes are meant to manipulate raw bytes (what you would store to a file system or send over a network). Inserting unnecessary readers/writers between input and output streams adds unnecessary computation encoding/decoding characters. I suspect that this program will only be used on ASCII files anyway. If you test it on a different encoding and it doesn't work, than it may be necessary to insert a Reader/Writer layer and a command line argument to specify the encoding (I don't think the Reader classes can guess).
[This message has been edited by David Garland (edited August 19, 2001).]
[This message has been edited by David Garland (edited August 19, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks David! Great explination on the "while ((c = reader.read()) != -1)" . Can you read an word or excel file using these readers , writers?
Hope I'm not asking to much. When I first started reading about java.io I actually thought that io isn't fun. Now , after seeing what can be done with java.io I don't have this feeling anymore.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
You don't want to use Reader/Writer classes on binary files. That's what the streams are for. I don't know exactly what the format of Word/Excel files is, but except for the office 2000 word xml format you would have to interpret the binary (or access the files through third party software).
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I just noticed I never said the most important thing about the Reader/Writer classes! When I said encodings, I meant specifically text encodings. Readers and Writers are meant to be used on text files. They weren't necessary in the above code however, because it doesn't need to support multiple encodings.
[This message has been edited by David Garland (edited August 19, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command line arguments