2017年計算機二級考試java章節(jié)輔導:例程:顯示一個文件

字號:


    9.6 例程:顯示一個文件
    如果文件的訪問權限足夠,你可以在TextArea對象里顯示文件內容。
    下面是顯示文件的程序片斷:
    FileInputStream fis;
    TextArea ta;
    public vod init(){
    byte b[] = new byte [1024];
    int I; //make it big enough or wait until you //know the size of the file String s;
    try {
    fis = new FileInputStream("/etc/motd");
    }
    catch(FileNotFoundException e) {
    /*do something appropriate */
    }
    try {
    I= fis.read(b);
    }
    catch(IOException e) {
    /* do something appropriate */
    }
    s = new String(b, 0);
    ta = new TextArea(s,5,40); add (ta);
    }