1. 程式人生 > >字節輸入流-InputStream demo4

字節輸入流-InputStream demo4

lose string類 inpu throw 通過 sta ack 所有 void

package inputstream.cn;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/*
 * 改進版 4
 * 使用read()通過循環讀取
 * byte[] b = new byte[(int)f.length()];
            for (int i = 0; i < b.length; i++) {
                    b[i] = (byte)is.read();
            }
 */
public class
InputStreamDemo4 { public static void main(String[] args) throws Exception { //使用file 找到一個文件 File f = new File("d:"+File.separator+"test.txt"); //通過子類實例化父類 InputStream is =new FileInputStream(f); //開辟一個1024的字節數組,把所有的類容讀到此數組中 byte
[] b = new byte[(int)f.length()]; for (int i = 0; i < b.length; i++) { b[i] = (byte)is.read(); } //關閉輸入流 is.close(); //打印讀的數據,將byte類型轉換為string類型輸出 System.out.println(new String(b)); } }

字節輸入流-InputStream demo4