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

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

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

在讀取 InputStream 的時(shí)候,其實(shí)內(nèi)部是有個(gè)指針,它指示每次讀取之后下一次要讀取的起始位置,當(dāng)?shù)谝淮巫x完的時(shí)候,指針已經(jīng)指到最后了,當(dāng)再次讀取的時(shí)候,自然是讀不到數(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)建一個(gè)-------");
        InputStream isOne = new ByteArrayInputStream(builder.toString().getBytes());
        // 下面是測(cè)試新的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)建一個(gè)-------");
        InputStream isTwo = new ByteArrayInputStream(builder.toString().getBytes());
        // 下面是測(cè)試新的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)建一個(gè)-------");
        InputStream isOne = new ByteArrayInputStream(byteArrayOut.toByteArray());
        // 下面是測(cè)試新的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)建一個(gè)-------");
        InputStream isTwo = new ByteArrayInputStream(byteArrayOut.toByteArray());
        // 下面是測(cè)試新的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)建一個(gè)新的 ByteArrayInputStream 對(duì)象,包含復(fù)制的數(shù)據(jù)
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());

        System.out.println("-------第一次使用-------");
        // 下面是測(cè)試新的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 的指針到開(kāi)頭
        byteArrayInputStream.reset();

        System.out.println("-------第二次使用-------");
        // 下面是測(cè)試新的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流的多次讀取的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java InputStream流讀取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

繁昌县| 泸溪县| 色达县| 通山县| 和平区| 襄城县| 通海县| 邳州市| 青川县| 吉水县| 宝鸡市| 车险| 会昌县| 锡林郭勒盟| 临沂市| 南江县| 大方县| 广宗县| 邮箱| 焉耆| 彭州市| 宣恩县| 蓬溪县| 大埔县| 永泰县| 崇义县| 临泉县| 南宫市| 南丹县| 新密市| 井冈山市| 永川市| 德保县| 罗山县| 苍南县| 石渠县| 洞头县| 报价| 皮山县| 蓬安县| 临沂市|