How to read all words in a text file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to read all words in a text file

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    1

    Unhappy How to read all words in a text file

    Dear all,

    Does anyone know how to rewrite the following code to read all the words from a text file since I've run it and it only reads the last line. I want to avoid using Lists since I have no knowlegde of how to use them in the code.

    Code:
    ............
    ..........
    try
    {
             br = new BufferedReader(new FileReader(args[0]));
    }
    catch (FileNotFoundException fnfe)
    {
               System.out.println("Error opening file '" + args[0] + "'. " +"Please omit .txt extension.");
                System.exit(3);
    }
    boolean finish = false;
    String input = null;
    String[] array=null;
    //read lines until end of file is reached
    while (!finish)
    {
           try
           {
                     input = br.readLine();
            }
            catch(IOException ioe)
            {
                   System.out.println("I/O error");
                    System.exit(2);
            }
            //end of file?
             if (input==null)
             {
                      finish=true;
              }
              else
             {
     
                     array = input.split("\\s+");
              }
          }   
    Thanks in advance!

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are "reinitializing" the content of your String array with every line read from the buffered reader stream ... so you'll find only the tokens created by your split of the last line from the stream.

Similar Threads

  1. Extract words from JSP into text file
    By Michelle in forum Java
    Replies: 2
    Last Post: 02-23-2002, 12:53 AM
  2. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  3. setting up text file downloads
    By sean in forum Web
    Replies: 1
    Last Post: 07-27-2001, 11:08 AM
  4. open text file using shell
    By Hian Chew in forum VB Classic
    Replies: 0
    Last Post: 03-06-2001, 12:28 PM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center