简易文本编辑器java_Java实现简易的文本编辑器

需求分析:

获得文本名称

实现尾部追加功能

实现覆盖式添加数据

删除数据

获取光标位置

在特定光标位置处添加数据

查找特定字符串在主串中第一次出现的位置

统计文本文件内出现的数字,汉字,英文字母,特殊字符的个数,及总的字符个数

开发环境:

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:

//代码的作用就是实现对特定的文件进行读取,并存入到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();

}

代码块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();

}

}

}

代码块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();

}

}

其中使用到的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)));

}

}

好了,下面是程序运行后得到的界面,在此我要声明的是,程序仍然存在一些bug,表现在获得光标位置时的java.lang.IllegalStateException: Attempt to mutate in notification异常,主要还是线程相关,如果博友能解决,还望不吝赐教

3f18222007d9

统计方法运行结果

3f18222007d9

删除方法运行结果

3f18222007d9

查找方法运行结果

3f18222007d9

文档名称运行结果

能力有限,希望和大家一起进步,一同提高!

接下来的是我从网上找到的一份用C语言实现的简易的文本编辑器的实现,个人认为较之,我的简直就是太菜了,现在将代码贴出来,希望这篇C语言的经典能让更多的人知晓:

#include

#define MAXLEN 80

#define MAXLINE 200

char 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

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

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

{

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

{

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=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

{

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

{

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值