io之FileInputStream
程序员文章站
2022-05-28 16:50:53
...
package com.laien.io; import java.io.FileInputStream; import java.io.InputStream; /** * 从硬盘中把数据读到内存中,谓之INPUT * @author Administrator * */ public class InputStreamTest { public static void main(String[] args) throws Exception { InputStream fileInputStream = new FileInputStream("D:/test.txt"); byte[] buffer = new byte[200]; //每次读到的长度 int length = 0; //流写下到buffer中,从0开始,每次最多读200字节,返回读取的长度,读取完成后,长度会返回-1 while (-1 != (length = fileInputStream.read(buffer, 0, 200))) { String str = new String(buffer, 0, length); System.out.print(str); } fileInputStream.close(); } }