Ideas:
First look at the view layer. There must be a JButton control to select a file. A JTextField control displays the absolute path of the selected file. A JLabel control prompts the user to enter the search text. A JLabel control prompts the user to enter the replaced text. A JTextField tag is for the user. Enter the text to be searched, a JTextField tag 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, use the addActionListener() method of the JButton class to bind the event to it, define the actionPerformed() function in the event, and call the method of selecting the file in the function body.
In the selection file method, first create the JFileChooser file selector, use the setFileFilter() method of the JFileChooser class to create the file extension filter, and then use the setFileSelectionMode() method of the JFileChooser class to set the file selection mode to a file, and use the showOpenDialog of the JFileChooser class to set the file selection mode to a file. ) method displays the file opening dialog box, and after confirming that the user presses the open button instead of the cancel button, the file object selected by the user is obtained through the getSelectedFile() method of the JFileChooser class, and the file information is displayed to the text box using the setText() method of the JTextField class. .
For the replacement button, select the file button, use the addActionListener() method of the JButton class to bind the event, define the actionPerformed() function in the event, and call the method of replacing 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 a FileReader file input stream, char buffered character array and StringBuilder string construction The read() method of the FileReader class is used to read the file contents to the string builder in the while() loop. After reading, use the close() method of the FileReader class to close the input stream, and use the StringBuilder class The replace() method generates a string from the builder and replaces the search text, then creates a FileWriter file output stream, use the write() method of the FileWriter class to write the replaced string into the file, and then use the Close of the FileWriter class The () method closes the output stream, then catches the FileNotFoundException and the IOException exception in turn, and finally uses the showMessageDialog() method of the JOptionPane class to prompt the user to complete the replacement.
For the Open File button, use the addActionListener() method of the JButton class to bind the event to it, define the actionPerformed() function in the event, and call the method of opening the file in the function body.
Try using Desktop.getDesktop().edit(file); in the open file method and catch the IOException exception.
The code is as follows:
The code copy is as follows:
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("Select File");
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("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();
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("replace with:");
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("replace");
replaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_replaceButton_actionPerformed(e);
}
});
panel_1.add(replaceButton);
JButton openfileButton = new JButton("Open file");
openfileButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_2_actionPerformed(e);
}
});
panel_1.add(openfileButton);
}
/**
* Select the file button event handling method
*
* @param e
*/
protected void do_button_actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("./");// Create a file selector
// Set file extension filter
chooser.setFileFilter(new FileNameExtensionFilter("Text", "txt",
"java", "php", "html", "htm"));
// Set file selection mode
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// Show file opening dialog box
int option = chooser.showOpenDialog(this);
// Make sure the user presses the Open button instead of the Cancel button
if (option != JFileChooser.APPROVE_OPTION)
return;
// Get the file object selected by the user
file = chooser.getSelectedFile();
// Show file information to the text box
fileField.setText(file.toString());
}
/**
* Replace button event handling method
*
* @param e
*/
protected void do_replaceButton_actionPerformed(ActionEvent event) {
String searchText = searchTextField.getText();// Get search text
String replaceText = replaceTextField.getText();// Get replacement text
if (searchText.isEmpty())
return;
try {
FileReader fis = new FileReader(file);// Create file input stream
char[] data = new char[1024];// Create a buffered character array
int rn = 0;
StringBuilder sb = new StringBuilder();// Create a string builder
while ((rn = fis.read(data)) > 0) {// Read file content to string builder
String str = String.valueOf(data, 0, rn);
sb.append(str);
}
fis.close();// Close the input stream
// Generate string from the builder and replace search text
String str = sb.toString().replace(searchText, replaceText);
FileWriter fout = new FileWriter(file);// Create file output stream
fout.write(str.toCharArray());// Write the replaced string into the file
fout.close();// Close the output stream
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "Replacement Completed");
}
/**
* How to handle event of the open file button.
*
* @param e
*/
protected void do_button_2_actionPerformed(ActionEvent e) {
try {
if (file == null)
return;
Desktop.getDesktop().edit(file);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}