Find and replace the content of a text file in Java

Source: Internet
Author: User

Text replacement is supported by almost all text editors, but it must be edited to perform this function. In this example, the content of a text file is replaced, and you do not need to edit it to open the text file.

Ideas:

The Code is as follows:

Import java. awt. borderLayout; public class ReplaceFileText extends JFrame {/***/private static final long serialVersionUID = bytes; 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); setdefaclocloseoperation (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 ("select file"); button. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_button_actionreceivmed (e) ;}}); GridBagConstraints gbc_button = new GridBagConstraints (); response = gridbagconi NT. 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 (); encoding = 3; gbc_fileField.insets = new Insets (0, 0, 5, 0); gbc_fileField.fill = encoding. HORIZONTAL; gbc_fileField.gri Dx = 1; gbc_fileField.gridy = 0; panel. add (fileField, gbc_fileField); fileField. setColumns (10); JLabel label = new JLabel ("search text:"); 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 (); GridBagConstraint S 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 ("replace with:"); GridBagConstraints gbc_l Abel_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 outer (); outer = 3; outer = 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 (); records = 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 ("replace"); replaceButton. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_replacebutton_actionreceivmed (e) ;}}); panel_1.add (replaceButton ); JButton openfileButton = new JButton ("Open File"); openfileButton. addActionListener (new ActionListener () {public void actionreceivmed (ActionEve Nt e) {do_button_2_actionreceivmed (e) ;}}); panel_1.add (openfileButton );} /*** select file button event processing method ** @ param e */protected void do_button_actionreceivmed (ActionEvent e) {JFileChooser chooser = new JFileChooser (". /"); // create a file selector // set the file extension filter chooser. setFileFilter (new FileNameExtensionFilter ("text file", "txt", "java", "php", "html", "htm"); // set the file selection mode to chooser. setFileSelectionMode (JFileChooser. FILES_ON LY); // display the file opening dialog box int option = chooser. showOpenDialog (this); // confirm that the user presses the OPEN button instead of the cancel button if (option! = JFileChooser. APPROVE_OPTION) return; // obtain the selected file object file = chooser. getSelectedFile (); // display the file information to the fileField in the text box. setText (file. toString ();}/*** replacement button event handling method ** @ param e */protected void do_replacebutton_actionreceivmed (ActionEvent event) {String searchText = searchTextField. getText (); // obtain the search text String replaceText = replaceTextField. getText (); // obtain the replaced text if (searchText. isEmpty () return; try {File Reader FD = new FileReader (file); // create a file input stream char [] data = new char [1024]; // create a buffer character array int rn = 0; stringBuilder sb = new StringBuilder (); // create a string builder while (rn = Fi. read (data)> 0) {// read the file content to the String builder String str = String. valueOf (data, 0, rn); sb. append (str);} fiis. close (); // close the input stream // generate a String from the builder and replace the search text String str = sb. toString (). replace (searchText, replaceText); FileWriter fout = new FileWriter (fi Le); // create the file output stream fout. write (str. toCharArray (); // write the replaced string to the fout file. close (); // close the output stream} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} JOptionPane. showMessageDialog (null, "replaced");}/*** event handling method for opening the file button. ** @ Param e */protected void do_button_2_action1_med (ActionEvent e) {try {if (file = null) return; Desktop. getDesktop (). edit (file) ;}catch (IOException e1) {e1.printStackTrace ();}}}

Effect

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.