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();   
    }   


0 0
原创粉丝点击