资源描述:
《java保存功能》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、保存时文件名如file.txt文件格式也要输入!!!注意该程序只能打开txt文件、java源程序文件、html文件等,Word的doc文件是特殊格式文件不能用本程序打开。importjava.io.*;importjava.awt.*;importjava.awt.event.*;publicclassjtxtfm{publicstaticvoidmain(Stringargs[]){jtxtfrmfm=newjtxtfrm();}}classjtxtfrmextendsFrameimplementsActionListener{FileDia
2、logop,sv;Buttonbtn1,btn2,btn3;TextAreatarea;jtxtfrm(){super("读写文件");setLayout(null);setBackground(Color.cyan);setSize(600,300);setVisible(true);btn1=newButton("打开");btn2=newButton("保存");btn3=newButton("关闭");tarea=newTextArea("");add(btn1);add(btn2);add(btn3);add(tarea);tarea
3、.setBounds(30,50,460,220);btn1.setBounds(520,60,50,30);btn2.setBounds(520,120,50,30);btn3.setBounds(520,180,50,30);op=newFileDialog(this,"打开",FileDialog.LOAD);sv=newFileDialog(this,"保存",FileDialog.SAVE);btn1.addActionListener(this);btn2.addActionListener(this);btn3.addAction
4、Listener(this);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){setVisible(false);System.exit(0);}});}publicvoidactionPerformed(ActionEvente){if(e.getSource()==btn1){Stringstr;op.setVisible(true);try{Filef1=newFile(op.getDirectory(),op.getFile());Fi
5、leReaderfr=newFileReader(f1);BufferedReaderbr=newBufferedReader(fr);tarea.setText("");while((str=br.readLine())!=null)tarea.append(str+'');fr.close();}catch(Exceptione1){}}if(e.getSource()==btn2){sv.setVisible(true);try{Filef1=newFile(sv.getDirectory(),sv.getFile());FileWr
6、iterfw=newFileWriter(f1);BufferedWriterbw=newBufferedWriter(fw);Stringgt=tarea.getText();bw.write(gt,0,gt.length());bw.flush();fw.close();}catch(Exceptione2){}}if(e.getSource()==btn3){System.exit(0);}}}