分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
需求分析:
- 获得文本名称
- 实现尾部追加功能
- 实现覆盖式添加数据
- 删除数据
- 获取光标位置
- 在特定光标位置处添加数据
- 查找特定字符串在主串中第一次出现的位置
统计文本文件内出现的数字,汉字,英文字母,特殊字符的个数,及总的字符个数
开发环境:
windows7 + Eclipse luna + WindowsBuilder插件
代码实现:
import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStreamWriter;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.border.EmptyBorder;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;public class Test extends JFrame { private JPanel contentPane; private static File file = null; static int CursorPosition=-1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Test frame = new Test(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Test() { file = new File("F://test.txt"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 720, 480); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JTextArea taShow = new JTextArea(); taShow.setLineWrap(true); taShow.setBounds(21, 41, 400, 359); JLabel label = new JLabel("\u6587\u672C\u9884\u89C8\u533A\uFF1A"); label.setBounds(21, 16, 89, 15); contentPane.add(label); JTextArea taEdit = new JTextArea(); taEdit.setLineWrap(true); taEdit.setBounds(449, 41, 233, 131); contentPane.add(taEdit); taShow.addCaretListener(new CaretListener() { @Override public void caretUpdate(CaretEvent e) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); taShow.setText("您打开的文件的内容是:" + fileContent); CursorPosition = e.getDot(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); contentPane.add(taShow); JButton btnGetName = new JButton("\u6587\u6863\u540D\u79F0"); btnGetName.setBounds(449, 211, 93, 23); btnGetName.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); fileTitle = file.getName().toString(); taEdit.setText("您打开的文件的名称是:" + fileTitle); taShow.setText("您打开的文件的内容是:" + fileContent); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); contentPane.add(btnGetName); JButton btnAppend = new JButton("\u8FFD\u52A0"); btnAppend.setBounds(449, 261, 93, 23); btnAppend.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String temp = taEdit.getText().toString(); method1("F://test.txt", temp); StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); fileTitle = file.getName().toString(); taEdit.setText("您打开的文件的名称是:" + fileTitle); taShow.setText("您打开的文件的内容是:" + fileContent); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); contentPane.add(btnAppend); JButton btnOverride = new JButton("\u8986\u76D6"); btnOverride.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file))); out.write(taEdit.getText().toString()); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException le) { le.printStackTrace(); } } StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); fileTitle = file.getName().toString(); taEdit.setText("您打开的文件的名称是:" + fileTitle); taShow.setText("您打开的文件的内容是:" + fileContent); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); btnOverride.setBounds(449, 308, 93, 23); contentPane.add(btnOverride); JButton btnSearch = new JButton("\u67E5\u627E"); btnSearch.setBounds(449, 357, 93, 23); btnSearch.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); taShow.setText("您打开的文件的内容是:" + fileContent); String p = taEdit.getText().toString().trim(); taShow.setText(fileContent+"\n\n"+"您查找的字符串第一次出现的位置是:"+fileContent.indexOf(p)); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); contentPane.add(btnSearch); JButton btnPosition = new JButton("\u5149\u6807\u4F4D\u7F6E"); btnPosition.setBounds(589, 211, 93, 23); btnPosition.enable(false); contentPane.add(btnPosition); JButton btnInsert = new JButton("\u5B9A\u70B9\u63D2\u5165"); btnInsert.setBounds(589, 261, 93, 23); btnInsert.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } String temp=taEdit.getText().toString(); sb.insert(CursorPosition, temp); method1("F://test.txt", sb.toString()); taShow.setText(sb.toString()); taEdit.setText("定点的数据插入成功执行!"); }catch(Exception ev){ ev.printStackTrace(); } } }); contentPane.add(btnInsert); JButton btnDelete = new JButton("\u5220\u9664"); btnDelete.setBounds(589, 308, 93, 23); btnDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file))); out.write(""); taShow.setText("删除操作已完成,请到相应路径下查看!"); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException le) { le.printStackTrace(); } } } }); contentPane.add(btnDelete); JButton btnTotal = new JButton("\u7EDF\u8BA1"); btnTotal.setBounds(589, 357, 93, 23); btnTotal.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); new Total().find(fileContent); String flag = "数据信息统计结果如下:" + "\n" + "汉字数目:"; flag += new Total().chineseCount; flag += "\n英文字母个数:"; flag += new Total().englishCount; flag += "\n特殊字符个数:"; flag += new Total().numberCount; flag += "\n总的字符个数为:" + (new Total().chineseCount + new Total().englishCount + new Total().numberCount); taShow.setText(flag); new Total().chineseCount = 0; new Total().englishCount = 0; new Total().numberCount = 0; } catch (Exception ec) { ec.printStackTrace(); } } }); contentPane.add(btnTotal); JLabel label_1 = new JLabel("\u6587\u672C\u7F16\u8F91\u533A\uFF1A"); label_1.setBounds(449, 16, 93, 15); contentPane.add(label_1); } public static void method1(String file, String conent) { BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file, true))); out.write(conent); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
下面解释一下为什么没有做好注释合作说明文档,因为我做注释做到一半的时候,出现了一点事故,导致没有来得及保存的文件丢失了,所以,请大家谨记,时刻记得保存编辑的被容,否则后果真的很严重。
代码追补解释,下面的代码块是我程序里面做的不好的,违背了代码的复用性原则,请予以为戒:
代码块1:
//代码的作用就是实现对特定的文件进行读取,并存入到String中,方便使用StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); taShow.setText("您打开的文件的内容是:" + fileContent); CursorPosition = e.getDot(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
代码块2:
//代码实现了向特定的文件内追加数据,若想要覆盖式追加,把参数true去掉即可,默认为覆盖式添加数据public static void method1(String file, String conent) { BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file, true))); out.write(conent); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
代码块3:
在统计模块中:
btnTotal.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String length = ""; String fileTitle; String fileContent; try { BufferedReader reader = new BufferedReader(new FileReader( "F://test.txt")); while ((length = reader.readLine()) != null) { sb.append(length); } fileContent = sb.toString(); new Total().find(fileContent); String flag = "数据信息统计结果如下:" + "\n" + "汉字数目:"; flag += new Total().chineseCount; flag += "\n英文字母个数:"; flag += new Total().englishCount; flag += "\n特殊字符个数:"; flag += new Total().numberCount; flag += "\n总的字符个数为:" + (new Total().chineseCount + new Total().englishCount + new Total().numberCount); taShow.setText(flag); new Total().chineseCount = 0; new Total().englishCount = 0; new Total().numberCount = 0; } catch (Exception ec) { ec.printStackTrace(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
其中使用到的new Total().find()方法,详见下面的代码:
package Editer;/** * 分别统计出其中字符串中汉字,英文字母,数字,其他字符数量 * @author wWX154783 * */public class Total{ static String E1,E2,E3; String str="a12中国3@b&4语*言3c"; static int chineseCount = 0; static int englishCount = 0; static int numberCount = 0; public void find(String str) { String E1 = "[\u4e00-\u9fa5]";// 中文 String E2 = "[a-zA-Z]";// 英文 String E3 = "[0-9]";// 数字 String temp; for (int i = 0; i < str.length(); i++) { temp = String.valueOf(str.charAt(i)); if (temp.matches(E1)) { chineseCount++; } if (temp.matches(E2)) { englishCount++; } if (temp.matches(E3)) { numberCount++; } } System.out.println("汉字数:" + chineseCount); System.out.println("英文数:" + englishCount); System.out.println("数字数:" + numberCount); System.out.println("特殊字符:" + (str.length() - (chineseCount + englishCount + numberCount))); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
好了,下面是程序运行后得到的界面,在此我要声明的是,程序仍然存在一些bug,表现在获得光标位置时的java.lang.IllegalStateException: Attempt to mutate in notification异常,主要还是线程相关,如果博友能解决,还望不吝赐教
能力有限,希望和大家一起进步,一同提高!
接下来的是我从网上找到的一份用C语言实现的简易的文本编辑器的实现,个人认为较之,我的简直就是太菜了,现在将代码贴出来,希望这篇C语言的经典能让更多的人知晓:
#include <stdio.h>#define MAXLEN 80#define MAXLINE 200char buffer[MAXLEN],fname[120];char *lineptr[MAXLINE];FILE *fp;void edit(),replace(),insert(),delete(),quit();char comch[]="EeRrIiDdQq";/*命令符*/void(*comfun[])()={edit,replace,insert,delete,quit};/*对应处理函数*/int modified=0,/*正文被修改标志*/ last;/*当前正文行数*/char *chpt;/*输入命令行字符指针*/main(){ int j; last=0; while(1) { printf("\nInput a command:[e,r,i,d,q].\n"); gets(buffer);/*读入命令行*/ for(chpt=buffer;*chpt=='\0'||*chpt=='\t';chpt++);/*掠过空白符*/ if(*chpt=='\0') continue;/*空行重新输入*/ for(j=0;comch[j]!='\0'&&comch[j]!=*chpt;j++);/*查命令符*/ if(comch[j]=='\0') continue;/*非法命令符*/ chpt++;/*掠过命令符,指向参数*/ (*comfun[j/2])();/*执行对应函数*/ fprintf(stdout,"The text is:\n"); for(j=0;j<last;j++)/*显示正文*/ fputs(lineptr[j],stdout); }}void quit(){ int c; if(modified)/* 如正文被修改 */ { printf("Save? (y/n)"); while(!(((c=getchar())>='a'&&c<='z')||(c>='A'&&c<='Z'))); if(c=='y'||c=='Y') save(fname); /* 保存被修改过的正文 */ } for(c=0;c<last;c++) free(lineptr[c]); /* 释放内存 */ exit(0);}void insert(){ int k,m,i; sscanf(chpt,"%d%d",&k,&m); /* 读入参数 */ if(m<0||m>last||last+k>=MAXLINE)/* 检查参数合理性 */ { printf("Error!\n"); return; } for(i=last;i>m;i--)/* 后继行向后移 */ lineptr[i+k-1]=lineptr[i-1]; for(i=0;i<k;i++) /* 读入k行正文,并插入 */ { fgets(buffer,MAXLEN,stdin); lineptr[m+i]=(char *)malloc(strlen(buffer)+1); strcpy(lineptr[m+i],buffer); } last+=k; /* 修正正文行数 */ modified=1; /* 正文被修改 */}void delete(){ int i,j,m,n; sscanf(chpt,"%d%d",&m,&n); /* 读入参数 */ if(m<=0||m>last||n<m) /* 检查参数合理性 */ { printf("Error!\n"); return; } if(n>last) n=last; /* 修正参数 */ for(i=m;i<=n;i++) /* 删除正文 */ free(lineptr[i-1]); for(i=m,j=n+1;j<=last;i++,j++) lineptr[i-1]=lineptr[j-1]; last=i-1; /* 修正正文行数 */ modified=1; /* 正文被修改 */}void replace(){ int k,m,n,i,j; sscanf(chpt,"%d%d%d",&k,&m,&n); /* 读入参数 */ if(m<=0||m>last||n<m||last-(n-m+1)+k>=MAXLINE)/* 检查参数合理性 */ { printf("Error!\n"); return; } /* 先完成删除 */ if(n>last) n=last; /* 修正参数 */ for(i=m;i<=n;i++) /* 删除正文 */ free(lineptr[i-1]); for(i=m,j=n+1;j<=last;i++,j++) lineptr[i-1]=lineptr[j-1]; last=i-1; /* 以下完成插入 */ for(i=last;i>=m;i--) lineptr[i+k-1]=lineptr[i-1]; for(i=0;i<k;i++) { fgets(buffer,MAXLEN,stdin); lineptr[m+i-1]=(char *)malloc(strlen(buffer)+1); strcpy(lineptr[m+i-1],buffer); } last+=k; /* 修正正文行数 */ modified=1; /* 正文被修改 */}save(char *fname) /* 保存文件 */{ int i; FILE *fp; if((fp=fopen(fname,"w"))==NULL) { fprintf(stderr,"Can't open %s.\n",fname); exit(1); } for(i=0;i<last;i++) { fputs(lineptr[i],fp); free(lineptr[i]); } fclose(fp);}void edit() /* 编辑命令 */{ int i; FILE *fp; i=sscanf(chpt,"%s",fname); /* 读入文件名 */ if(i!=1) { printf("Enter file name.\n"); scanf("%s",fname); } if((fp=fopen(fname,"r"))==NULL) /* 读打开 */ { fp=fopen(fname,"w"); /* 如不存在,则创建文件 */ fclose(fp); fp=fopen(fname,"r"); /* 重新读打开 */ } i=0; while(fgets(buffer,MAXLEN,fp)==buffer) { lineptr[i]=(char *)malloc(strlen(buffer)+1); strcpy(lineptr[i++],buffer); } fclose(fp); last=i;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161