I am displaying the contents of a textfile in a textArea. I want that when i edit this text in the textArea it is automatically saved in the textfile. I have wriitten this code, but the problem is that when i edit the text in the text Area it isn't saving it on the textfile. What do i have wrong? Thanks a lot.
Code:
import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JDialog;
import javax.swing.JTextArea;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.util.Calendar;
import java.awt.image.*;
import javax.imageio.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.FileNotFoundException;
public class Rock_Notes_Editable {
private JTextArea textArea;
public Rock_Notes_Editable() {
super();
try{
JDialog dialog = new JDialog();
dialog.getContentPane().setLayout(new BorderLayout(10, 10));
Test4Rx imagePanel = new Test4Rx("rocks.gif");
dialog.add(imagePanel, "North");
dialog.add(new Test4Rx("rocks.gif"));
//
String record = null;
File file = new File("rock.txt");
FileInputStream fileInput = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fileInput);
String string = dis.readLine();
textArea = new JTextArea();
while(string != null){
textArea.append(string + "\n");
string = dis.readLine();
textArea.setBackground(Color.pink);
textArea.setFont(new Font("Times New Roman", Font.BOLD, 14));
textArea.setEditable(true);
textArea.setForeground(Color.blue);
BufferedWriter w = new BufferedWriter(new FileWriter("rock.txt", true));
try {
textArea.write(w);
} finally {
w.close();
}
}
dialog.getContentPane().add(BorderLayout.CENTER, textArea);
dialog.setTitle("Geography Notes");
dialog.setSize(800, 900);
dialog.setVisible(true);
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Rock_Notes_Editable();
}
}
Comment