Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7694761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:23:33+00:00 2026-05-31T21:23:33+00:00

I have a text file which contains something like this: Hello, my name is

  • 0

I have a text file which contains something like this:

Hello, my name is Joe

What is your name?
My name is Jack.

That is good for you.

The only problem is that I have to load it into a JTextArea with the append method to display the text in a JScrollPane like so:

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);

But when I read the file into the text area, the text area displays something like this:

Hello, my name is JoeWhat is your name?My name is Jack.That is good for you.

The BufferedReader never reads in newlines (\n) into the JTextArea. How could I make the reader add the spaces and blank lines as they appear in the file? If anyone can help I would appreciate it. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:23:35+00:00Added an answer on May 31, 2026 at 9:23 pm

    All JTextComponents have the ability to read in text files and write to text files while fully respecting the newline character for the current operating system, and it is often advantageous to use this. In your case, you would use the JTextArea’s read(...) method to read in the file while fully understanding the file system’s native new-line character. Something like so:

    BufferedReader br = new BufferedReader(new FileReader(file));
    textArea.read(br, null);
    

    Or for a more complete example:

    import java.io.*;
    import javax.swing.*;
    
    public class TextIntoTextArea {
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    
       private static void createAndShowGui() {
          JFileChooser fileChooser = new JFileChooser();
          int response = fileChooser.showOpenDialog(null);
          if (response == JFileChooser.APPROVE_OPTION) {
             File file = fileChooser.getSelectedFile();
             BufferedReader br = null;
             try {
                br = new BufferedReader(new FileReader(file));
                final JTextArea textArea = new JTextArea(20, 40);
    
                textArea.read(br, null); // here we read in the text file
    
                JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
             } catch (FileNotFoundException e) {
                e.printStackTrace();
             } catch (IOException e) {
                e.printStackTrace();
             } finally {
                if (br != null) {
                   try {
                      br.close();
                   } catch (IOException e) {
                   }
                }
             }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which contains many (over 5000) links. Like this: http://www.website.com/rgfdefvrggh
I have a text file which contains columns of data that are either integer,
(PHP 5.3.6) I have a php file which contains simply this text - there
I have a text file which contains data seperated by '|'. I need to
I have an XML file which I'd like to parse into a non-XML (text)
i have a java ee project which has a text file that uses a
I have a very simple python script that should scan a text file, which
I have text file with a row like this: SendersTimeSeriesIdentification: COMPANY_A/COMPANY_B/REF_7/20090505 I'd like to
I have text file with some stuff that i would like to put into
Problem I have a file which contains such text (nothing else) 2010-10-05 I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.