日期:2014-05-20 浏览次数:20824 次
NotePaid.java package notepaid; import java.awt.FileDialog; importjava.awt.Frame; importjava.awt.Menu; importjava.awt.MenuBar; importjava.awt.MenuItem; importjava.awt.TextArea; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class NotePaid extends Frameimplements ActionListener{ MenuBarmb ; Menufile,help; MenuItemmiOpen,miSave,miSaveAs,miExist; FileDialogfd_Open,fd_Save,fd_SaveAs; TextArea ta; String path = ""; publicNotePaid(){ Frame f = new Frame("记事本"); mb = new MenuBar(); file = new Menu("文件"); help = new Menu("帮助"); miOpen = new MenuItem("打开"); miSave = new MenuItem("保存"); miSaveAs = new MenuItem("另存为"); miExist = new MenuItem("退出"); ta = new TextArea(); file.add(miOpen); file.add(miSave); file.add(miSaveAs); file.add(miExist); mb.add(file); mb.add(help); miOpen.addActionListener(this); miSave.addActionListener(this); miSaveAs.addActionListener(this); miExist.addActionListener(this); fd_Open= new FileDialog(this,"打开",FileDialog.LOAD); fd_Save = new FileDialog(this, "保存", FileDialog.SAVE); fd_SaveAs = new FileDialog(this, "另存为", FileDialog.SAVE); this.addWindowListener(newWindowAdapter(){ @Override publicvoid windowClosing(WindowEvent e) { System.exit(0); } }); this.setMenuBar(mb); this.add(ta); this.setBounds(400,0,600,750); this.setVisible(true); } publicstatic void main(String[] args) { newNotePaid(); } @Override publicvoid actionPerformed(ActionEvent e) { Stringname = e.getActionCommand(); if(name.equals("打开")){ fd_Open.setVisible(true); getReadPath(fd_Open); }elseif(name.equals("保存")){ /* String d = fd_Save.getDirectory(); String f = fd_Save.getFile(); path = d+f;*/ if((path== null) ||(path.equals(""))){ fd_Save.setVisible(true); getWriteSaveAsPath(fd_Save); }else{ getWriteSavePath(fd_Save); } }elseif(name.equals("另存为")){ fd_SaveAs.setVisible(true); getWriteSaveAsPath(fd_SaveAs); } else if (name.equals("退出")){ System.exit(0); } } public void openFile(String path){ //再打开文件之前将记事本的内容清空。 ta.setText(""); FileReader fr = null; BufferedReaderbr = null; try { fr= new FileReader(path); br = new BufferedReader(fr); Stringcontent = null; while((content = br.readLine())!=null) { ta.append(content); //每读出一行就向该行的末尾处追加一个换行符。 ta.append("\r\n"); } }catch (IOException e) { e.printStackTrace(); }finally{ if(br!= null){ try{ br.close(); }catch (IOException e) { e.printStackTrace(); } } } } public void saveFile(String path){ FileWriter fw = null; BufferedWriter bw = null; try { fw= new FileWriter(path); bw= new BufferedWriter(fw); Stringcontent = ta.getText(); bw.write(content); bw.flush(); }catch (IOException e) { e.printStackTrace(); }finally{ if(bw!=null){ try{ bw.close(); }catch (IOException e) { e.printStackTrace(); } } } } public void saveFileAs(String path){ FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter(path); bw = new BufferedWriter(fw); String content = ta.getText(); bw.write(content); bw.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ if(bw!=null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void getReadPath(FileDialog fd){ String d = fd.getDirectory(); String f = fd.getFile(); if((d!=null &&!d.equals(""))&&(f!=null &&!f.equals(""))){ path = d+f; openFile(path); } } public void getWriteSaveAsPath(FileDialog fd){ String d = fd.getDirectory(); String f = fd.getFile(); if((d!=null && !d.equals(""))&&(f!=null&& !f.equals(""))){ path = d+f; saveFileAs(path); } } public void getWriteSavePath(FileDialog fd){ saveFile(path); } }
好了,到这里,关于流的处理中的字符流部分已经介绍完了,之后会为您介绍关于字节流的操作。