本篇内容介绍了“如何使用JAVA写文本编辑器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
再来分析一下,最后一个Menu,里面有几个按钮,不知道大家发现没有,有两个还是特别简单,一个是新建,一个是退出。新建我们再实例化一下父窗口就可以了,但是这里有bug,关闭任一子窗口父窗口都会跟着关掉。另一个是退出,直接dispose()就好了。在监听器里处理一下:
这里就不需要贴太多上下文代码了,找到主窗口.java 找到该函数就可以
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == item_about) {
new about_Window();
}else if (e.getSource() == item_word_format) {
new about_Format();
}else if (e.getSource() == item_new) {
new test5(); // 选中新建 new一个新窗口 ,有bug,关闭任意子窗口父窗口也会跟着关闭
}else if (e.getSource() == item_exit) {
this.dispose();
}
}
在JAVA写文本编辑器(一)我们有分析过,有一个封装好的工具JFileChooser可以直接调用。
其实消化完超链接里的这篇组件介绍,对于文件的存取已经没什么问题了。接下来我们添加监听器,监听器里添加对应的方法:
当然要先在类内声明JFileChooser
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == item_about) {
new about_Window();
}else if (e.getSource() == item_word_format) {
new about_Format();
}else if (e.getSource() == item_new) {
new test5(); // 选中新建 new一个新窗口 ,有bug,关闭任意子窗口父窗口也会跟着关闭
}else if (e.getSource() == item_exit) {
this.dispose();
}else if (e.getSource() == item_open) {
openFile();
}else if (e.getSource() == item_save) {
saveFile();
}
}
SaveFile方法:
private void saveFile() {
File file = null;
int result ;
fileChooser = new JFileChooser("C:\\");
fileChooser.setApproveButtonToolTipText("保存"); // 设置确认按钮的现实文本
fileChooser.setDialogTitle("保存文件"); // 设置title
result = fileChooser.showOpenDialog(rootPane); // 设置Dialog的根View 根布局
//--------------------------------------------------------------------------
if(result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile(); // 若点击了确定按钮,给file填文件路径
}
//--------------------------------------------------------------------------
/*FileOutputStream fileOutputStream = null; // 文件io类
if (file != null) {
try {
fileOutputStream = new FileOutputStream(file);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
String content = edit_text_area.getText();
try {
fileOutputStream.write(content.getBytes());
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fileOutputStream!=null) {
fileOutputStream.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}*/
//---------------这里有严重bug,对于字符写入文件没问题,但是在读取中文字符的时候会出现乱码-----------
//--------------------------------------------------------------------------
try{
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); // 对字符进行编码转换
BufferedWriter writer = new BufferedWriter(write);
String content = edit_text_area.getText();
writer.write(content);
writer.close();
}catch(IOException e) {
e.printStackTrace();
}
}
OpenFile方法:
/**
* 点击新建按item时 打开JFileChooser对话框
* 并且对文件读取进行处理
*/
private void openFile() {
File file = null;
int result ;
fileChooser = new JFileChooser("C:\\");
fileChooser.setApproveButtonToolTipText("确定"); // 设置确认按钮的现实文本
fileChooser.setDialogTitle("打开文件"); // 设置title
result = fileChooser.showOpenDialog(rootPane); // 设置Dialog的根View 根布局
//--------------------------------------------------------------------------
if(result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile(); // 若点击了确定按钮,给file填文件路径
}
//--------------------------------------------------------------------------
//--------------------下面对文件进行处理,把内容装到父窗体的textarea中--------------------
/*FileInputStream fileInputStream = null;
if (file != null) {
try { //此处需要注意空指针异常 即没有找到文件的时候需要处理
fileInputStream = new FileInputStream(file); // 将file文件的数据流装到fileInputStream里
}catch (FileNotFoundException e) { // 捕获到异常 ,需要处理
e.printStackTrace(); // 将异常实例化为e 然后在控制台Console 打印出错误的位置和原因
TipDialog tmpDialog = new TipDialog(this,"错误文件",true,"文件夹名称错误,请重新检查!");// 此处我们还可以对一场做一些处理,在这里弹出一个警示对话框
}
//读取文件
int readbyte ;
try {
while ((readbyte = fileInputStream.read())!=-1) { //一段段的读取文件
edit_text_area.append(String.valueOf((char)readbyte)); //在editarea 里一行行添加
}
}catch (IOException e) { // 处理异常
e.printStackTrace();
}finally {
try {
if (fileInputStream != null) { //对fileInputStream 回收
fileInputStream.close();
}
}catch (IOException e) { //抛出异常
e.printStackTrace();
}
}
}*/
//---------------这里有严重bug,对于读取中文字符会出现乱码-------------------------------
//--------------------------------------------------------------------------
if(file.isFile() && file.exists()) {
BufferedReader reader = null;
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
reader = new BufferedReader(inputStreamReader);
String readLine = "";
while ((readLine = reader.readLine()) != null) { // 对BufferedReader数据一行行读
//edit_text_area.append(readLine); 这样写会出现所有的句子都出现在同一行的情况,所以在每次append的时候在后面加一个换行符
edit_text_area.append(readLine+'\n'); //对edit_text_area 一行行加
}
reader.close(); // 关闭reader
}catch (IOException e) {
e.printStackTrace();
//TipDialog tmpDialog = new TipDialog(this,"错误文件",true,"文件夹名称错误,请重新检查!");
}
}
}
其实这里的两个方法都很相似,通过FileChooser可以得到选中的文件的路径,然后通过File 把路径拿到,在执行一下常规的文件读写操作。注意一定要处理IO操作异常。
如果大家在注意的话可以看到其实我的IO是有一些操作注释掉的,注释掉的部分是按照使用JFileChooser组件写的读写操作,但是经过测试,发现对于中文字符没有编码,所以存入之后再读取就是乱码。所以采用了另外一种写法。
“如何使用JAVA写文本编辑器”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。