Saturday 15 September 2012

Reading from a text file in Java -


I have a text file like this:

  items 1 item 2 item 3 < / Code>  

I should be able to read every "item X" in a string and ideally all the stars must be stored in the form of vector / arreiilist.

I tried:

  InputStream = new FileInputStream ("file.txt"); is read(); // looped for every line of text   

but it seems that only handles integer.

Thanks

You have a lot of answers here, the easiest of us For a scanner (java.util).

There are several types of feature such as nextLine () and next () and nextINT (), so that you can do the following:

  scanner scanner = new scanner (New file ("file.txt")); & Lt; String & gt; Text = new arreelist & lt; String & gt; (); While (scanner.hasNextLine ()) {text.add (scanner.nextLine ()); }   

Alternatively you can use a buffered reader (in java.io):

  BufferedReader Reader = new BufferedReader (New FileReader ( "File.txt")); & Lt; String & gt; Text = new arreelist & lt; String & gt; (); (String line; (line = reader. Readline ())! = Null;) {text.add (line); }   

However scanners are usually easy to work on.

No comments:

Post a Comment