【IO流】17 - 字节流 - available()方法

 

package cn.itcast.io.d.bytestream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ByteStreamDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        
        /*
         * 字节输入流中的方法。
         * available();
         * 该方法可以获取文件的字节大小
         */
        
        FileInputStream fis = new FileInputStream("tempfile\\file.txt");
        
//        System.out.println(fis.available());
        byte[] buf = new byte[fis.available()];//定义了一个刚刚好的数组。 注意:如果文件过大,容易溢出。
                                        //建议缓冲区的长度最好还是1024的整数倍。
        
        fis.read(buf);
        String str = new String(buf);
        System.out.println(str);
        
        
        fis.close();
    }

}

 

posted @ 2018-02-01 18:30  清风拂柳  阅读(611)  评论(0编辑  收藏  举报