1. 程式人生 > >java原始碼——對檔案內容的查詢和替換(開始寫介面咯)

java原始碼——對檔案內容的查詢和替換(開始寫介面咯)

問題是:“鍵盤輸入檔案的路徑、查詢內容和替換內容,對指定路徑的檔案的內容進行查詢和替換。”

好久沒寫介面了,今天熟悉一下介面的書寫和監聽器操作。

這個問題的本身不是很難,重點應該是檔案處理時的各種可能出現的錯誤進行處理。因此寫程式碼時需要非常細心,並且把錯誤處理到位。

另外,這個介面寫的很規範,註釋也都很到位,也可以直接拿走留作他用。

上原始碼。

Replace.java

package com.fuxuemingzhu.replace.main;

import java.awt.Color;//顏色
import java.awt.Font;//字型
import java.awt.event.ActionEvent;//事件處理
import java.awt.event.ActionListener;//事件監聽
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;//按鈕
import javax.swing.JFrame;//框架
import javax.swing.JLabel;//標籤
import javax.swing.JOptionPane;//訊息視窗
import javax.swing.JPanel;//面板
import javax.swing.JTextField;//文字框

/**
 * <p>
 * Title: Replace
 * </p>
 * <p>
 * Description:視覺化的txt的內容替換程式
 * </p>
 * 
 * @author fuxuemingzhu
 * 
 * @email 
[email protected]
* * @date 2014年12月5日 下午6:27:50 */ public class Replace extends JFrame { /** * serialVersionUID * */ private static final long serialVersionUID = 1L; /** * pnl_mian 主面板 */ public JPanel pnl_mian; /** * lbl_help 提示面板 */ public JLabel lbl_help; /** * lbl_find 查詢內容提示 */ public JLabel lbl_find; /** * lbl_replace 替換內容提示 */ public JLabel lbl_replace; /** * lbl_path 路徑提示 */ public JLabel lbl_path; /** * txt_path 文字路徑輸入 */ public JTextField txt_path; /** * txt_find 查詢內容輸入 */ public JTextField txt_find; /** * txt_replace 替換內容輸入 */ public JTextField txt_replace; /** * btn_sub 確定按鍵 */ public JButton btn_sub; /** * btn_reset 重置按鍵 */ public JButton btn_reset; /** * path 檔案路徑字串 */ public static String path; /** * find 查詢內容字串 */ public static String find; /** * replace 替換內容字串 */ public static String replace; /** * <p> * Title:Replace * </p> * <p> * Description:構造方法 * </p> */ public Replace() { pnl_mian = new JPanel(); lbl_help = new JLabel(); lbl_find = new JLabel(); lbl_replace = new JLabel(); lbl_path = new JLabel(); txt_path = new JTextField(); txt_find = new JTextField(); txt_replace = new JTextField(); btn_sub = new JButton(); btn_reset = new JButton(); userInit(); } /** * <p> * Title: userInit * </p> * <p> * Description:使用者介面設定 * </p> * */ public void userInit() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設定關閉框架的同時結束程式 this.setSize(400, 250);// 設定框架大小為長500,寬200 this.setResizable(false);// 設定框架不可以改變大小 this.setTitle("查詢替換");// 設定框架標題 this.pnl_mian.setLayout(null);// 設定面板佈局管理 this.pnl_mian.setBackground(Color.cyan);// 設定面板背景顏色 this.lbl_help.setText("查詢替換");// 設定標籤標題 this.lbl_help.setFont(new Font("宋體", Font.BOLD | Font.ITALIC, 14));// 設定標籤字型 this.lbl_help.setForeground(Color.RED);// 設定標籤字型顏色 this.lbl_path.setText("文字路徑:"); this.lbl_find.setText("查詢文字:"); this.lbl_replace.setText("替換為:"); this.btn_sub.setText("確定"); this.btn_reset.setText("重置"); this.lbl_help.setBounds(150, 25, 60, 20);// 設定標籤x座標120,y座標20,長60,寬20 this.lbl_path.setBounds(50, 50, 60, 20); this.lbl_find.setBounds(50, 80, 60, 20); this.lbl_replace.setBounds(50, 110, 60, 25); this.txt_path.setBounds(110, 50, 200, 20); this.txt_find.setBounds(110, 80, 200, 20); this.txt_replace.setBounds(110, 110, 200, 20); this.btn_sub.setBounds(105, 160, 60, 20); this.btn_sub.addActionListener(new ActionListener()// 匿名類實現ActionListener介面 { public void actionPerformed(ActionEvent e) { btnsub_ActionEvent(e); } }); this.btn_reset.setBounds(195, 160, 60, 20); this.btn_reset.addActionListener(new ActionListener()// 匿名類實現ActionListener介面 { public void actionPerformed(ActionEvent e) { btnreset_ActionEvent(e); } }); this.pnl_mian.add(lbl_help);// 載入標籤到面板 this.pnl_mian.add(lbl_path); this.pnl_mian.add(lbl_find); this.pnl_mian.add(lbl_replace); this.pnl_mian.add(txt_path); this.pnl_mian.add(txt_find); this.pnl_mian.add(txt_replace); this.pnl_mian.add(btn_sub); this.pnl_mian.add(btn_reset); this.add(pnl_mian);// 載入面板到框架 this.setVisible(true);// 設定框架可顯 } /** * <p> * Title: btnsub_ActionEvent * </p> * <p> * Description:點選確定鍵的操作 * </p> * * @param e * */ public void btnsub_ActionEvent(ActionEvent e) { path = txt_path.getText(); find = txt_find.getText(); replace = String.valueOf(txt_replace.getText()); if (path.equals("")) { JOptionPane.showMessageDialog(null, "檔案路徑不能為空!", "錯誤", JOptionPane.ERROR_MESSAGE); return; } else if (find.equals("")) { JOptionPane.showMessageDialog(null, "查詢物件不能為空!", "錯誤", JOptionPane.ERROR_MESSAGE); return; } else if (replace.equals("")) { JOptionPane.showMessageDialog(null, "替換內容不能為空!", "錯誤", JOptionPane.ERROR_MESSAGE); return; } else { File file = new File(path); try { changeFile(file); } catch (Exception e1) { e1.printStackTrace(); return; } this.dispose(); } } /** * <p> * Title: btnreset_ActionEvent * </p> * <p> * Description:點選重置鍵的操作 * </p> * * @param e * */ public void btnreset_ActionEvent(ActionEvent e) { txt_path.setText(""); txt_find.setText(""); txt_replace.setText(""); } /** * <p> * Title: changeFile * </p> * <p> * Description:讀取檔案 * </p> * * @param file * @throws Exception * */ public static void changeFile(File file) throws IOException { BufferedReader br = null; try { if (!file.exists()) { JOptionPane.showMessageDialog(null, "檔案路徑有誤!", "錯誤", JOptionPane.ERROR_MESSAGE); return; } FileReader fileReader = new FileReader(file); br = new BufferedReader(fileReader); StringBuffer sbf = new StringBuffer(""); try { for (String tmp = null; (tmp = br.readLine()) != null; tmp = null) { // 在這裡做替換操作 if (tmp.contains(find)) { tmp = tmp.replaceAll(find, replace); sbf.append(tmp); sbf.append(System.getProperty("line.separator")); // 檔案的重新寫入 BufferedWriter bw = new BufferedWriter(new FileWriter( path)); bw.write(sbf.toString()); JOptionPane.showMessageDialog(null, "檔案內容已經替換成功!", "確定", JOptionPane.YES_OPTION); bw.close(); } else { JOptionPane.showMessageDialog(null, "檔案中不含有要替換的內容!", "確定", JOptionPane.YES_OPTION); } } br.close(); } catch (IOException e1) { JOptionPane.showMessageDialog(null, "檔案讀取有誤!", "錯誤", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); return; } } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "檔案路徑有誤!", "錯誤", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); return; } } /** * <p> * Title: main * </p> * <p> * Description:main方法,程式的入口 * </p> * * @param args * */ public static void main(String[] args) { new Replace(); } }
下面是各種介面,各種秀。嗯~

首先是正常執行介面。

查詢不到要替換的內容時的處理。

輸入錯誤路徑時的處理。

輸入內容為空時的處理。



相關推薦

no