Hello,
Can somebody check this code, cause I literally cant save or load anything ?! :S
package pkgtry.catchexercises;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FileDemo1 extends JFrame implements ActionListener{
private TextField inputTextArea = new TextField(20);
private TextField outputTextArea = new TextField(20);
private JButton saveButton = new JButton("save");
private JButton loadButton = new JButton("load");
private FileWriter outFile;
public static void main(String[] args){
new FileDemo1();
public FileDemo1(){
setSize(350, 300);
setTitle("File Output Demo");
add("East", inputTextArea);
add("West", outputTextArea);
JPanel bottom = new JPanel();
add("South", bottom);
bottom.add(loadButton);
bottom.add(saveButton);
loadButton.addActionListener(this);
saveButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==saveButton){
try{
outFile = new FileWriter("testout.txt", true);
outFile.write(inputTextArea.getText());
outFile.close();
}catch(IOException e){
System.err.println("File Error:"+e);
System.exit(1);
}
}
if(evt.getSource()==loadButton){
try{
inFile = new BufferedReader(new FileReader("FileDemo1.txt"));
outFile.write(inputTextArea.getText());
outArea.setText("");
String line;
while(line = inFile.readLine())! =null){
outputArea.append(line + "\n");
}
inFile.close();
}catch(IOException e){
JOptionPane.showMessageDialog(this, "File Not Found");
}
}
}