文件类练习题(FileInputStream类)

应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:d:/myjava/Hello.java 内容自己决定);

package stage3;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;

public class Test6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File f = new File("d:/data/Hello.java");
        try(FileInputStream fin = new FileInputStream(f);
        BufferedInputStream bin = new BufferedInputStream(fin);
        PrintStream ps = new PrintStream(System.out); ){
            byte [] b = new byte[(int)f.length()];
            bin.read(b);
            ps.println(new String(b,"gbk"));
        }catch(IOException e) {
            e.printStackTrace();
        }
        
        
    }

}

猜你喜欢

转载自www.cnblogs.com/summerdata/p/10850635.html