黑马程序员_打开文件 存储文件

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
class practice27 

{
private Frame f;
private File fe;
private MenuBar mb;
private Menu m;
private MenuItem mload,msave,mclose;
private TextArea ta;
practice27()
{
ini();
}
public void ini()
{
f=new Frame();
ta=new TextArea();
mb=new MenuBar();
m=new Menu("文件");
mload=new MenuItem("打开");
msave=new MenuItem("保存");
mclose=new MenuItem("关闭");
f.setBounds(200,100,500,400);
f.setLayout(new FlowLayout());
f.add(ta);
f.setMenuBar(mb);
mb.add(m);
m.add(mload);
m.add(msave);
m.add(mclose);
method();
f.setVisible(true);
}
public void method()
{
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
mclose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
mload.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
FileDialog fdg=new FileDialog(f,"",FileDialog.LOAD);
fdg.setVisible(true);
String dirpath=fdg.getDirectory();
String filepath=fdg.getFile();

if(dirpath==null||filepath==null)
return;
ta.setText("");
fe=new File(dirpath+filepath);
String line;
BufferedReader bis=new BufferedReader(new InputStreamReader(new FileInputStream(fe)));
while((line=bis.readLine())!=null)
{
//String sb=new String(b,0,len);
ta.append(line+"\r\n");
System.out.println(ta.getText());
}
}
catch (Exception ex)
{
throw new RuntimeException("。。。。");
}

}



}
);
msave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try
{
String textstr=ta.getText();
if(fe==null)
{
FileDialog fdg=new FileDialog(f,"",FileDialog.SAVE);
fdg.setVisible(true);
String dirpath=fdg.getDirectory();
String filepath=fdg.getFile();
if(dirpath==null||filepath==null)
return;
else{
fe=new File(dirpath,filepath);}}
PrintStream ps=new PrintStream(new FileOutputStream(fe),true);
ps.println(textstr);
}
catch (Exception ex)
{
throw new RuntimeException(";;;;");
}
}
});


}
public static void main(String[] args) 
{
new practice27();
}

}
---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值