<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)

    搜索

    最新評論

    主站蜘蛛池模板: 久久久久免费看黄a级试看| 亚洲人成网站在线播放2019| 国产黄在线播放免费观看| 国产一区二区三区无码免费| 亚洲av无码专区在线观看下载| 处破痛哭A√18成年片免费| 国产精品亚洲精品观看不卡| 无码人妻一区二区三区免费手机| 亚洲人成影院77777| 曰批全过程免费视频在线观看| 亚洲欧洲综合在线| 丁香花免费完整高清观看| 亚洲va在线va天堂成人| 无限动漫网在线观看免费 | 国产成人免费午夜在线观看| 亚洲小说区图片区| 日本最新免费不卡二区在线| 污视频网站免费观看| 亚洲毛片αv无线播放一区| 一级特黄aa毛片免费观看| 亚洲国产精品综合福利专区| 在线看片免费不卡人成视频| 久久亚洲AV成人无码国产电影| 亚洲精品麻豆av| 在免费jizzjizz在线播| 亚洲AV无码一区二区三区牛牛| 免费一级毛片在线观看| 波多野结衣免费一区视频| 亚洲国产精品yw在线观看| 免费在线精品视频| 四虎国产成人永久精品免费| 亚洲国产区男人本色在线观看| 亚洲男人第一无码aⅴ网站| 无码国产精品一区二区免费16| 亚洲 日韩经典 中文字幕 | 国产成人3p视频免费观看| 两个人的视频www免费| 亚洲人和日本人jizz| 亚洲?V无码乱码国产精品| 一级毛片免费视频| 黄页网站在线观看免费|