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 5 of 5

Thread: Reading a file with bufferedreader

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Reading a file with bufferedreader

    I can open a file and have it's contents read and printed out on the compiler, so I atleast know that it is reading the file. What I am trying to do though, is to set what is being read, into a JTextArea of a memopad that I created. I just can't get it to work.

    This is my textarea:

    private JTextArea display = new JTextArea();
    ]

    This is my method to open a file:

    private void open()
    {
      String file = JOptionPane.showInputDialog("Please eneter a file name");
      if(file.equals("") || file.length()==0)
     JOptionPane.showMessageDialog(null, "No file selected. Please enter a file name.");
     String text;
         try
            {
              in = new BufferedReader(new FileReader(file));
     
              while((text=in.readLine())!=null)
                {
                   System.out.println(text);//just to make sure it is reading
                 }
     
               display.setText(text);
               in.close();
           }
     
        catch(FileNotFoundException fnfe)
          {
            JOptionPane.showMessageDialog(null, "Error opening file. File Not Found.");
          }
       catch(IOException e)
        {
          JOptionPane.showMessageDialog(null, "Error reading file.");
        }
     
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reading a file with bufferedreader

    Set text replaces all text in the textbox with the given string. You're assigning text to whatever is being read from the file, so when you're done reading, text will always contain null.

    Then, you're setting the text of the textbox to null.

    Instead, you need to concatenate everything you read into one string (preferably a StringBuilder, these are well suited for this type of situation), then set the text of the textbox using this.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    ppata (November 20th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file with bufferedreader

    Thanks! This is what I came up with
    try
     {
      in = new BufferedReader(new FileReader(file));
     String read=in.readLine();
     String text=new String();
     
    while((read=in.readLine())!=null)
     {
       text=(new StringBuilder()).append(text).append(read).append("\n").toString();
       System.out.println(text);//just to make sure it is reading
     }
            in.close();
            display.setText(text);
     
    }

    Only problem is, it is not reading the first line of text from the file?

  5. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file with bufferedreader

    Sorry! Nevermind, I figured out why!! Sheesh, that was simple :[

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Reading a file with bufferedreader

    Quote Originally Posted by ppata View Post
    Sorry! Nevermind, I figured out why!! Sheesh, that was simple :[
    Usually the hardest questions have the simplest solutions.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    ppata (November 20th, 2010)

Similar Threads

  1. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  2. Help with reading from file
    By drazenmd in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 15th, 2010, 03:43 AM
  3. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 03:14 AM
  4. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM