<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java 的讀寫問題

    A common question when using the IO classes is why this block of code does not work:


    File aFile = new File("input.data");
    InputStream is = new FileInputStream(aFile);
    byte[] data = new byte[aFile.length()];
    is.read(data);

    The results can be puzzling. If the input file is an image it may be mangled or not display at all. Binary or text data may be incomplete. This is because Read Doesn't Do What You Think It Does. A quick glance at the Java API for java.io.InputStream gives us the following information:


    public int read(byte[] b) throws IOException

    Reads some number of bytes from the input stream and stores them
    into the buffer array b. The number of bytes actually read is returned as an integer. . .

    The documentation states that read reads "some number" of bytes. It is not guaranteed to fill the byte array. The number of bytes read is returned by the read method. One should use this return value to make sure that he is receiving the data which he expects. A correct usage of read() would be to read chunks of a file and collect them in a buffer like so:


    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    File aFile = new File("input.data");
    InputStream is = new FileInputStream(aFile);
    byte[] temp = new byte[1024];
    int read;

    while((read = is.read(temp)) > 0){
       buffer.write(temp, 0, read);
    }
               
    byte[] data = buffer.toByteArray();
    // process the data array . . .

    posted on 2007-11-01 17:28 劉錚 閱讀(226) 評論(0)  編輯  收藏 所屬分類: JAVA General

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    留言簿(1)

    文章分類(141)

    文章檔案(147)

    搜索

    最新評論

    主站蜘蛛池模板: 亚洲伊人tv综合网色| 亚洲va在线va天堂成人| 亚洲成年人免费网站| 亚洲中文字幕无码中文| 亚洲精品成人片在线观看| 久久久久免费看黄a级试看| 中文字幕无码亚洲欧洲日韩| 亚洲精品国产电影| 四虎在线最新永久免费| 四虎精品成人免费视频| 精品日韩亚洲AV无码一区二区三区 | 亚洲美女视频免费| 免费国产高清视频| 99在线观看免费视频| 国产产在线精品亚洲AAVV| 亚洲精品天天影视综合网| 免费v片视频在线观看视频| 日韩免费无码一区二区三区| 国产亚洲综合一区二区三区| 911精品国产亚洲日本美国韩国| 国产免费人视频在线观看免费| 巨波霸乳在线永久免费视频| 精品亚洲av无码一区二区柚蜜| 久久精品亚洲日本佐佐木明希| 免费a级毛片在线观看| 日本黄网站动漫视频免费| 中文成人久久久久影院免费观看| 亚洲乱码在线观看| 久久亚洲精品成人AV| 久久国产成人精品国产成人亚洲| 毛片免费观看的视频在线| 99久久免费精品视频| 国产一精品一AV一免费| 欧亚一级毛片免费看| 久久久久亚洲精品无码网址色欲| 亚洲手机中文字幕| 亚洲AV乱码久久精品蜜桃| 国产精品亚洲产品一区二区三区 | 啦啦啦高清视频在线观看免费| 久草免费手机视频| japanese色国产在线看免费|