Hi. I have a file that will be typed into something like notepad and I would
like my program to read the file line-by-line, putting each line into an
array. In Visual Basic it was very simple. Is there an equivalent to this in
Java:
Open "test.txt" for input as #1
for a = 1 to 10
input #1, array(a)
next
Close #1
I want to use showOpenDialog which stores the file in a File object. Thanks
> Hi. I have a file that will be typed into something like notepad and I
> would like my program to read the file line-by-line, putting each line
[quoted text clipped - 9 lines]
> I want to use showOpenDialog which stores the file in a File object.
> Thanks
Use a FileReader:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html
Tutorial on IO:
http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html

Signature
Kind regards,
Christophe Vanfleteren
Jon A. Cruz - 06 Mar 2004 18:54 GMT
> Use a FileReader:
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html
Just be careful. The FileReader will use the default encoding. That's
not always the best thing.
Usually you'll want to create a FileInputStream around your File.
File f;
...
FileInputStream fin = new FileInputStream(f);
Then if you want characters it can be wrapped in an InputStreamReader
using an explicit encoding.
http://java.sun.com/docs/books/tutorial/essential/io/processing.html
InputStreamReader in = new InputStreamReader(fin, "Cp1252");
Then you'd probably want to wrap things with a LineNumberInputStream and
just read from that.
The encoding name matters because unlike VB where characters are 8-bit,
Java characters are 16-bit Unicode, so you always are peforming a
transformation. It's just a matter of whether or not it's hidden from
you. Hidden makes it easier to program, but much easier for tricky hard
to find bugs to get in.