Results 1 to 4 of 4
  1. #1
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default How do I make the line go to the next line when using Buffered Reader?

    Hi,

    I'm trying to play with Buffered Reader but I'm not sure how do I make the lines go to the next line.

    Hope someone can advise me.
    Java Code:
    try {
    File file = new File("Remedies.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuffer stringBuffer = new StringBuffer();
    String line;
    while((line = bufferedReader.readLine())!= null) {
        stringBuffer.append(line);
        stringBuffer.append("\n");
    }
    fileReader.close();
    System.out.println("Contents of file : ");
    JOptionPane.showMessageDialog(null, stringBuffer.toString());
    The result came out with a box and the line just keep to one long line.
    I would like the text box to resize to a reasonable readable size and the text will go to the next line until the passage has been read.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I make the line go to the next line when using Buffered Reader?

    Your code should work, and should display each new line in the text file on a new line in the JOptionPane. Are you sure that the text file holds separate lines?

  3. #3
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Re: How do I make the line go to the next line when using Buffered Reader?

    Hi,

    It doesn't work. All the display box displayed is one line and not like the text below.

    This is the text in notepad that I have wanted the Buffered Reader to read in and display.

    I can't find the attached file in this post reply so here's the text copied for you to see.

    IMHO, it is best to engage in behaviors and activities that cater to our useful gods. As JY has pointed out on numerous occasions, nothing else can substitute for "human luck." We are masters of our destiny. We have to make conscious choices in our actions, careers, partners, and behaviors. Classical Feng Shui provides us with clear guidelines on ways to harmonize our living/work spaces to maximally receive favorable Qi from our environment. In so doing, this positions us most favorably to accomplish our goals. Therefore, IMO, Feng Shui cannot substitute for missing Qi in one's BaZi. Only willful human actions/behaviors that are also aligned with our useful gods can.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I make the line go to the next line when using Buffered Reader?

    Then the problem is with your text file, in that it likely has no new-lines in it but rather is all one long line that only appears to have new lines due to your having set your text editor to wrap the text. A possible solution is to create a JTextArea with x rows and y columns (you'll have to play with these values), with these methods called, setLineWrap(true) and setWrapStyleWord(true), and then setting your text into the JTextArea and displaying the text area in the JOPtionPane.

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 03:41 AM
  2. Line Reader changeing to read 1 line
    By Javanooby in forum New To Java
    Replies: 15
    Last Post: 05-10-2011, 05:34 AM
  3. Replies: 1
    Last Post: 02-13-2011, 04:09 PM
  4. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  5. Replies: 4
    Last Post: 09-10-2009, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •