国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java實(shí)現(xiàn)查找文本內(nèi)容替換功能示例

java實(shí)現(xiàn)查找文本內(nèi)容替換功能示例

2019-11-11 13:32java教程網(wǎng) JAVA教程

文本替換幾乎是所有文本編輯器都支持的功能,但是要限制在編輯其中才可以執(zhí)行該功能。本實(shí)例實(shí)現(xiàn)了制定文本文件的內(nèi)容替換,并且不需要再編輯其中打開文本文件

思路:

先看視圖層,要有一個(gè)JButton控件用來選擇文件,一個(gè)JTextField控件顯示選中文件的絕對路徑,一個(gè)JLabel控件提示用戶輸入搜索文本,一個(gè)JLabel控件提示用戶輸入替換后的文本,一個(gè)JTextField標(biāo)簽供用戶輸入要搜索的文本,一個(gè)JTextField標(biāo)簽供用戶輸入替換后的文本,一個(gè)JButton控件執(zhí)行替換,一個(gè)JButton控件用來打開修改后的文件。
對于選擇文件按鈕,使用JButton類的addActionListener()方法為其綁定事件,在該事件中定義actionPerformed()函數(shù),在該函數(shù)體中調(diào)用選擇文件的方法。
在選擇文件方法中,首先創(chuàng)建JFileChooser文件選擇器,使用JFileChooser類的setFileFilter()方法創(chuàng)建文件擴(kuò)展名過濾器,再使用JFileChooser類的setFileSelectionMode()方法設(shè)置文件選擇模式為文件,通過JFileChooser類的showOpenDialog()方法顯示文件打開對話框,確定用戶按下打開按鈕,而非取消按鈕后,通過JFileChooser類的getSelectedFile()方法獲取用戶選擇的文件對象,使用JTextField類的setText()方法顯示文件信息到文本框。
對于替換按鈕,同選擇文件按鈕,使用JButton類的addActionListener()方法為其綁定事件,在該事件中定義actionPerformed()函數(shù),在該函數(shù)體中調(diào)用替換文本的方法。
在替換文本方法中,首先使用TextField類的getText()方法獲取要搜索的文本和要替換成的文本,若搜索文本不為空則嘗試創(chuàng)建FileReader文件輸入流和char緩沖字符數(shù)組以及StringBuilder字符串構(gòu)建器,在while()循環(huán)中使用FileReader類的read()方法讀取文件內(nèi)容到字符串構(gòu)建器,讀取完畢后使用FileReader類的close()方法關(guān)閉輸入流,使用StringBuilder類的replace()方法從構(gòu)建器中生成字符串,并替換搜索文本,然后創(chuàng)建FileWriter文件輸出流,使用FileWriter類的write()方法把替換完成的字符串寫入文件內(nèi),然后使用FileWriter類的close()方法關(guān)閉輸出流,然后依次捕獲FileNotFoundException異常和IOException異常,最后使用JOptionPane類的showMessageDialog()方法提示用戶替換完成。
對于打開文件按鈕,使用JButton類的addActionListener()方法為其綁定事件,在該事件中定義actionPerformed()函數(shù),在該函數(shù)體中調(diào)用打開文件的方法。
在打開文件方法中嘗試使用 Desktop.getDesktop().edit(file);,并捕獲IOException異常。
代碼如下:

 

復(fù)制代碼代碼如下:

import java.awt.BorderLayout;

public class ReplaceFileText extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 8674569541853793419L;
    private JPanel contentPane;
    private JTextField fileField;
    private JTextField searchTextField;
    private JTextField replaceTextField;
    private File file;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ReplaceFileText frame = new ReplaceFileText();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ReplaceFileText() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 501, 184);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(10, 91));
        contentPane.add(panel, BorderLayout.CENTER);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 81, 0, 0, 66, 0 };
        gbl_panel.rowHeights = new int[] { 23, 0, 0, 0, 0 };
        gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
                Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
                Double.MIN_VALUE };
        panel.setLayout(gbl_panel);

        JButton button = new JButton("選擇文件");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        GridBagConstraints gbc_button = new GridBagConstraints();
        gbc_button.anchor = GridBagConstraints.NORTHWEST;
        gbc_button.insets = new Insets(0, 0, 5, 5);
        gbc_button.gridx = 0;
        gbc_button.gridy = 0;
        panel.add(button, gbc_button);

        fileField = new JTextField();
        fileField.setEditable(false);
        GridBagConstraints gbc_fileField = new GridBagConstraints();
        gbc_fileField.gridwidth = 3;
        gbc_fileField.insets = new Insets(0, 0, 5, 0);
        gbc_fileField.fill = GridBagConstraints.HORIZONTAL;
        gbc_fileField.gridx = 1;
        gbc_fileField.gridy = 0;
        panel.add(fileField, gbc_fileField);
        fileField.setColumns(10);

        JLabel label = new JLabel("搜索文本:");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.anchor = GridBagConstraints.EAST;
        gbc_label.insets = new Insets(0, 0, 5, 5);
        gbc_label.gridx = 0;
        gbc_label.gridy = 1;
        panel.add(label, gbc_label);

        searchTextField = new JTextField();
        GridBagConstraints gbc_searchTextField = new GridBagConstraints();
        gbc_searchTextField.gridwidth = 3;
        gbc_searchTextField.insets = new Insets(0, 0, 5, 0);
        gbc_searchTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_searchTextField.gridx = 1;
        gbc_searchTextField.gridy = 1;
        panel.add(searchTextField, gbc_searchTextField);
        searchTextField.setColumns(10);

        JLabel label_1 = new JLabel("替換為:");
        GridBagConstraints gbc_label_1 = new GridBagConstraints();
        gbc_label_1.anchor = GridBagConstraints.EAST;
        gbc_label_1.insets = new Insets(0, 0, 5, 5);
        gbc_label_1.gridx = 0;
        gbc_label_1.gridy = 2;
        panel.add(label_1, gbc_label_1);

        replaceTextField = new JTextField();
        GridBagConstraints gbc_replaceTextField = new GridBagConstraints();
        gbc_replaceTextField.gridwidth = 3;
        gbc_replaceTextField.insets = new Insets(0, 0, 5, 0);
        gbc_replaceTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_replaceTextField.gridx = 1;
        gbc_replaceTextField.gridy = 2;
        panel.add(replaceTextField, gbc_replaceTextField);
        replaceTextField.setColumns(10);

        JPanel panel_1 = new JPanel();
        GridBagConstraints gbc_panel_1 = new GridBagConstraints();
        gbc_panel_1.gridwidth = 4;
        gbc_panel_1.fill = GridBagConstraints.BOTH;
        gbc_panel_1.gridx = 0;
        gbc_panel_1.gridy = 3;
        panel.add(panel_1, gbc_panel_1);

        JButton replaceButton = new JButton("替換");
        replaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_replaceButton_actionPerformed(e);
            }
        });
        panel_1.add(replaceButton);

        JButton openfileButton = new JButton("打開文件");
        openfileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_2_actionPerformed(e);
            }
        });
        panel_1.add(openfileButton);
    }

    /**
     * 選擇文件按鈕事件處理方法
     * 
     * @param e
     */
    protected void do_button_actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser("./");// 創(chuàng)建文件選擇器
        // 設(shè)置文件擴(kuò)展名過濾器
        chooser.setFileFilter(new FileNameExtensionFilter("文本文件", "txt",
                "java", "php", "html", "htm"));
        // 設(shè)置文件選擇模式
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // 顯示文件打開對話框
        int option = chooser.showOpenDialog(this);
        // 確定用戶按下打開按鈕,而非取消按鈕
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        // 獲取用戶選擇的文件對象
        file = chooser.getSelectedFile();
        // 顯示文件信息到文本框
        fileField.setText(file.toString());
    }

    /**
     * 替換按鈕的事件處理方法
     * 
     * @param e
     */
    protected void do_replaceButton_actionPerformed(ActionEvent event) {
        String searchText = searchTextField.getText();// 獲取搜索文本
        String replaceText = replaceTextField.getText();// 獲取替換文本
        if (searchText.isEmpty())
            return;
        try {
            FileReader fis = new FileReader(file);// 創(chuàng)建文件輸入流
            char[] data = new char[1024];// 創(chuàng)建緩沖字符數(shù)組
            int rn = 0;
            StringBuilder sb = new StringBuilder();// 創(chuàng)建字符串構(gòu)建器
            while ((rn = fis.read(data)) > 0) {// 讀取文件內(nèi)容到字符串構(gòu)建器
                String str = String.valueOf(data, 0, rn);
                sb.append(str);
            }
            fis.close();// 關(guān)閉輸入流
            // 從構(gòu)建器中生成字符串,并替換搜索文本
            String str = sb.toString().replace(searchText, replaceText);
            FileWriter fout = new FileWriter(file);// 創(chuàng)建文件輸出流
            fout.write(str.toCharArray());// 把替換完成的字符串寫入文件內(nèi)
            fout.close();// 關(guān)閉輸出流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JOptionPane.showMessageDialog(null, "替換完成");
    }

    /**
     * 打開文件按鈕的事件處理方法。
     * 
     * @param e
     */
    protected void do_button_2_actionPerformed(ActionEvent e) {
        try {
            if (file == null)
                return;
            Desktop.getDesktop().edit(file);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}



java實(shí)現(xiàn)查找文本內(nèi)容替換功能示例

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 激情欧美一区二区三区中文字幕 | 国产精品视频久久久 | 亚洲精品久久久久久久久久久 | 无毒黄网| 亚洲综合二 | 成人欧美一区二区 | 久久天天 | 一区二区免费看 | 亚洲精品久久久久久一区二区 | 欧美一区二区在线视频 | 在线 亚洲 欧美 | 精品免费视频 | 精品一区二区电影 | 久久久久久国产精品 | 丁香久久 | 国产成人精品一区二区三区 | 欧美第5页 | 亚洲激情视频在线 | 久久久久久久91 | 夜夜摸夜夜操 | 精品免费国产一区二区三区 | 99国产精品久久久久久久成人热 | 羞羞影院| 国产欧美日韩一区 | 亚洲欧美精品一区 | 男女深夜视频 | 中文字幕日本一区二区 | 日本 欧美 国产 | 成人高清免费观看 | 久久一区 | 人人爽人人爽人人片av | 黄色99| 久久99国产精品免费网站 | 久久久亚洲| 国产艹 | 男人天堂网站 | 日韩av免费在线观看 | 成av在线| 欧美午夜精品久久久 | 国产在线不卡一区 | 国产目拍亚洲精品99久久精品 |