<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 劉錚 閱讀(232) 評論(0)  編輯  收藏 所屬分類: JAVA General

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    留言簿(1)

    文章分類(141)

    文章檔案(147)

    搜索

    最新評論

    主站蜘蛛池模板: 182tv免费视频在线观看 | 日韩精品人妻系列无码专区免费| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 精品乱子伦一区二区三区高清免费播放| 一级做a爱过程免费视| 50岁老女人的毛片免费观看| 国产午夜亚洲精品午夜鲁丝片| 免费看美女午夜大片| 2022久久国产精品免费热麻豆| 久久夜色精品国产亚洲AV动态图| 亚洲国产精品综合久久20| 免费成人激情视频| 亚洲线精品一区二区三区影音先锋| 成年大片免费高清在线看黄| 亚洲视频在线免费| 叮咚影视在线观看免费完整版| 亚洲欧洲精品成人久久曰影片| 四虎影视在线看免费观看| 久久久青草青青国产亚洲免观| 在线观看免费无码视频| 亚洲国产一成人久久精品| 亚洲a∨国产av综合av下载| 国产大片51精品免费观看| 久久亚洲AV无码精品色午夜麻豆| 九九免费精品视频在这里| 夜夜春亚洲嫩草影院| 免费A级毛片无码视频| 自拍日韩亚洲一区在线| 日韩免费视频播放| 亚洲一卡2卡4卡5卡6卡残暴在线| 国产精品免费观看久久| 野花视频在线官网免费1| 亚洲精品国产品国语在线| 免费AA片少妇人AA片直播| 国产AV无码专区亚洲AV麻豆丫| 在线观看av永久免费| 亚洲一区二区影视| 亚洲欧洲久久久精品| 黄在线观看www免费看| 麻豆亚洲AV成人无码久久精品 | 亚洲国产片在线观看|