-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempProgram.java
50 lines (45 loc) · 1.52 KB
/
TempProgram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class FileManager {
private JTextArea textArea;
public FileManager(JTextArea textArea) {
this.textArea = textArea;
}
// Open file
public void openFile() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
textArea.setText(content.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Save file
public void saveFile() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(textArea.getText());
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Close file
public void closeFile() {
textArea.setText("");
}
}