最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中InputStream流的多次讀取的實現(xiàn)示例

 更新時間:2026年01月26日 09:11:12   作者:一線大碼  
本文主要介紹了Java中InputStream流的多次讀取的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在讀取 InputStream 的時候,其實內(nèi)部是有個指針,它指示每次讀取之后下一次要讀取的起始位置,當(dāng)?shù)谝淮巫x完的時候,指針已經(jīng)指到最后了,當(dāng)再次讀取的時候,自然是讀不到數(shù)據(jù)的。

1. 使用 StringBuilder 中轉(zhuǎn)

    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\wangb\\Desktop\\test.txt";
        // 使用Files.newInputStream方法獲取InputStream
        InputStream is = Files.newInputStream(Paths.get(filePath));

        // builder 保留讀到的數(shù)據(jù)
        StringBuilder builder = new StringBuilder();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            builder.append(new String(buffer, 0, len));
        }

        System.out.println("-------第一次需要InputStream,那么就創(chuàng)建一個-------");
        InputStream isOne = new ByteArrayInputStream(builder.toString().getBytes());
        // 下面是測試新的InputStream到底是否有數(shù)據(jù)
        byte[] bufferOne = new byte[1024];
        int lenOne;
        while ((lenOne = isOne.read(bufferOne)) != -1) {
            System.out.println(new String(bufferOne, 0, lenOne));
        }

        System.out.println("-------第二次需要InputStream,那么就再創(chuàng)建一個-------");
        InputStream isTwo = new ByteArrayInputStream(builder.toString().getBytes());
        // 下面是測試新的InputStream到底是否有數(shù)據(jù)
        byte[] bufferTwo = new byte[1024];
        int lenTwo;
        while ((lenTwo = isTwo.read(bufferTwo)) != -1) {
            System.out.println(new String(bufferTwo, 0, lenTwo));
        }
        is.close();
        isOne.close();
        isTwo.close();
    }

2. 使用 ByteArrayOutputStream 中轉(zhuǎn)

    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\wangb\\Desktop\\test.txt";
        // 使用Files.newInputStream方法獲取InputStream
        InputStream is = Files.newInputStream(Paths.get(filePath));

        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        int len;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            byteArrayOut.write(buffer, 0, len);
        }

        System.out.println("-------第一次需要InputStream,那么就創(chuàng)建一個-------");
        InputStream isOne = new ByteArrayInputStream(byteArrayOut.toByteArray());
        // 下面是測試新的InputStream到底是否有數(shù)據(jù)
        byte[] bufferOne = new byte[1024];
        int lenOne;
        while ((lenOne = isOne.read(bufferOne)) != -1) {
            System.out.println(new String(bufferOne, 0, lenOne));
        }

        System.out.println("-------第二次需要InputStream,那么就再創(chuàng)建一個-------");
        InputStream isTwo = new ByteArrayInputStream(byteArrayOut.toByteArray());
        // 下面是測試新的InputStream到底是否有數(shù)據(jù)
        byte[] bufferTwo = new byte[1024];
        int lenTwo;
        while ((lenTwo = isTwo.read(bufferTwo)) != -1) {
            System.out.println(new String(bufferTwo, 0, lenTwo));
        }
        is.close();
        isOne.close();
        isTwo.close();
    }

3. 使用 ByteArrayInputStream 重置指針

    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\wangb\\Desktop\\test.txt";
        // 使用Files.newInputStream方法獲取InputStream
        InputStream is = Files.newInputStream(Paths.get(filePath));

        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        int len;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            byteArrayOut.write(buffer, 0, len);
        }

        // 創(chuàng)建一個新的 ByteArrayInputStream 對象,包含復(fù)制的數(shù)據(jù)
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());

        System.out.println("-------第一次使用-------");
        // 下面是測試新的InputStream到底是否有數(shù)據(jù)
        byte[] bufferOne = new byte[1024];
        int lenOne;
        while ((lenOne = byteArrayInputStream.read(bufferOne)) != -1) {
            System.out.println(new String(bufferOne, 0, lenOne));
        }

        // 重置 ByteArrayInputStream 的指針到開頭
        byteArrayInputStream.reset();

        System.out.println("-------第二次使用-------");
        // 下面是測試新的InputStream到底是否有數(shù)據(jù)
        byte[] bufferTwo = new byte[1024];
        int lenTwo;
        while ((lenTwo = byteArrayInputStream.read(bufferTwo)) != -1) {
            System.out.println(new String(bufferTwo, 0, lenTwo));
        }
        is.close();
        byteArrayInputStream.close();
    }

到此這篇關(guān)于Java中InputStream流的多次讀取的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java InputStream流讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

西充县| 独山县| 延庆县| 娄底市| 镇雄县| 太仆寺旗| 广东省| 渝北区| 论坛| 钟祥市| 中卫市| 威海市| 洞头县| 甘洛县| 寻甸| 彩票| 逊克县| 治县。| 贵阳市| 个旧市| 新巴尔虎左旗| 夹江县| 梨树县| 万年县| 茌平县| 双峰县| 公安县| 永州市| 紫阳县| 凭祥市| 左贡县| 宜宾市| 准格尔旗| 镇宁| 柘城县| 平顺县| 乃东县| 阜阳市| 涿州市| 上蔡县| 嘉峪关市|