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();
}
}}
如果放置一些數(shù)據(jù),它將給出以下輸出:
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)
最后,開(kāi)發(fā)這么多年我也總結(jié)了一套學(xué)習(xí)Java的資料與面試題,如果你在技術(shù)上面想提升自己的話,可以關(guān)注我,私信發(fā)送領(lǐng)取資料或者在評(píng)論區(qū)留下自己的聯(lián)系方式,有時(shí)間記得幫我點(diǎn)下轉(zhuǎn)發(fā)讓跟多的人看到哦。
總結(jié)
以上是生活随笔為你收集整理的fileinputstream_从Java中的FileInputStream读取字节的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python求超级素数代码_C语言求超级
- 下一篇: Java充电宝模型设计_继续探讨点赞功能