java实现记事本
来源:互联网 发布:长虹大数据公司 编辑:程序博客网 时间:2024/06/06 18:24
首先,制作记事本的可视化界面,用于记事本的编辑;其次,记事本文件的操作如打开、关闭、保存;最后,记事本文件内容的读写涉及IO操作。
package fileeditor;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileEditor extends JFrame {
private JTextField selectField;
private JTextArea editArea;
private JButton saveBtn;
private JButton openFileBtn;
private int level = 0;
public FileEditor() {
this.init();
}
private void init() {
this.setTitle("Editor");
this.setBounds(300, 50, 600, 650);
selectField = new JTextField(40);
openFileBtn = new JButton("Browse");
openFileBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
FileEditor.this.level = 0;
String path = selectField.getText();
openDirOrFile(path.replaceAll("//", "\\\\"));
}
});
JPanel upPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
upPanel.setBackground(Color.CYAN);
upPanel.add(selectField);
upPanel.add(openFileBtn);
this.add(upPanel, BorderLayout.NORTH);
editArea = new JTextArea();
ScrollPane scollPanel = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
scollPanel.add(editArea);
this.add(scollPanel, BorderLayout.CENTER);
saveBtn = new JButton("Save");
saveBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
saveFile();
}
});
JPanel southPanel = new JPanel();
southPanel.setBackground(Color.green);
southPanel.add(saveBtn);
this.add(southPanel, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void saveFile() {
FileDialog fd = new FileDialog(this, "Save File");
fd.setFile("untitled.txt");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
String fileName = fd.getFile();
String dir = fd.getDirectory();
File newFile = new File(dir + File.separator + fileName);
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
newFile)));
String str = editArea.getText();
pw.println(str);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
pw.close();
}
}
private void openDirOrFile(String absolutePath) {
File file = new File(absolutePath);
if (!(file.exists())) {
editArea.setText("The file does not exist!");
} else if (file.isDirectory()) {
editArea.setText(null);
showDir(file);
} else if (file.isFile()) {
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
String str = null;
editArea.setText(null);
while ((str = br.readLine()) != null) {
editArea.append(str + "\r\n");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void showDir(File directory)
{
File[] files = directory.listFiles();
int len = files.length;
for(int i = 0; i < len; i++)
{
if(files[i].isDirectory())
{
for(int j = 0; j < this.level; j++)
{
editArea.append(" ");
}
editArea.append("|-- "+ files[i].getName() + " (Folder)\r\n");
this.level++;
showDir(files[i]);
this.level--;
}
else if(files[i].isFile())
{
for(int j = 0; j < this.level; j++)
{
editArea.append(" ");
}
editArea.append("|-- " + files[i].getAbsolutePath() + "\r\n");
}
}
}
public static void main(String[] args) {
new FileEditor();
}
}
- Java 实现记事本
- Java简单记事本实现
- Java实现记事本
- java实现记事本
- java实现记事本
- java实现记事本
- Java记事本实现
- Java实现记事本
- java Swing 实现记事本
- java GUI实现记事本
- java实现图形记事本
- java实现记事本之Java记事本源码及实现
- java实现记事本之Java记事本源码及实现
- java实现记事本之Java记事本源码及实现
- Java实现记事本功能:JNotePad
- NoteBook Java实现记事本功能
- java实现我的记事本
- Java实现记事本知识小结
- uva 100 The 3n + 1 problem
- 转接IC SJXX串口扩展芯片
- 转载: Python--详解Python中re.sub 作者:Mrzhoug
- 利用synchronized解决哲学家问题
- 1007(数组,时间超限)
- java实现记事本
- /etc/inittab
- hdu5375 格雷码性质 dp
- 基于How To Tango With Django 1.9的重新实践(12)——Bootstrapping Rango
- Unity 3D中的内存管理
- L1-027. 出租
- python进行删除标点符号
- win7 64位安装wamp缺失vcruntime140.dll和api-ms-win-crt-runtime-l1-1-0.dll 等
- HDU 4886 TIANKENG’s restaurant(Ⅱ) (哈希)