fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.*;
  6.  
  7.  
  8. public class stringsearch extends JFrame implements ActionListener {
  9.  
  10. private static final long serialVersionUID = 1L;
  11. JButton fileselect_b, search_b, print_b, end_b;
  12. JTextField path_f, keyword_f, total_f;
  13. JTextArea result_f;
  14. JLabel announce1, announce2, announce3, kensuu, gyo, naiyo;
  15. JFrame f1;
  16. JFileChooser fileselect;
  17. File file, cdirectory, filelist, filereader;
  18. JPanel p;
  19.  
  20. int number, kensu, total;
  21. String path, keyword, read;
  22. String read2[] = new String[1000];
  23.  
  24. //ボタンや入力欄を作る処理
  25. public stringsearch() {
  26. p = new JPanel();
  27. p.setLayout(null);
  28.  
  29. f1 = new JFrame();
  30. this.setSize(500, 600);
  31. this.setTitle("ストリングスサーチ Var.0.01");
  32.  
  33. fileselect_b = new JButton("ファイル選択");
  34. fileselect_b.setBounds(325, 40, 130, 25);
  35. fileselect_b.addActionListener(this);
  36. p.add(fileselect_b);
  37.  
  38. path_f = new JTextField();
  39. path_f.setBounds(20, 40, 300, 25);
  40. p.add(path_f);
  41.  
  42. announce1 = new JLabel("検索ファイル名の選択(直接入力可能)");
  43. announce1.setBounds(23, 18, 250, 25);
  44. p.add(announce1);
  45.  
  46. search_b = new JButton("検索");
  47. search_b.setBounds(325, 100, 130, 25);
  48. search_b.addActionListener(this);
  49. p.add(search_b);
  50.  
  51. keyword_f = new JTextField();
  52. keyword_f.setBounds(20, 100, 300, 25);
  53. p.add(keyword_f);
  54.  
  55. announce2 = new JLabel("検索文字列の入力");
  56. announce2.setBounds(23, 80, 250, 25);
  57. p.add(announce2);
  58.  
  59. print_b = new JButton("印字");
  60. print_b.setBounds(325, 150, 130, 25);
  61. p.add(print_b);
  62.  
  63. result_f = new JTextArea(10, 10);
  64. result_f.setBounds(23, 200, 434, 300);
  65. p.add(result_f);
  66.  
  67. end_b = new JButton("終了");
  68. end_b.setBounds(355, 525, 100, 25);
  69. end_b.addActionListener(this);
  70. p.add(end_b);
  71.  
  72. announce3 = new JLabel("全検索数");
  73. announce3.setBounds(120, 150, 130, 25);
  74. p.add(announce3);
  75.  
  76. kensuu = new JLabel("件数");
  77. kensuu.setBounds(23, 175, 250, 25);
  78. p.add(kensuu);
  79.  
  80. gyo = new JLabel("行数");
  81. gyo.setBounds(100, 175, 250, 25);
  82. p.add(gyo);
  83.  
  84. naiyo = new JLabel("内容");
  85. naiyo.setBounds(190, 175, 250, 25);
  86. p.add(naiyo);
  87.  
  88. total_f = new JTextField();
  89. total_f.setBounds(175, 150, 60, 25);
  90. p.add(total_f);
  91.  
  92. sl = new JScrollPane(result_f);
  93. sl.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  94. sl.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  95. sl.setBounds(23, 200, 434, 300);
  96. p.add(sl);
  97.  
  98. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  99. this.setVisible(true);
  100. getContentPane().add(p, BorderLayout.CENTER);
  101.  
  102. }
  103.  
  104. public void actionPerformed(ActionEvent e) {
  105. //ファイル選択ボタンを押された時の処理
  106. if (e.getSource().equals(fileselect_b)) {
  107. result_f.setText("");
  108. total_f.setText("");
  109. total = 0;
  110. kensu = 0;
  111.  
  112. fileselect = new JFileChooser();
  113. fileselect.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  114. fileselect.setDialogTitle("ファイルの選択");
  115. int selected = fileselect.showDialog(this, "選択");
  116. if (selected == JFileChooser.APPROVE_OPTION) {
  117. file = fileselect.getSelectedFile();
  118. path = file.getAbsolutePath();
  119. path_f.setText(path);
  120. } else if (selected == JFileChooser.CANCEL_OPTION) {
  121. path_f.setText("キャンセルされました。");
  122. } else if (selected == JFileChooser.ERROR_OPTION) {
  123. path_f.setText("エラーです。");
  124. }
  125. }
  126. //検索ボタンを押された時の処理
  127. if (e.getSource().equals(search_b)) {
  128. keyword = keyword_f.getText();
  129. int i = 0;
  130. total = 0;
  131. kensu = 0;
  132. if (path == null || keyword == null) {
  133. result_f.setText("検索文字列を入力してください");
  134. } else {
  135. result_f.setText("");
  136. try {
  137. FileReader file = new FileReader(path);
  138.  
  139. // マッチしたかを示す。一度マッチしたら true に変更する。
  140. boolean matched = false;
  141.  
  142. // ファイル中の行番号
  143. int lineNumber = 1;
  144.  
  145. // result_f 用メッセージ。マッチした結果はここに追加する。
  146. StringBuilder message = new StringBuilder();
  147.  
  148. while ((read = br.readLine()) != null) {
  149. System.out.println(read);
  150.  
  151. String str = new String(read);
  152.  
  153. if (str.indexOf(keyword) >= 0) {
  154. matched = true;
  155. total++;
  156. kensu++;
  157. message.append(kensu + "件 " + lineNumber + "行 " + read + "\n");
  158. }
  159.  
  160. lineNumber++;
  161. }
  162.  
  163. // 結果の出力 - 全検索数
  164. total_f.setText(total + "件");
  165.  
  166. // 結果の出力 - 件数, 行数, 内容
  167. if (matched) {
  168. result_f.setText(message.toString());
  169. } else {
  170. result_f.setText("検索した文字列が見つかりません");
  171. }
  172.  
  173. br.close();
  174.  
  175. } catch (FileNotFoundException e2) {
  176. result_f.setText("読み込めませんでした。");
  177. } catch (IOException e2) {
  178. result_f.setText("読み込めませんでした。");
  179. }
  180. }
  181. }
  182.  
  183. if (e.getSource().equals(end_b)) {
  184. System.exit(0);
  185. }
  186. }
  187.  
  188. public static void main(String[] args) {
  189. new stringsearch();
  190.  
  191. }
  192. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty