标签:
弄了个简单的记事本,能进行简单的打开文件,编辑文件,保存文件,但是怎么都无法解决中文乱码问题,求大家指教……
、
package com.xywei.awt;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
*
* @author Welins 写一个自己的简单记事本、
*
*/
public class MyNotepad {
private Frame fr = new Frame();
private MenuBar mbar = new MenuBar();
private Menu mu = new Menu();
private MenuItem clsItem = new MenuItem();
private MenuItem saveItem = new MenuItem();
private MenuItem opnItem = new MenuItem();
private TextArea txAe = new TextArea();
// 要打开的文件,涉及IO
private File file;
// 保存或者打开的对话框
// private FileDialog openDia,saveDia;
private FileDialog opnDial, saveDial;
public void myNotepad() {
fr.setTitle("我的记事本");
fr.setBounds(400, 100, 600, 400);
fr.setVisible(true);
fr.setMenuBar(mbar);
mu.setLabel("选择");
mbar.add(mu);
opnItem.setLabel("打开");
mu.add(opnItem);
saveItem.setLabel("保存");
mu.add(saveItem);
clsItem.setLabel("关闭");
mu.add(clsItem);
// 把文本加入到框架窗口中
fr.add(txAe);
opnDial = new FileDialog(fr, "打开", FileDialog.LOAD);
saveDial = new FileDialog(fr, "保存", FileDialog.SAVE);
Event();
}
public void Event() {
fr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("准备关闭窗口====");
System.out.println("窗口关闭了,程序退出来了");
System.exit(0);
}
});
clsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
// 读文件操作
opnItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 调用系统的函数,实现打开文件
// TODO Auto-generated method stub
opnDial.setVisible(true);
// 获得目录和文件
String dir = opnDial.getDirectory();
String fileName = opnDial.getFile();
System.out.println("你选择文件的目录是:" + dir + "文件为:" + fileName);
// 选择好了之后就在文本域中显示出来,先做清空处理
txAe.setText(null);
// 通过路径和文件名获得相应的文件
file = new File(dir, fileName);
// 下面的涉及到IO流操作,把文件打开,是按字节打开好还是字符还是一行一行好?
// 比较容易的是一行一行地读
/**
* // 把文件变为输入流 InputStreamReader inStrRead = new
* InputStreamReader( new FileInputStream(file), encoding); //
* 接下来把输入流变为字节流 BufferedReader bufRead = new
* BufferedReader(inStrRead); // 字符串 String lineTxt = null;
*
* while ((lineTxt = bufRead.readLine()) != null) {
* System.out.println(lineTxt); } // 读完之后还得关闭读入流
* bufRead.close(); inStrRead.close();
*/
String encoding = "UTF-8";
InputStreamReader bufIn;
try {
bufIn = new InputStreamReader(new FileInputStream(file),
encoding);
BufferedReader bufRead = new BufferedReader(bufIn);
// 一行一行读肯定还是得有个字符串、
String readF = null;
try {
while ((readF = bufRead.readLine()) != null) {
System.out.println(readF);
txAe.append(readF);
}
// 读完还得关闭流
bufRead.close();
bufIn.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("读取失败1======");
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("读取失败2======");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("读取失败3======");
}
// TODO Auto-generated catch block
}
});
// 写文件操作
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (file == null) {
saveDial.setVisible(true);
String dir = saveDial.getDirectory();
String fileName = saveDial.getFile();
file = new File(dir, fileName);
}
// 下面进行写的操作设计输出流操作
try {
OutputStreamWriter bufw = new OutputStreamWriter(
new FileOutputStream(file));
String writeStr = txAe.getText();
try {
bufw.write(writeStr);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
bufw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyNotepad notePad = new MyNotepad();
notePad.myNotepad();
}
}
Java中使用AWT编写自己的第四个图形界面--简单的记事本
标签:
原文地址:http://www.cnblogs.com/listentothecloud20150215/p/4297835.html