简单记事本及目录树形图的Java实现

简介:

用Java实现一个简单的记事本,使其可以通过给定文件的绝对路径打开文件,当该文件名是目录时,则打开一个目录并浏览该目录下的文件结构,即目录树形图。

实现原理也不难,如下:

1、判断给定的绝对路径是否为文件,若为文件则直接在编辑区中显示文件里面的内容。

2、当绝对路径是目录时,则进入该目录,遍历目录内的每一个文件,并递归地打印出该目录的树形图。

3、保存文件是=时,则打开文件对话框,获取对话框当前的目录路径,以及对话框中用户输入的“文件名”,据此新建一个File对象,将编辑区中的内容写入到新建的文件中,即实现了保存文件的功能。 

下面是实现之后的界面:

 

 代码实现如下:

 
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.io.*;  
  4. import javax.swing.*;  
  5.  
  6. public class FileEditor extends JFrame {  
  7.  
  8.     private JTextField selectField; //文件的绝对路径文本域  
  9.     private JTextArea editArea;     //编辑区  
  10.     private JButton saveBtn;        //“保存”按钮  
  11.     private JButton openFileBtn;    //“浏览”按钮  
  12.  
  13.     private int level = 0;          //记录目录层次数  
  14.  
  15.     public FileEditor() {  
  16.         this.init();  
  17.     }  
  18.  
  19.     private void init() {  
  20.         this.setTitle("简单记事本");  
  21.         this.setBounds(30050600650);  
  22.  
  23.         /*  
  24.          * 面板的北边,即路径输入域、浏览按钮  
  25.          */ 
  26.         selectField = new JTextField(40);  
  27.         openFileBtn = new JButton("浏览");  
  28.         openFileBtn.addActionListener(new ActionListener() {  
  29.             public void actionPerformed(ActionEvent ae) {  
  30.                 FileEditor.this.level = 0;  
  31.                 String path = selectField.getText();  
  32.                 // 浏览目录或者文件  
  33.                 openDirOrFile(path.replaceAll("/""\\"));  
  34.             }  
  35.         });  
  36.         JPanel upPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));  
  37.         upPanel.setBackground(Color.CYAN);  
  38.         upPanel.add(selectField);  
  39.         upPanel.add(openFileBtn);  
  40.         this.add(upPanel, BorderLayout.NORTH);  
  41.  
  42.         /*  
  43.          * 文本编辑区  
  44.          */ 
  45.         editArea = new JTextArea();  
  46.         ScrollPane scollPanel = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);  
  47.         scollPanel.add(editArea);  
  48.         this.add(scollPanel, BorderLayout.CENTER);  
  49.  
  50.         /*  
  51.          * 保存按钮  
  52.          */ 
  53.         saveBtn = new JButton("保存");  
  54.         saveBtn.addActionListener(new ActionListener() {  
  55.             public void actionPerformed(ActionEvent ae) {  
  56.                 // 保存  
  57.                 saveFile();  
  58.             }  
  59.         });  
  60.         JPanel southPanel = new JPanel();  
  61.         southPanel.setBackground(Color.green);  
  62.         southPanel.add(saveBtn);  
  63.         this.add(southPanel, BorderLayout.SOUTH);  
  64.  
  65.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  66.         this.setVisible(true);  
  67.     }  
  68.  
  69.     /**  
  70.      * 保存文件  
  71.      */ 
  72.     private void saveFile() {  
  73.         FileDialog fd = new FileDialog(this"保存文件");  
  74.         fd.setFile("*.java");  
  75.         //设置为“保存”模式  
  76.         fd.setMode(FileDialog.SAVE);  
  77.         fd.setVisible(true);  
  78.         //获取文件名  
  79.         String fileName = fd.getFile();  
  80.         //获取对话框的当前目录  
  81.         String dir = fd.getDirectory();  
  82.         //根据目录名、文件名创建一个文件,即要保存的目标文件  
  83.         File newFile = new File(dir + File.separator + fileName);  
  84.         PrintWriter pw = null;  
  85.         try {  
  86.             pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(  
  87.                     newFile)));  
  88.  
  89.             String str = editArea.getText();  
  90.             pw.println(str);  
  91.             pw.flush();  
  92.         } catch (IOException e) {  
  93.             e.printStackTrace();  
  94.         } finally {  
  95.             pw.close();  
  96.         }  
  97.     }  
  98.  
  99.     /**  
  100.      * 打开目录或文件  
  101.      *   
  102.      * @param absolutePath : 指定目录或文件的绝对路径名  
  103.      */ 
  104.     private void openDirOrFile(String absolutePath) {  
  105.         File file = new File(absolutePath);  
  106.  
  107.         if (!(file.exists())) {  
  108.             editArea.setText("文件不存在!");  
  109.         } else if (file.isDirectory()) {  
  110.             editArea.setText(null);  
  111.             showDir(file);  
  112.         } else if (file.isFile()) {  
  113.             try {  
  114.                 FileInputStream fis = new FileInputStream(file);  
  115.                 BufferedReader br = new BufferedReader(new InputStreamReader(  
  116.                         fis));  
  117.                 String str = null;  
  118.                 editArea.setText(null);  
  119.                 while ((str = br.readLine()) != null) {  
  120.                     editArea.append(str + "\r\n");  
  121.                 }  
  122.                 br.close();  
  123.             } catch (IOException e) {  
  124.                 e.printStackTrace();  
  125.             }  
  126.         }  
  127.  
  128.     }  
  129.  
  130.     /**  
  131.      * 浏览目录,建立树形图  
  132.      *   
  133.      * @param directory :需要打开的目录  
  134.      */ 
  135.     private void showDir(File directory) {  
  136.         File[] files = directory.listFiles();  
  137.         int len = files.length;  
  138.         for (int i = 0; i < len; i++) {  
  139.             if (files[i].isDirectory()) {  
  140.                 for (int j = 0; j < this.level; j++) {  
  141.                     editArea.append("  ");  
  142.                 }  
  143.                 editArea.append(this.level + 1 + "  +" + files[i].getName() + " 文件夹\r\n");  
  144.                 this.level++;  
  145.                 showDir(files[i]);  
  146.             } else if(files[i].isFile()){  
  147.                 for(int j = 0; j < this.level + 2; j++) {  
  148.                     editArea.append(" ");  
  149.                 }  
  150.                 editArea.append(this.level + " ┝ ┈" + files[i].getAbsolutePath() + "\r\n");  
  151.             }  
  152.         }  
  153.     }  
  154.  
  155.     /**  
  156.      * 测试  
  157.      * @param args  
  158.      */ 
  159.     public static void main(String[] args) {  
  160.         new FileEditor();  
  161.     }  

