用java写记事本
保存功能怎么实现??? --------------------编程问答-------------------- 使用io流写到文件里面 --------------------编程问答-------------------- 前天网上视频写的package demo3;import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
public class Editor extends Frame{
String filename;
TextArea tx;
Clipboard clip =getToolkit().getSystemClipboard();
Editor(){
setLayout(new GridLayout(1, 1));
//
tx=new TextArea();
add(tx);
MenuBar mb = new MenuBar();
Menu F = new Menu("文件");
MenuItem n = new MenuItem("新建");
MenuItem o = new MenuItem("打开");
MenuItem s = new MenuItem("保存");
//MenuItem as = new MenuItem("另存为");
MenuItem e = new MenuItem("退出");
n.addActionListener(new New());
F.add(n);
o.addActionListener(new Open());
F.add(o);
s.addActionListener(new Save());
F.add(s);
e.addActionListener(new Exit());
F.add(e);
mb.add(F);
Menu E = new Menu("编辑");
MenuItem cut=new MenuItem("剪切");
MenuItem copy = new MenuItem("复制");
MenuItem paste = new MenuItem("粘贴");
cut.addActionListener(new Cut());
E.add(cut);
copy.addActionListener(new Copy());
E.add(copy);
paste.addActionListener(new Paste());
E.add(paste);
mb.add(E);
setMenuBar(mb);
mylistener mylist= new mylistener();
addWindowListener(mylist);
}
//内部类
class mylistener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class New implements ActionListener{
public void actionPerformed(ActionEvent e){
tx.setText("此处输入文字");
setTitle(filename);
}
}
class Open implements ActionListener{
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(Editor.this, "选择一个文件", FileDialog.LOAD);
fd.show();
if(fd.getFile()!=null){
filename = fd.getDirectory()+fd.getFile();
setTitle(filename);
ReadFile();
}
tx.requestFocus();
}
}
void ReadFile(){
// 读流,插入文件,读完记得关闭流
BufferedReader d;
//StringBuffer是lang包里类,是String改进,可以读一行,替换,追加的,.toString(),
StringBuffer sb=new StringBuffer();
try {
d=new BufferedReader(new FileReader(filename));
String line;
try {
while((line=d.readLine() )!=null)
//这个while没有花括号
sb.append(line+"\n");
//跳出循环
tx.setText(sb.toString());
d.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class Cut implements ActionListener{
public void actionPerformed(ActionEvent e) {
String sel=tx.getSelectedText();
StringSelection ss=new StringSelection(sel);
clip.setContents(ss, ss);
tx.replaceRange("", tx.getSelectionStart(), tx.getSelectionEnd());
}
}
class Copy implements ActionListener{
public void actionPerformed(ActionEvent e) {
String sel=tx.getSelectedText();
StringSelection clipString=new StringSelection(sel);
clip.setContents(clipString, clipString);
}
}
class Paste implements ActionListener{
public void actionPerformed(ActionEvent e) {
Transferable cliptran=clip.getContents(Editor.this);
//??
String sel = null;
try {
sel = (String)cliptran.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
tx.replaceRange(sel, tx.getSelectionStart(), tx.getSelectionStart());
}
}
class Save implements ActionListener{
public void actionPerformed(ActionEvent e) {
FileDialog fd=new FileDialog(Editor.this, "保存到", FileDialog.SAVE);
fd.show();
if(fd.getFile()!=null){
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
}
try {
DataOutputStream d=new DataOutputStream(new FileOutputStream(filename));
String line =tx.getText();
BufferedReader br=new BufferedReader(new StringReader(line));
try {
while((line=br.readLine())!=null){
d.writeBytes(line+"\r\n");
d.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
class Exit implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
Frame f=new Editor();
f.setSize(500, 400);
f.setVisible(true);
f.show();
}
}
--------------------编程问答-------------------- 保存功能 其中需要用到IO包里的流操作了。
首先你要声明并实例化一个 JFileChooser fileChooser 控件 。 设置 fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
fileChooser.showSaveDialog(null);
File file =fileChooser.getSelectedFile();这样得到保存后生成的文件
然后使用PrintWriter pw = PrintWriter(file); 假设你所保存的文本已经取出 String str 封装。 则pw.write(str);即可完成保存 pw.close();别忘了关闭流
这里保存方法就完成了。。。
当然你也可以看看这个学习网站了:http://java.itcast.cn/java/video.shtml --------------------编程问答-------------------- 就是IO操作。可能的话加上一个判断,如果当前没有保存过,就弹出另存为对话框;如果已经保存过了,就直接用当前内容覆盖原来内容。 --------------------编程问答--------------------
给LZ 点思路 很简单的保存,其他功能自己扩展
public class AwtTest--------------------编程问答--------------------
{
private Frame f ;
private TextArea ta;
private FileDialog saveDia;
private File file;
private MenuBar mb;
private Menu m;
private MenuItem saveItem;
AwtTest()
{
init();
}
public void init()
{
//窗体 自定义
f = new Frame("数据世界");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
ta = new TextArea(25, 60);
f.add(ta);
//保存窗口
saveDia = new FileDialog(f,"保存",FileDialog.SAVE);
// 菜单初始化
mb = new MenuBar();
m = new Menu("文件");
f.setMenuBar(mb);
mb.add(m);
//保存条目
saveItem = new MenuItem("保存");
m.add(saveItem);
}
public void myEvent()
{
//保存
saveItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (file == null) {
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if (dirPath == null && fileName == null)
return;
file = new File(dirPath, fileName);
}
try {
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
} catch (IOException eq) {
throw new RuntimeException("文件保存失败");
}
}
});
//保存快捷键ctrl+s
ta.addKeyListener(new KeyAdapter()
{
public void keyPressedKey(KeyEvent e)
{
int code = e.getKeyCode();
if(e.isControlDown() && code==KeyEvent.VK_S)
{
saveDia.setVisible(true);
}
}
});
}
}
import java.io.*;
class hello{
public static void main(String[] args) {
File f=new File("D:\\hello.txt");
try{
f.createNewFile();
}catch (Exception e) {
e.printStackTrace();
}
}
}
java文件流 --------------------编程问答-------------------- 保存到本地硬盘。
补充:Java , Java SE