Java 写个记事本 打开时能自动读取上一个打开的文件
时间: 2024-05-26 20:11:28 浏览: 18
可以使用Java的文件IO操作来实现读取上一个打开的文件。具体方法是,将最近一次打开的文件路径记录在一个配置文件中,每次进入记事本时读取该文件,如果存在该文件,则自动将其载入记事本中。你可以通过Java的FileInputStream和FileOutputStream来实现该功能。如果需要保存打开的文件路径,可以使用Java中的Properties类。
相关问题
Java 写个记事本 打开时能自动读取上一个打开的文件代码实现
您好!我是 CSDN 开发的 ChitGPT。
对于您的问题,您可以使用 Java 中的 FileWriter、BufferedReader 和 FileReader 来实现。具体步骤如下:
首先,在你的记事本程序中,你可以通过菜单或者按钮实现打开文件的功能,在这个事件中,你可以通过 FileReader 来读取上一个打开的文件。在 FileReader 中,你可以使用 BufferedReader 来按行读取文件内容。
接下来,你需要将读取到的文件内容设置为文本框中的内容,这可以通过将每一行文件内容添加到 StringBuilder 中,最后将 StringBuilder 转换为字符串,并使用 setText() 方法来显示到文本框中。
最后,使用 FileWriter 将修改后的内容写入到文件中,并将文件名保存在本地,以便下一次打开程序时可以自动读取之前的文件。
代码实现如下:
```
public class SimpleNotepad extends JFrame {
private JTextArea textArea;
private File file;
public SimpleNotepad() {
textArea = new JTextArea();
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(SimpleNotepad.this);
if (result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
try {
FileReader reader = new FileReader(file);
BufferedReader bReader = new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
String line;
while ((line = bReader.readLine()) != null) {
builder.append(line);
builder.append("\n");
}
textArea.setText(builder.toString());
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (file == null) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(SimpleNotepad.this);
if (result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
}
}
if (file != null) {
try {
FileWriter writer = new FileWriter(file);
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
fileMenu.add(saveMenuItem);
setJMenuBar(menuBar);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SimpleNotepad();
}
}
```
希望我的回答对您有所帮助!
用Java写的记事本如何让它在打开的时候默认打开一个文件
可以通过设置记事本的快捷方式打开某个指定文件来实现。具体步骤如下:
1. 找到记事本的快捷方式,一般在桌面或者开始菜单中。
2. 右键单击快捷方式,选择“属性”。
3. 在“目标”文本框中添加参数“记事本文件路径”,例如:C:\Users\You\Desktop\example.txt。
4. 点击“应用”和“确定”按钮保存更改。
这样,每次打开该快捷方式时,就会默认打开指定的文件了。同时要注意,相同的操作也适用于其他以记事本为基础的文本编辑器。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)