测试:

1、打印一个目录的树形图,如下: 


2、打开一个文本文件,如下:

 

 3、保存文件,如下: 

 

至此,一个简单记事本就完成了。但是,该记事本并没有实现菜单栏等功能。



本文转自 xxxx66yyyy 51CTO博客,原文链接:http://blog.51cto.com/haolloyin/336196,如需转载请自行联系原作者

相关文章
|
3月前
|
存储 Java
1038. 从二叉搜索树到更大和树 --力扣 --JAVA
给定一个二叉搜索树 root (BST),请将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。 提醒一下, 二叉搜索树 满足下列约束条件: 节点的左子树仅包含键 小于 节点键的节点。 节点的右子树仅包含键 大于 节点键的节点。 左右子树也必须是二叉搜索树。
37 1
|
5月前
|
Java
java读取本地目录的文件转换为list
java读取本地目录的文件转换为list
88 0
|
6月前
|
JSON Java 数据格式
Java读取resource目录下的json文件
Java读取resource目录下的json文件
469 0
|
7月前
|
Java
Java记事本编写HelloWorld程序
Java记事本编写HelloWorld程序
85 0
|
3月前
|
C++ Java 容器
【Java每日一练】总目录(2023.3.11~5.18)共69篇
【Java每日一练】总目录(2023.3.11~5.18)共69篇
165 0
【Java每日一练】总目录(2023.3.11~5.18)共69篇
|
2月前
|
Java
AVL树(Java)
AVL树(Java)
29 0
|
3月前
|
Java
数据结构 AVL树概念以及实现插入的功能(含Java代码实现)
数据结构 AVL树概念以及实现插入的功能(含Java代码实现)
54 0
|
3月前
|
Java
基于Java Swing实现的日历记事本系统【源码+报告文档】
基于Java Swing实现的日历记事本系统【源码+报告文档】
|
4月前
|
Java API Maven
Java获取当前项目下的文件或目录物理地址System.getProperty(“user.dir“)
Java获取当前项目下的文件或目录物理地址System.getProperty(“user.dir“)
|
6月前
|
Java
Java 创建文件自动新增父目录、查询目录文件、删除文件目录下面的文件
要处理文件保存和删除的操作,记录一下以免遗忘: 1、创建文件,并且自动创建父目录 2、删除目录下面的所有文件
102 0