myfiletest.java

来自「该原代码为<Java编程基础、应用与实例>的附盘代码」· Java 代码 · 共 49 行

JAVA
49
字号
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFile{
  private FileDialog fDlg;
  public MyFile(Frame parent){
    fDlg=new FileDialog(parent,"",FileDialog.LOAD); 
  }
  private String getPath(){
    return fDlg.getDirectory()+"\\"+fDlg.getFile();
  }
  public String getData() throws IOException{
    fDlg.setTitle("凯扁");
    fDlg.setMode(FileDialog.LOAD);
    fDlg.setVisible(true);
    BufferedReader br=
            new BufferedReader(new FileReader(getPath()));
    StringBuffer sb=new StringBuffer();
    String aline;
    while((aline=br.readLine())!=null)
      sb.append(aline+'\n');  
    br.close();   
    return sb.toString();   
  }
  public void setData(String data) throws IOException{
    fDlg.setTitle("历厘");
    fDlg.setMode(FileDialog.SAVE);
    fDlg.setVisible(true);
    BufferedWriter bw=
            new BufferedWriter(new FileWriter(getPath()));  
    bw.write(data);
    bw.close();
  }
}
public class MyFileTest{
  public static void main(String[] args){
    Frame f=new Frame("MyFile Test");
    TextArea ta=new TextArea();
    f.add(ta);
    f.setSize(300,300);
    f.setVisible(true);
    MyFile mf=new MyFile(f);
    try{
      ta.setText(mf.getData());
      mf.setData(ta.getText());
    }catch(IOException ie){}
    
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?