第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java的DataInputStream的readUTF方法是怎么讀取字符串的???

Java的DataInputStream的readUTF方法是怎么讀取字符串的???

皈依舞 2019-03-19 17:19:26
大神們好,我是新手小白,今天在學習Java的IO操作中遇到一個百思不得其解的問題,下面的代碼是今天做的關(guān)于DataInputStream類的練習,我很不解為什么DataInputStream的readUTF方法不需要任何參數(shù),但是卻在讀取的時候可以知道自己讀取多長,不明白他是怎么知道我當初在寫入的時候?qū)懭氲拈L度的,他是靠什么控制讀取的范圍的呀???比如下面的程序,readUTF方法就可以知道讀取"昨天"這兩個字,是怎么知道我就剛好需要讀這兩個字,而不會把下面的內(nèi)容給讀出。很是感謝大家的回答package com.zhang.hello;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class IODataStreamTest {    public static void main(String[] args) throws IOException {        File file=new File("f:/zhang.dat");        if(!file.exists()){            file.createNewFile();        }        DataInputStream dis=new DataInputStream(new FileInputStream(file));        DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));        int testInt=2016;        double testDouble=10.12;        long testLong=20161012;        dos.writeInt(testInt);        dos.writeDouble(testDouble);        dos.writeLong(testLong);        //utf-8 一個中文占3個字節(jié)        dos.writeUTF("昨天");        dos.writeUTF("今天是個好日子");        dos.close();                int testRInt=dis.readInt();        System.out.println(testRInt);        double testRDouble=dis.readDouble();        System.out.println(testRDouble);        long testRLong=dis.readLong();        System.out.println(testRLong);        String testRStr=dis.readUTF();//為什么readUTF知道讀取多長        System.out.println(testRStr);        dis.close();    }}執(zhí)行結(jié)果:201610.1220161012昨天
查看完整描述

2 回答

?
慕碼人8056858

TA貢獻1803條經(jīng)驗 獲得超6個贊

看源碼,調(diào)用的第一句就獲取了長度
int utflen = in.readUnsignedShort();
這個方法的Doc :

Reads two input bytes and returns an int value in the range 0 through

  1. Let a be the first byte read and b be the second byte. The value returned is:

(((a & 0xff) << 8) | (b & 0xff)) This method is suitable for reading
the bytes written by the writeShort method of interface DataOutput if
the argument to writeShort was intended to be a value in the range 0
through 65535. Returns: the unsigned 16-bit value read. Throws:
EOFException - if this stream reaches the end before reading all the

  1. IOException - if an I/O error occurs.

readUTF的Doc:

Reads from the stream in a representation of a Unicode character
string encoded in modified UTF-8 format; this string of characters is
then returned as a String. The details of the modified UTF-8
representation are exactly the same as for the readUTF method of
DataInput.


查看完整回答
反對 回復 2019-04-25
  • 2 回答
  • 0 關(guān)注
  • 898 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號