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.