BUKA FILE (Membaca isi file .txt) :
{
JFileChooser fileChooser=new JFileChooser(“.”);
int choice=fileChooser.showOpenDialog(this);
if(choice==JFileChooser.APPROVE_OPTION)
{
FileReader fr = null;
try {
file = fileChooser.getSelectedFile();
setTitle(file.getPath());
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
textArea.setText(“”);
while (br.ready()) {
textArea.append(br.readLine() + “\n”);
}
}catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this,”Cannot find the file”);
}
catch (IOException ex) {
JOptionPane.showMessageDialog(this,”Cannot read the file data”);
}
}
}
SAVE FILE (Menyimpan file .txt) :
{
if(file!=null)
{
FileWriter fw = null;
try {
fw = new FileWriter(file);
fw.write(textArea.getText());
fw.flush();
isSaved=true;
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,”An error occured! Cannot Save”);
} finally {
try {
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
else
{
// saveAs();
}
}
CATATAN :
- nama palette (komponen) harap disesuaikan sendiri.
- kalau kesulitan silakan konsultasi, jangan “malu bertanya”.