Example of finding text content replacement function in Java

Idea:

First look at the view layer. There should be a JButton control to select the file, a jtextfield control to display the absolute path of the selected file, a jlabel control to prompt the user to enter the search text, a jlabel control to prompt the user to enter the replaced text, a jtextfield label for the user to enter the text to search, and a jtextfield label for the user to enter the replaced text, A JButton control performs the replacement, and a JButton control is used to open the modified file. For the select File button, Using the addActionListener () method of the JButton class to define its actionPerformed () function for its binding event, the method of selecting the file is called in the function body. First, the JFileChooser file selector is created in the selection file method, and the setFileFilter of the JFileChooser class is used. () method to create a file extension filter, and then use the setfileselectionmode() method of Jfilechooser class to set the file selection mode to file. The file opening dialog box is displayed through the showopendialog() method of Jfilechooser class. After confirming that the user presses the open button instead of the Cancel button, use getselectedfile() of Jfilechooser class Method to obtain the file object selected by the user, and use the settext() method of jtextfield class to display the file information to the text box. For the replacement button, use the addActionListener () method of the JButton class to bind the event with the select File button, define the actionPerformed () function in the event, and call the replacement text in the function body. In the replacement text method, first use the getText () method of the TextField class to get the text to be searched and the text to be replaced. If the search text is not empty, try to create the FileReader file input stream, char buffer character array and StringBuilder string builder. In the while () loop, use the read() method of FileReader class to read the file content to the string builder. After reading, use the close() method of FileReader class to close the input stream, Use the replace() method of the StringBuilder class to generate a string from the builder and replace the search text, then create a filewriter file output stream, use the write() method of the filewriter class to write the replaced string into the file, then use the close() method of the filewriter class to close the output stream, and then catch FileNotFoundException and IOException in turn, Finally, we use the showMessageDialog () method of the JOptionPane class to prompt the user to complete the replacement. For the open file button, we use the addActionListener () method of the JButton class to bind the event for it, define the actionPerformed () function in the event, use the method of opening the file in the function body, and try to use Desktop.getDesktop () in the open file method. edit(file);, And catch the IOException exception. The code is as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>