fileinputstream_从Java中的FileInputStream读取字节
以下示例顯示了如何從Java中的FileInputStream讀取字節(jié)。
import java.io.File;import java.io.FileInputStream;public class fileInputStream {
public static void main(String[] args) {
byte[] data = new byte[1024]; //allocates memory for 1024 bytes
//be careful about how to declare an array in Java
int readBytes;
try {
File file = new File("testfile");
file.createNewFile();
FileInputStream in = new FileInputStream(file);
while ((readBytes = in.read(data)) != -1) {
//read(byte[] b)
//Reads some number of bytes from the input stream and stores them into the buffer array b.
System.out.println("read " + readBytes + " bytes, and placed them into temp array named data");
System.out.println("data :" + data[123]);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}}
如果放置一些數據,它將給出以下輸出:
run:
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 1024 bytes, and placed them into temp array named data
read 952 bytes, and placed them into temp array named data
BUILD SUCCESSFUL (total time: 2 seconds)
最后,開發(fā)這么多年我也總結了一套學習Java的資料與面試題,如果你在技術上面想提升自己的話,可以關注我,私信發(fā)送領取資料或者在評論區(qū)留下自己的聯系方式,有時間記得幫我點下轉發(fā)讓跟多的人看到哦。
總結
以上是生活随笔為你收集整理的fileinputstream_从Java中的FileInputStream读取字节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python求超级素数代码_C语言求超级
- 下一篇: hive 创建表_2min快速了解,Hi