- //根据给定的正则表达式,删除匹配字符串
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.JLabel;
- import java.awt.FlowLayout;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.JFileChooser;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class RemoveSplChar extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = -2003245766738195271L;
- private JPanel contentPane;
- private JTextField textField;
- private JTextField textField_1;
- private JTextField textField_2;
- private File sourceFile = null;
- private File objectFile = null;
- private Scanner scan=null;
- private Scanner scanner=null;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- RemoveSplChar frame = new RemoveSplChar();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public RemoveSplChar() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- setResizable(false);
- JPanel panel = new JPanel();
- contentPane.add(panel, BorderLayout.NORTH);
- panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
- JLabel label = new JLabel("\u8BF7\u9009\u62E9\u6587\u672C\uFF1A");
- panel.add(label);
- textField = new JTextField();
- panel.add(textField);
- textField.setColumns(10);
- JButton button = new JButton("\u9009\u62E9\u6587\u4EF6");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser= new JFileChooser();
- FileNameExtensionFilter filer = new FileNameExtensionFilter("txt","txt");
- chooser.setFileFilter(filer);
- chooser.setMultiSelectionEnabled(false);
- int result = chooser.showOpenDialog(getContentPane());
- if (result == JFileChooser.APPROVE_OPTION) {
- sourceFile = chooser.getSelectedFile();
- if (!sourceFile.getName().endsWith(".txt")) {
- JOptionPane.showMessageDialog(getContentPane(), "请选择文本文件","",JOptionPane.WARNING_MESSAGE);
- return;
- }
- textField.setText(sourceFile.getAbsolutePath());
- }
- }
- });
- panel.add(button);
- JPanel panel_1 = new JPanel();
- contentPane.add(panel_1, BorderLayout.CENTER);
- panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 45));
- JLabel label_1 = new JLabel("\u8BF7\u9009\u62E9\u8F93\u51FA\u8DEF\u5F84\u53CA\u6587\u4EF6\u540D\uFF1A");
- panel_1.add(label_1);
- textField_1 = new JTextField();
- panel_1.add(textField_1);
- textField_1.setColumns(10);
- JButton button_1 = new JButton("\u9009\u62E9\u8DEF\u5F84");
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser= new JFileChooser();
- FileNameExtensionFilter filer = new FileNameExtensionFilter(".txt","txt");
- chooser.setFileFilter(filer);
- chooser.setMultiSelectionEnabled(false);
- int result = chooser.showSaveDialog(getContentPane());
- if (result == JFileChooser.APPROVE_OPTION) {
- objectFile = chooser.getSelectedFile();
- // System.out.print(objectFile.getAbsolutePath());
- if (!objectFile.getName().endsWith(".txt")) {
- if (!objectFile.exists()) {
- File temFile = new File(objectFile.getParent(),objectFile.getName()+".txt");
- objectFile = temFile;
- }
- else {
- JOptionPane.showMessageDialog(getContentPane(), "请选择文本文件或输入名称","",JOptionPane.WARNING_MESSAGE);
- return;
- }
- }
- textField_1.setText(objectFile.getAbsolutePath());
- }
- }
- });
- panel_1.add(button_1);
- JPanel panel_2 = new JPanel();
- contentPane.add(panel_2, BorderLayout.SOUTH);
- panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 15));
- JLabel label_2 = new JLabel("\u8F93\u5165\u6B63\u5219\u8868\u8FBE\u5F0F\uFF1A");
- panel_2.add(label_2);
- textField_2 = new JTextField();
- panel_2.add(textField_2);
- textField_2.setColumns(10);
- JButton button_2 = new JButton("\u8F6C\u6362");
- button_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String tem = textField_2.getText();
- if (sourceFile == null) {
- Toolkit.getDefaultToolkit().beep();
- JOptionPane.showMessageDialog(getContentPane(), "请选择源文件","",JOptionPane.WARNING_MESSAGE);
- return;
- }
- if (objectFile == null) {
- Toolkit.getDefaultToolkit().beep();
- JOptionPane.showMessageDialog(getContentPane(), "请选择目标文件","",JOptionPane.WARNING_MESSAGE);
- return;
- }
- if (tem.equals("") ) {
- Toolkit.getDefaultToolkit().beep();
- JOptionPane.showMessageDialog(getContentPane(), "请输入正则表达式","",JOptionPane.WARNING_MESSAGE);
- return;
- }
- if (!objectFile.exists()) {
- try {
- objectFile.createNewFile();
- } catch (IOException e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
- FileWriter writer=null;
- System.out.println(tem);
- try {
- writer = new FileWriter(objectFile);
- scanner = new Scanner(sourceFile);
- while(scanner.hasNextLine()) {
- StringBuilder str = new StringBuilder(scanner.nextLine());
- scan = new Scanner(str.toString());
- String num = scan.findInLine(tem.trim());
- while (num != null) {
- str.delete(str.indexOf(num), str.indexOf(num)+num.length());
- num = scan.findInLine(tem.trim());
- }
- str.append("\r\n");
- writer.write(str.toString());
- }
- writer.flush();
- } catch (FileNotFoundException e2) {
- // TODO 自动生成的 catch 块
- e2.printStackTrace();
- } catch (IOException e3) {
- // TODO 自动生成的 catch 块
- e3.printStackTrace();
- } finally {
- try {
- writer.close();
- } catch (IOException e4) {
- // TODO 自动生成的 catch 块
- e4.printStackTrace();
- }
- scanner.close();
- scan.close();
- }
- }
- });
- panel_2.add(button_2);
- }
- }
复制代码 在文本框中选择目标文件,在选择输出文件,然后在给定字符串或者是正则表达式,就可删除符合规则的字符串。这个代码是我之前手头上有个代码,但里面有许多注解的/*和行号掺杂到代码行里面,五百多行啊,要是手动删除那要搞好久,所以自己写了这代码来删除掉。效果还不错,代码还不怎么完善,有待继续改进
|