W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
DataInputStream可以從輸入流中讀取Java基本數(shù)據(jù)類型值。
DataInputStream類包含讀取數(shù)據(jù)類型值的讀取方法。例如,要讀取int值,它包含一個(gè)readInt()方法;讀取char值,它有一個(gè)readChar()方法等。它還支持使用readUTF()方法讀取字符串。
以下代碼顯示了如何從文件讀取原始值和字符串。
import java.io.DataInputStream; import java.io.FileInputStream; public class Main { public static void main(String[] args) { String srcFile = "primitives.dat"; try (DataInputStream dis = new DataInputStream(new FileInputStream(srcFile))) { // Read the data in the same order they were written int intValue = dis.readInt(); double doubleValue = dis.readDouble(); boolean booleanValue = dis.readBoolean(); String msg = dis.readUTF(); System.out.println(intValue); System.out.println(doubleValue); System.out.println(booleanValue); System.out.println(msg); } catch (Exception e) { e.printStackTrace(); } } }
上面的代碼生成以下結(jié)果:
java.io.FileNotFoundException: primitives.dat (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at file.main(file.java:8)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: