Java source--to find and replace the contents of the file (start writing interface slightly) __ source code

Source: Internet
Author: User
Tags gettext stringbuffer

The problem is: the path to the keyboard input file, find content, and replace content to find and replace the contents of the file for the specified path. ”

Long time no write interface, today familiar with the interface of the writing and listener operation.

The problem is not very difficult in itself, and the focus should be on the handling of various possible errors during file processing. So write code needs to be very careful, and the error is handled in place.

In addition, the interface is written very standard, the annotation is very in place, can also be taken away for his use directly.

On the source.


Replace.java

Package com.fuxuemingzhu.replace.main; Import java.awt.color;//color Import java.awt.font;//font Import java.awt.event.actionevent;//event handling Import
java.awt.event.actionlistener;//incident monitoring 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;//button Import javax.swing.jframe;//frame import javax.swing.jlabel;//label Import javax.swing.joptionpane;//message window Import javax.swing.jpanel;//Panel import javax.swing.jtextfield;//text Box/** * <p> * Le:replace * </p> * <p> * Description: Visual TXT content Replacement Program * </p> * @author Fuxuemingzhu * * * @ema Il fuxuemingzhu@163.com * * @date December 5, 2014 PM 6:27:50 */public class Replace extends JFrame {/** * Serialversio

	NUID * */private static final long serialversionuid = 1L;
	/** * Pnl_mian Main Panel * * Public JPanel Pnl_mian; /** * LBL_HELP Hint Panel * * PublIC JLabel lbl_help;
	/** * Lbl_find Find content hint * * public JLabel lbl_find;
	/** * Lbl_replace REPLACEMENT content hint * * public JLabel lbl_replace;
	/** * Lbl_path PATH hint * * public JLabel Lbl_path;
	/** * Txt_path Text Path Input * * public JTextField Txt_path;
	/** * Txt_find Lookup Content Input * * public JTextField txt_find;
	/** * Txt_replace Replacement Content Input * * public JTextField txt_replace;
	/** * btn_sub OK button/public JButton btn_sub;

	/** * Btn_reset Reset button * * Public JButton Btn_reset;
	/** * Paths File path String */public static string path;
	/** * Find Lookup content String */public static string found;

	/** * Replace replacement content String */public static string replace; /** * <p> * title:replace * </p> * <p> * Description: Construction method * </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 = newJTextField ();
		Btn_sub = new JButton ();
		Btn_reset = new JButton ();
	UserInit (); /** * <p> * title:userinit * </p> * <p> * Description: User interface Settings * </p> */PU  Blic void UserInit () {this.setdefaultcloseoperation (jframe.exit_on_close);/set closes the frame while closing the program This.setsize (400, 250); Sets the frame size to 500, this.setresizable (FALSE),//Setting the frame can not change the size this.settitle (find substitution);/Set the frame heading This.pnl_mian.setLayout (nu ll)//Setup Panel layout Management This.pnl_mian.setBackground (Color.cyan);//Set panel background color this.lbl_help.setText (find replace);//Set label title THIS.LBL _help.setfont (New Font ("Song Body", Font.Bold |
		Font.Italic, 14);//Set Label font this.lbl_help.setForeground (color.red);//Set Label font Color this.lbl_path.setText ("Text path:");
		This.lbl_find.setText ("Find text:");
		This.lbl_replace.setText ("Replace with:");
		This.btn_sub.setText ("OK");
		This.btn_reset.setText ("reset");
		This.lbl_help.setBounds (150, 25, 60, 20);/set label x coordinate 120,y coordinates 20, length 60, 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 (The new ActionListener ()//anonymous class implements the ActionListener interface {public void actionperformed (Actione
					Vent e) {btnsub_actionevent (e);
		}
				});
		This.btn_reset.setBounds (195, 160, 60, 20); This.btn_reset.addActionListener (The new ActionListener ()//anonymous class implements the ActionListener interface {public void actionperformed (actio
					Nevent e) {btnreset_actionevent (e);
		}
				});
		This.pnl_mian.add (LBL_HELP)//load tag to panel 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)//load panel to frame this.setvisible (trUE)//Setting frame can be marked}/** * <p> * title:btnsub_actionevent * </p> * <p> * Description: Click OK key action
		* </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," The file path cannot be empty.)
			"," error ", joptionpane.error_message);
		Return else if (Find.equals (")) {Joptionpane.showmessagedialog (null, the Lookup object cannot be empty.)
			"," error ", joptionpane.error_message);
		Return else if (Replace.equals (")) {Joptionpane.showmessagedialog (null," Replace cannot be empty.)
			"," error ", 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: Click reset button Operation *</p> * * @param e * */public void btnreset_actionevent (ActionEvent e) {txt_path.settext ("");
		Txt_find.settext ("");
	Txt_replace.settext (""); /** * <p> * title:changefile * </p> * <p> * Description: Read file * </p> * * @par  AM file * @throws Exception */public static void Changefile (file file) throws IOException {BufferedReader br
		= NULL; try {if (!file.exists ()) {Joptionpane.showmessagedialog (null, "file path is incorrect.)
				"," error ", 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) {//Replace operation here if (Tmp.contains (find)) {tmp = Tmp.replaceall (find, replace);
						Sbf.append (TMP);
						Sbf.append (System.getproperty ("Line.separator")); File re-write BufferedWriter bw = new BufferedWriter (new FileWriter (path));
						Bw.write (Sbf.tostring ()); Joptionpane.showmessagedialog (NULL, "The file contents have been replaced successfully.)
						"," ascertained ", joptionpane.yes_option);
					Bw.close (); } else {joptionpane.showmessagedialog (null, "file does not contain content to be replaced.)
					"," ascertained ", joptionpane.yes_option);
			} br.close (); The catch (IOException E1) {Joptionpane.showmessagedialog (null, "file read incorrectly.)
				"," error ", joptionpane.error_message);
				E1.printstacktrace ();
			Return } catch (FileNotFoundException E1) {Joptionpane.showmessagedialog (null, "file path is incorrect.)
			"," error ", joptionpane.error_message);
			E1.printstacktrace ();
		Return  }/** * <p> * title:main * </p> * <p> * Description:main method, program entry * </p> * *
	@param args * */public static void main (string[] args) {new Replace (); }
}


Here are a variety of interfaces, various shows. UH ~

The first is the normal running interface.


The processing when the content to be replaced is not found.


The processing when you enter an error path.


Processing when the input is empty.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.