Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Reading a file into an array.

Thread view: 
Stephen - 06 Mar 2004 15:09 GMT
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
Christophe Vanfleteren - 06 Mar 2004 15:18 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.