A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. //根据给定的正则表达式,删除匹配字符串
  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4. import java.awt.Toolkit;

  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.filechooser.FileNameExtensionFilter;
  9. import javax.swing.JLabel;
  10. import java.awt.FlowLayout;

  11. import javax.swing.JOptionPane;
  12. import javax.swing.JTextField;
  13. import javax.swing.JFileChooser;
  14. import javax.swing.JButton;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.util.Scanner;


  22. public class RemoveSplChar extends JFrame {

  23. /**
  24. *
  25. */
  26. private static final long serialVersionUID = -2003245766738195271L;
  27. private JPanel contentPane;
  28. private JTextField textField;
  29. private JTextField textField_1;
  30. private JTextField textField_2;
  31. private File sourceFile = null;
  32. private File objectFile = null;
  33. private Scanner scan=null;
  34. private Scanner scanner=null;

  35. public static void main(String[] args) {
  36. EventQueue.invokeLater(new Runnable() {
  37. public void run() {
  38. try {
  39. RemoveSplChar frame = new RemoveSplChar();

  40. frame.setVisible(true);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. });
  46. }

  47. /**
  48. * Create the frame.
  49. */
  50. public RemoveSplChar() {
  51. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. setBounds(100, 100, 450, 300);
  53. contentPane = new JPanel();
  54. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  55. contentPane.setLayout(new BorderLayout(0, 0));
  56. setContentPane(contentPane);
  57. setResizable(false);

  58. JPanel panel = new JPanel();
  59. contentPane.add(panel, BorderLayout.NORTH);
  60. panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

  61. JLabel label = new JLabel("\u8BF7\u9009\u62E9\u6587\u672C\uFF1A");
  62. panel.add(label);

  63. textField = new JTextField();
  64. panel.add(textField);
  65. textField.setColumns(10);

  66. JButton button = new JButton("\u9009\u62E9\u6587\u4EF6");
  67. button.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. JFileChooser chooser= new JFileChooser();
  70. FileNameExtensionFilter filer = new FileNameExtensionFilter("txt","txt");
  71. chooser.setFileFilter(filer);
  72. chooser.setMultiSelectionEnabled(false);
  73. int result = chooser.showOpenDialog(getContentPane());
  74. if (result == JFileChooser.APPROVE_OPTION) {
  75. sourceFile = chooser.getSelectedFile();
  76. if (!sourceFile.getName().endsWith(".txt")) {
  77. JOptionPane.showMessageDialog(getContentPane(), "请选择文本文件","",JOptionPane.WARNING_MESSAGE);
  78. return;
  79. }
  80. textField.setText(sourceFile.getAbsolutePath());
  81. }

  82. }
  83. });
  84. panel.add(button);

  85. JPanel panel_1 = new JPanel();
  86. contentPane.add(panel_1, BorderLayout.CENTER);
  87. panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 45));

  88. JLabel label_1 = new JLabel("\u8BF7\u9009\u62E9\u8F93\u51FA\u8DEF\u5F84\u53CA\u6587\u4EF6\u540D\uFF1A");
  89. panel_1.add(label_1);

  90. textField_1 = new JTextField();
  91. panel_1.add(textField_1);
  92. textField_1.setColumns(10);

  93. JButton button_1 = new JButton("\u9009\u62E9\u8DEF\u5F84");
  94. button_1.addActionListener(new ActionListener() {
  95. public void actionPerformed(ActionEvent e) {
  96. JFileChooser chooser= new JFileChooser();
  97. FileNameExtensionFilter filer = new FileNameExtensionFilter(".txt","txt");
  98. chooser.setFileFilter(filer);
  99. chooser.setMultiSelectionEnabled(false);
  100. int result = chooser.showSaveDialog(getContentPane());
  101. if (result == JFileChooser.APPROVE_OPTION) {
  102. objectFile = chooser.getSelectedFile();
  103. // System.out.print(objectFile.getAbsolutePath());
  104. if (!objectFile.getName().endsWith(".txt")) {
  105. if (!objectFile.exists()) {
  106. File temFile = new File(objectFile.getParent(),objectFile.getName()+".txt");
  107. objectFile = temFile;
  108. }
  109. else {
  110. JOptionPane.showMessageDialog(getContentPane(), "请选择文本文件或输入名称","",JOptionPane.WARNING_MESSAGE);
  111. return;
  112. }
  113. }
  114. textField_1.setText(objectFile.getAbsolutePath());
  115. }
  116. }
  117. });
  118. panel_1.add(button_1);

  119. JPanel panel_2 = new JPanel();
  120. contentPane.add(panel_2, BorderLayout.SOUTH);
  121. panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 15));

  122. JLabel label_2 = new JLabel("\u8F93\u5165\u6B63\u5219\u8868\u8FBE\u5F0F\uFF1A");
  123. panel_2.add(label_2);

  124. textField_2 = new JTextField();
  125. panel_2.add(textField_2);
  126. textField_2.setColumns(10);

  127. JButton button_2 = new JButton("\u8F6C\u6362");
  128. button_2.addActionListener(new ActionListener() {
  129. public void actionPerformed(ActionEvent e) {
  130. String tem = textField_2.getText();
  131. if (sourceFile == null) {
  132. Toolkit.getDefaultToolkit().beep();
  133. JOptionPane.showMessageDialog(getContentPane(), "请选择源文件","",JOptionPane.WARNING_MESSAGE);
  134. return;
  135. }
  136. if (objectFile == null) {
  137. Toolkit.getDefaultToolkit().beep();
  138. JOptionPane.showMessageDialog(getContentPane(), "请选择目标文件","",JOptionPane.WARNING_MESSAGE);
  139. return;
  140. }
  141. if (tem.equals("") ) {
  142. Toolkit.getDefaultToolkit().beep();
  143. JOptionPane.showMessageDialog(getContentPane(), "请输入正则表达式","",JOptionPane.WARNING_MESSAGE);
  144. return;
  145. }
  146. if (!objectFile.exists()) {
  147. try {
  148. objectFile.createNewFile();
  149. } catch (IOException e1) {
  150. // TODO 自动生成的 catch 块
  151. e1.printStackTrace();
  152. }
  153. }
  154. FileWriter writer=null;
  155. System.out.println(tem);
  156. try {
  157. writer = new FileWriter(objectFile);
  158. scanner = new Scanner(sourceFile);
  159. while(scanner.hasNextLine()) {
  160. StringBuilder str = new StringBuilder(scanner.nextLine());
  161. scan = new Scanner(str.toString());
  162. String num = scan.findInLine(tem.trim());
  163. while (num != null) {
  164. str.delete(str.indexOf(num), str.indexOf(num)+num.length());
  165. num = scan.findInLine(tem.trim());
  166. }
  167. str.append("\r\n");
  168. writer.write(str.toString());
  169. }
  170. writer.flush();
  171. } catch (FileNotFoundException e2) {
  172. // TODO 自动生成的 catch 块
  173. e2.printStackTrace();
  174. } catch (IOException e3) {
  175. // TODO 自动生成的 catch 块
  176. e3.printStackTrace();
  177. } finally {
  178. try {
  179. writer.close();
  180. } catch (IOException e4) {
  181. // TODO 自动生成的 catch 块
  182. e4.printStackTrace();
  183. }
  184. scanner.close();
  185. scan.close();
  186. }
  187. }
  188. });
  189. panel_2.add(button_2);
  190. }

  191. }
复制代码
在文本框中选择目标文件,在选择输出文件,然后在给定字符串或者是正则表达式,就可删除符合规则的字符串。这个代码是我之前手头上有个代码,但里面有许多注解的/*和行号掺杂到代码行里面,五百多行啊,要是手动删除那要搞好久,所以自己写了这代码来删除掉。效果还不错,代码还不怎么完善,有待继续改进

评分

参与人数 1技术分 +1 收起 理由
薛鹏鹏 + 1

查看全部评分

2 个回复

倒序浏览
不错哦。楼主继续加油
回复 使用道具 举报
不错!!!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马