Java中输入、输出流的问题
用Java输出流把编辑的一个文件信息保存为“student.txt”,用输入流打开后编辑,然后在保存,要求覆盖原来的student.txt文件,该如何做?...
用Java输出流把编辑的一个文件信息保存为“student.txt”,用输入流打开后编辑,然后在保存,要求覆盖原来的student.txt文件,该如何做?
展开
1个回答
2013-08-17
展开全部
一个简单的数据留程序,有你想要的。自己看看吧
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Jishiben1 extends Frame implements ActionListener,ItemListener
{
TextArea text;
String str="";
CheckboxMenuItem miFontBold,miFontItalic;
public Jishiben1(String s)
{
super(s);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
setVisible(false);
}
});
Menu mn=new Menu("文件");
MenuItem miOpen=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_0));
MenuItem miSave=new MenuItem("保存",new MenuShortcut(KeyEvent.VK_S));
MenuItem miExit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
mn.add(miOpen);
mn.add(miSave);
mn.addSeparator();
mn.add(miExit);
miOpen.addActionListener(this);
miSave.addActionListener(this);
miExit.addActionListener(this);
Menu mn1=new Menu("编辑");
MenuItem miCopy=new MenuItem("拷贝",new MenuShortcut(KeyEvent.VK_C));
MenuItem miPaste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V));
mn1.add(miCopy);
mn1.add(miPaste);
miCopy.addActionListener(this);
miPaste.addActionListener(this);
mn1.addSeparator();
Menu miFont=new Menu("字体");
miFontBold=new CheckboxMenuItem("黑体");
miFontItalic=new CheckboxMenuItem("斜体");
miFont.add(miFontBold);
miFont.add(miFontItalic);
mn1.add(miFont);
miFontBold.addItemListener(this);
miFontItalic.addItemListener(this);
MenuBar mb=new MenuBar();
mb.add(mn);
mb.add(mn1);
setMenuBar(mb);
text=new TextArea();
add("Center",text);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String strCmd=e.getActionCommand();
if(strCmd.equals("退出"))
setVisible(false);
try
{
if(strCmd.equals("打开"))
openText();
if(strCmd.equals("保存"))
saveText();
}
catch(IOException e1){}
if(strCmd.equals("拷贝"))
str=text.getSelectedText();
if(strCmd.equals("粘贴"))
text.insert(str,text.getCaretPosition());
}
public void itemStateChanged(ItemEvent e)
{
int style=Font.PLAIN;
Font font=new Font("宋体",style,15);
if(miFontBold.getState())
style+=Font.BOLD;
String s=text.getText();
text.setFont(new Font("宋体",style,15));
text.setText(s);
}
public void openText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
fd.setVisible(true);
File file=new File(fd.getDirectory()+fd.getFile());
FileReader fr=new FileReader(file);
BufferedReader bufr=new BufferedReader(fr,4096);
text.setText("");
String texts="",s;
while((s=bufr.readLine())!=null)
{
text.setFont(new Font("宋体",Font.PLAIN,15));
texts+=s+"\n";
}
text.setText(texts);
bufr.close();
}
public void saveText() throws IOException
{
FileDialog fd=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
fd.setVisible(true);
File file=new File(fd.getDirectory()+fd.getFile()+".txt");
FileWriter fw=new FileWriter(file);
BufferedWriter bufw=new BufferedWriter(fw);
String s=text.getText();
bufw.write(s,0,s.length());
bufw.flush();
bufw.close();
}
}
public class Jishiben
{
public static void main(String args[])
{
Jishiben1 w=new Jishiben1("文本编辑程序");
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Jishiben1 extends Frame implements ActionListener,ItemListener
{
TextArea text;
String str="";
CheckboxMenuItem miFontBold,miFontItalic;
public Jishiben1(String s)
{
super(s);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
setVisible(false);
}
});
Menu mn=new Menu("文件");
MenuItem miOpen=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_0));
MenuItem miSave=new MenuItem("保存",new MenuShortcut(KeyEvent.VK_S));
MenuItem miExit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
mn.add(miOpen);
mn.add(miSave);
mn.addSeparator();
mn.add(miExit);
miOpen.addActionListener(this);
miSave.addActionListener(this);
miExit.addActionListener(this);
Menu mn1=new Menu("编辑");
MenuItem miCopy=new MenuItem("拷贝",new MenuShortcut(KeyEvent.VK_C));
MenuItem miPaste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V));
mn1.add(miCopy);
mn1.add(miPaste);
miCopy.addActionListener(this);
miPaste.addActionListener(this);
mn1.addSeparator();
Menu miFont=new Menu("字体");
miFontBold=new CheckboxMenuItem("黑体");
miFontItalic=new CheckboxMenuItem("斜体");
miFont.add(miFontBold);
miFont.add(miFontItalic);
mn1.add(miFont);
miFontBold.addItemListener(this);
miFontItalic.addItemListener(this);
MenuBar mb=new MenuBar();
mb.add(mn);
mb.add(mn1);
setMenuBar(mb);
text=new TextArea();
add("Center",text);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String strCmd=e.getActionCommand();
if(strCmd.equals("退出"))
setVisible(false);
try
{
if(strCmd.equals("打开"))
openText();
if(strCmd.equals("保存"))
saveText();
}
catch(IOException e1){}
if(strCmd.equals("拷贝"))
str=text.getSelectedText();
if(strCmd.equals("粘贴"))
text.insert(str,text.getCaretPosition());
}
public void itemStateChanged(ItemEvent e)
{
int style=Font.PLAIN;
Font font=new Font("宋体",style,15);
if(miFontBold.getState())
style+=Font.BOLD;
String s=text.getText();
text.setFont(new Font("宋体",style,15));
text.setText(s);
}
public void openText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
fd.setVisible(true);
File file=new File(fd.getDirectory()+fd.getFile());
FileReader fr=new FileReader(file);
BufferedReader bufr=new BufferedReader(fr,4096);
text.setText("");
String texts="",s;
while((s=bufr.readLine())!=null)
{
text.setFont(new Font("宋体",Font.PLAIN,15));
texts+=s+"\n";
}
text.setText(texts);
bufr.close();
}
public void saveText() throws IOException
{
FileDialog fd=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
fd.setVisible(true);
File file=new File(fd.getDirectory()+fd.getFile()+".txt");
FileWriter fw=new FileWriter(file);
BufferedWriter bufw=new BufferedWriter(fw);
String s=text.getText();
bufw.write(s,0,s.length());
bufw.flush();
bufw.close();
}
}
public class Jishiben
{
public static void main(String args[])
{
Jishiben1 w=new Jishiben1("文本编辑程序");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询