···
package com.shiyanlou.fileEditor;
import java.awt.;//abstract window tookit抽象窗口工具包
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;
// 定义一个私有的整型变量记录目录层次数,其初始值为0
private int level = 0;
public FileEditor() {
this.init();
// TODO Auto-generated constructor stub
}
public void init() {
// 设置窗口标题, 父类的setTile
this.setTitle("Editor");
// 设置组件大小 窗口组件
this.setBounds(300, 50, 600, 650);
// 创建一个选择框对象
selectField = new JTextField(40);// 设置文本域大小
// 创建一个选择按钮对象
openFileBtn = new JButton("Browse");// 调用构造函数, 设置按钮显示文本
// editArea = new JTextArea();
// 为创建的按钮对象添加监听事件
openFileBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 若监听到按钮点击事件, 则将层级记录数重置为0
// 获取选择文本域的string字符串
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 scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
// 滚动面板添加文本编辑区组件
scrollPane.add(editArea);
this.add(scrollPane, 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() {
// TODO Auto-generated method stub
// java类默认文件选择窗口
FileDialog fileDialog = new FileDialog(this, "Save File");
// 设置需要保存文件的后缀
fileDialog.setFile("unitiled.txt");
// 设置为"保存"模式
fileDialog.setMode(FileDialog.SAVE);
// 设置窗口是否可见
fileDialog.setVisible(true);
// 获取文件名
String fileName = fileDialog.getFile();
// 获取文件当前目录
String dir = fileDialog.getDirectory();
// 根据目录名、文件名创建一个文件,即要保存的目录文件
// File.separator为文件默认分隔符
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.print(str);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
pw.close();
}
}
private void openDirOrFile(String absolutePath) {
// TODO Auto-generated method stub
// 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);
}
// 关闭字符流
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void showDir(File directory) {
// TODO Auto-generated method stub
// 获得当前目录
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) {
// TODO Auto-generated method stub
new FileEditor();
}
}
···