using sun-jdk 1.6.0.20 on Gentoo 32 bit
When i create a text file and then open it back up with the below applcation,
i get little squares appended to the end of the text.
The appended characters only show up in the JTextArea, but they do not show up in any text editors on my computer.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class FileIO extends JFrame implements ActionListener { JButton btn_open; JButton btn_save; JButton btn_clear; JTextField file_path; JTextArea stuff; public FileIO() { super("FILE IO Example"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout bag_layout = new GridBagLayout(); setLayout(bag_layout); GridBagConstraints c = new GridBagConstraints(); //TextField file_path file_path = new JTextField(40); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridwidth = 4; c.gridy = 0; c.gridheight = 1; c.weightx = 1.0; add(file_path, c); //TextArea stuff and JScrollPane scroll stuff = new JTextArea(40, 40); JScrollPane scroll = new JScrollPane(stuff, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); c.fill = GridBagConstraints.BOTH; c.gridy++; c.gridheight = 4; c.weighty = 1.0; add(scroll, c); //JButton btn_open btn_open = new JButton("Open File"); btn_open.addActionListener(this); c.fill = GridBagConstraints.HORIZONTAL; c.gridy = 5; c.gridheight = 1; c.gridwidth = 1; c.weightx = 1.0; c.weighty = 0.0; add(btn_open, c); //JButton btn_save btn_save = new JButton("Save File"); btn_save.addActionListener(this); c.gridx++; add(btn_save, c); //JButton btn_clear btn_clear = new JButton("Clear"); btn_clear.addActionListener(this); c.gridx++; add(btn_clear, c); setVisible(true); } //Methods private void Read_File() { File input = new File(file_path.getText()); FileReader reader; JDialog msg; char[] lines = new char[80]; try { reader = new FileReader(input); while(reader.read(lines) != -1) { stuff.append(new String(lines)); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); msg = new JOptionPane("FILE NOT FOUND ERROR", JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE"); msg.setVisible(true); } catch (IOException e) { e.printStackTrace(); msg = new JOptionPane("FILE IO ERROR", JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE"); msg.setVisible(true); } } private void Write_File() { File output = new File(file_path.getText()); FileWriter w; JDialog msg; try { w = new FileWriter(output); stuff.write(w); w.close(); } catch (IOException e) { e.printStackTrace(); msg = new JOptionPane("FILE IO ERROR", JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE"); msg.setVisible(true); } } //ActionListener public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if(source == btn_open) { Read_File(); } else if(source == btn_save) { Write_File(); } else if(source == btn_clear) { stuff.setText(""); } } //Main public static void main(String[] args) { new FileIO(); } }