Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Read text file

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read text file

    Hey guys so here is my problem,i have a text file that i want it to be shown in a textarea here is the code i am using,the problem is that it doesnt read the whole file,it reads only the first line any help appreciated.
    try{
    FileReader fr = new FileReader("file2.txt");
    BufferedReader myInput = new BufferedReader(fr);
    text = myInput.readLine();
    while ((tmp = myInput.readLine()) != null){
    text += tmp;
                                                              }
    jTextArea1.setText(text);
    }
    catch(Exception e){}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read text file

    A comment on your code:
    NEVER have an empty catch clause. Add at lease a e.printStackTrace() to it

    Add a println to show the value of tmp following each read.
    Last edited by Norm; June 23rd, 2011 at 11:34 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Starving
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read text file

    Quote Originally Posted by cardamis View Post
    Hey guys so here is my problem,i have a text file that i want it to be shown in a textarea here is the code i am using,the problem is that it doesnt read the whole file,it reads only the first line any help appreciated.
    try{
    FileReader fr = new FileReader("file2.txt");
    BufferedReader myInput = new BufferedReader(fr);
    text = myInput.readLine();
    while ((tmp = myInput.readLine()) != null){
    text += tmp;
                                                              }
    jTextArea1.setText(text);
    }
    catch(Exception e){}

    Replace the following line:

    jTextArea1.setText(text);

    with the following :
    System.out.println(text);

    If you see your file completely ( not only the first line ), then something wrong with your JTextArea, otherwise, if your println shows the same as
    the JTextArea, most likely something is not quite right with reading the file, maybe u don't have Return carriages at the end of each line in the file2.txt.

Similar Threads

  1. Re: How to read a text from an Image file?
    By aparnaverma in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 5th, 2011, 08:01 AM
  2. How to read a text from an Image file?
    By GautamRy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 5th, 2011, 05:02 AM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM