How to read in a text file in as an array?

Discussion in 'Java' started by heano30@gmail.com, Feb 7, 2006.

  1. Guest

    Hi, im trying to read a text file into my program. It simply contains a
    dictionary of commonly used words, but i want to store this list of
    words as an array so that I will be able to make use of it later when
    comparing against other text files. So how do I do this?(read in the
    file as an array i mean) Any suggestions+code would be much
    appreciated,thanks
    , Feb 7, 2006
    #1
    1. Advertising

  2. Roedy Green Guest

    On 7 Feb 2006 04:49:00 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >Hi, im trying to read a text file into my program. It simply contains a
    >dictionary of commonly used words, but i want to store this list of
    >words as an array so that I will be able to make use of it later when
    >comparing against other text files. So how do I do this?(read in the
    >file as an array i mean) Any suggestions+code would be much
    >appreciated,thanks


    Let's say it is a HashMap. Just store the HashMap as a single
    serialiased object.

    see http://mindprod.com/jgloss/serialization.html
    http://mindprod.com/applets/fileio.html
    to generate some sample code.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 7, 2006
    #2
    1. Advertising

  3. Try this program, it uses Generics feature compatible in JDK 5.0 or
    above:

    /*
    * WordReader.java - A program to read words from a file
    */
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;

    import java.util.List;
    import java.util.ArrayList;

    public class WordReader {
    public static void main( String[] args ) {
    // will store the words read from the file
    List<String> wordList = new ArrayList<String>();

    BufferedReader br = null;
    try {
    // attempt to open the words file
    br = new BufferedReader( new FileReader( "words.txt" ) );

    String word;

    // loop and read a line from the file as long as we dont
    get null
    while( ( word = br.readLine() ) != null )
    // add the read word to the wordList
    wordList.add( word );
    } catch( IOException e ) {
    e.printStackTrace();
    } finally {
    try {
    // attempt the close the file
    br.close();
    } catch( IOException ex ) {
    ex.printStackTrace();
    }
    }

    // initialize a new string array equal to the size of the
    wordList
    String[] words = new String[ wordList.size() ];

    // call the wordList's toArray method to and transfer items
    from
    // wordList to our string array words
    wordList.toArray( words );

    // loop and display each word from the words array
    for( int i = 0; i < words.length; i++ )
    System.out.println( words[ i ] );
    }
    }

    Hope it helps!
    Ranganath Kini, Feb 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    1
    Views:
    1,055
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 20, 2005
  2. Replies:
    2
    Views:
    615
    Thomas Matthews
    Feb 27, 2007
  3. Mmcolli00 Mom
    Replies:
    2
    Views:
    182
    Mmcolli00 Mom
    Jan 27, 2009
  4. Alex Dowad
    Replies:
    4
    Views:
    248
    Michel Demazure
    May 1, 2010
  5. Replies:
    19
    Views:
    220
    Andreas Perstinger
    Oct 7, 2013
Loading...

Share This Page