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

Thread: Getting extra lines when saving and loading into a JTextArea

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Getting extra lines when saving and loading into a JTextArea

    I have made a SSCCE, it only has what I need. The problem happens to be replicated in this section, so my hunch was correct of where the problem was coming from. I think I might know the issue.

    In the past, I had used the line break character to do line breaks, and the spacing was correct. However, I wanted a platform independent approach so I did System.getProperty("line.separator") instead and now the spacing is messed up. The new line and the System.getProperty() way both should work, with the latter being more portable. However, it is not reading it in properly.

    I'm not sure yet if the problem is just caused by the read in, or if, when saved, it is messing up.

     
     
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import javax.swing.JFileChooser;
    import javax.swing.JScrollPane;
     
    public class IOWoes extends JFrame
    {
     
       private JPanel contentPane;
       private JButton saveButton;
       private JButton loadButton;
       private JTextArea textArea;
     
       public IOWoes()
       {
     
          super("Why isn't this working?");
     
     
          contentPane = new JPanel(new BorderLayout());
     
          setContentPane(contentPane);
     
          textArea = new JTextArea(30, 30);
     
          JScrollPane jsp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
          contentPane.add(jsp, BorderLayout.NORTH);
     
          JPanel lowerPanel = new JPanel(new BorderLayout());
     
          contentPane.add(lowerPanel, BorderLayout.SOUTH);
     
          saveButton = new JButton("Save");
     
          lowerPanel.add(saveButton, BorderLayout.WEST);
     
          loadButton = new JButton("Load");
     
          lowerPanel.add(loadButton, BorderLayout.EAST);
     
          saveButton.addActionListener(
                new ActionListener() {
     
                   public void actionPerformed(ActionEvent e)
                   {
     
                      JFileChooser chooser = new JFileChooser();
     
                      int option = chooser.showSaveDialog(null);
     
     
                      if (option == JFileChooser.APPROVE_OPTION)
                      {
     
                         File f = chooser.getSelectedFile();
     
     
                         writeToFile(f, textArea);
     
     
                      }
     
     
     
     
     
                   }});
     
     
          loadButton.addActionListener(
                new ActionListener() {
     
                   public void actionPerformed(ActionEvent e)
                   {
     
                      JFileChooser chooser = new JFileChooser();
     
                      int option = chooser.showOpenDialog(null);
     
     
                      if (option == JFileChooser.APPROVE_OPTION)
                      {
     
                         File f = chooser.getSelectedFile();
     
     
     
                         readTextFromFile(textArea, f);
     
                      }
     
                   }});
     
     
     
     
          setDefaultCloseOperation(EXIT_ON_CLOSE);
     
          setVisible(true);
     
       }
     
       public static void main(String[] args)
       {
     
          new IOWoes();
     
     
       }
     
     
     
     
       private void writeToFile(File f, JTextArea area)
       {
     
          try (
          java.io.BufferedWriter fileOut = new java.io.BufferedWriter(new java.io.FileWriter(f))) {
             area.write(fileOut);
          }
     
          catch (java.io.IOException ioe) {
             ioe.printStackTrace();
          }
     
       }
     
       private String readTextFromFile(JTextArea jtt, File f)
       {
     
          Scanner s = null;
          String readin = "";
     
          try
          {
             s = new Scanner(f);
          }
     
          catch(NullPointerException npe)
          {
     
     
          }
     
          catch(FileNotFoundException fnfe)
          {
             //JOptionPane.showMessageDialog(null, "File not found.", "Could not open selected file.", JOptionPane.ERROR_MESSAGE);
             return null;
     
          }
     
          while (s.hasNext())
          {
     
             readin = readin + s.nextLine() + System.getProperty("line.separator");
     
     
     
          }
     
          s.close();
     
     
          jtt.setText(readin);
     
     
     
          return readin;
     
     
     
       }
     
     
    }


    The problem is in one (or both) of the IO methods. I'm not sure what the save one is doing as it is a different approach than what I normally had used.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,373
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Getting extra lines when saving and loading into a JTextArea

    The System.getProperty("line.separator") String is for writing Strings to a disk file on an OS, not for using in a text area. Change to use "\n".
    If you don't understand my answer, don't ignore it, ask a question.

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

    GoodbyeWorld (December 19th, 2013)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Getting extra lines when saving and loading into a JTextArea

    Thanks. So, what do you use then for a cross-platform line break thing? I heard that "\n" was only for Windows or something. That it wouldn't work on all systems.

    (By the way, I did get it to work. I fixed the readTextFromFile() method to use the JTextArea read() method. Now it works.

    I had heard that "\n" wasn't platform independent and looked up a way and came across the System.getProperty("line.separator").

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,373
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Getting extra lines when saving and loading into a JTextArea

    I would use System.getProperty("line.separator") when writing to disk to be cross system compatible.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Getting extra lines when saving and loading into a JTextArea

    Quote Originally Posted by Norm View Post
    I would use System.getProperty("line.separator") when writing to disk to be cross system compatible.
    So, the "\n" is cross platform already.

    Also, when you say "writing to disk", do you mean the hard drive, and hence that I shouldn't use JTextArea write() or whatever it was called to do it?

    Or do you mean a separate disk outside of the hard drive? Like the kind they use to hold data at insurance companies and stuff like that.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,373
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Getting extra lines when saving and loading into a JTextArea

    By "disk" I meant some permanent medium outside of a computer's memory.

    I don't think "\n" is cross platforms. I assume that what System.getProperty("line.separator") returns is cross platforms.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    GoodbyeWorld (December 19th, 2013)

Similar Threads

  1. Saving and loading
    By wuppy29 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 21st, 2012, 10:38 AM
  2. saving / loading a file
    By Herah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 06:04 AM
  3. Saving and Loading
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 01:31 PM
  4. How would you get a JTextArea to know how many lines it had?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2011, 06:58 PM

Tags for this Thread