question about reading text file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: question about reading text file

  1. #1
    Join Date
    Aug 2005
    Posts
    3

    Question question about reading text file

    hello! i am new to java
    i don't know how to read the text from *.txt file and then return the string to the application screen
    can anyone help?

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    There have been posts here of simple programs that do both. Look thru previous posts or use search for 'readLine()' and System.out.println() for example.

  3. #3
    Join Date
    Aug 2005
    Posts
    24
    BufferedReader buff = new BufferedReader( new FileReader("*.txt") ) ;
    String line = buff.readLine();
    System.out.println(line);
    (* = any file name)

  4. #4
    Join Date
    Aug 2005
    Posts
    3
    it works! thanks
    but if the text file contains more than 1 line, how can all lines be display until null?

  5. #5
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Code:
    import java.io.*;
    
    public class Test {
        public static void main(String[] args) {
            try {
                BufferedReader buff = new BufferedReader( new FileReader("test.txt") ) ;
                
                String line = buff.readLine();
    
                while(line != null) {
                    System.out.println(line);
                    line = buff.readLine();
                }
            }
            catch(Exception e) { e.printStackTrace(); }
        }
    }

  6. #6
    Join Date
    Aug 2005
    Posts
    3
    thanks! prometheuzz
    i tried and it works! however, i have some more question...
    i tried to use StringTokenizer for seperating my text in the file... however, it only works for the first line of my text file...

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Test {
        public static void main(String[] args) {
            try {
                BufferedReader buff = new BufferedReader( new FileReader("test.txt") ) ;
                String line = buff.readLine();
                StringTokenizer st = new StringTokenizer (line, ",");
    
    
                while(st.hasMoreTokens()) {
                    System.out.println(st.nextToken());
                    //line = buff.readLine();
                }
    				buff.close();
            }
            catch(Exception e) { e.printStackTrace(); }
        }
    	 
    }

  7. #7
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Your code only uses StringTokenizer on the first line. You need another while() loop surrounding the one you have that will continue to readLine() until it returns null.
    The outer loop will readLine() until null is returned, the inner loop will process the tokens for the last line read.

    Give it a try. Or wait until someone else writes the code for you.

Similar Threads

  1. Replies: 0
    Last Post: 08-08-2002, 06:56 AM
  2. reading text file with FSO on client side
    By ariel in forum ASP.NET
    Replies: 0
    Last Post: 10-09-2001, 06:00 PM
  3. Replies: 1
    Last Post: 03-06-2001, 09:42 AM
  4. Reading a text file using ADO
    By Ted Young in forum VB Classic
    Replies: 1
    Last Post: 08-16-2000, 09:55 AM
  5. Replies: 0
    Last Post: 04-17-2000, 02: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