zoukankan      html  css  js  c++  java
  • Java中InputStream输入流转String字符串的操作

    一、InputStream类中read方法

    package com.zhiyin.test;
    
    import java.io.InputStream;
    
    public class MyTest {
        public static void main(String[] args) {
            MyTest myTest = new MyTest();
            myTest.test();
        }
    
        public void test() {
            try {
                // 读取测试文件
                MyTest test = new MyTest();
                InputStream is = test.getClass().getResourceAsStream("testFile.txt");
                byte[] byteArr = new byte[is.available()];
                is.read(byteArr);
                String str = new String(byteArr);
                System.out.println(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

    二、开源工具类IOUtils中toString方法

    maven项目中pom.xml文件里引入依赖:

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    

    演示代码如下:

    package com.zhiyin.test;
    
    import org.apache.commons.io.IOUtils;
    
    import java.io.InputStream;
    
    public class MyTest {
        public static void main(String[] args) {
            MyTest myTest = new MyTest();
            myTest.test();
        }
    
        public void test() {
            try {
                // 读取测试文件
                MyTest test = new MyTest();
                InputStream is = test.getClass().getResourceAsStream("testFile.txt");
                
                String str = IOUtils.toString(is);
                System.out.println(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

    三、开源工具类CharStreams中toString方法

    maven项目中pom.xml文件里引入依赖:

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>28.1-jre</version>
    </dependency>
    

    演示代码如下:

    package com.zhiyin.test;
    
    import com.google.common.io.CharStreams;
    
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class MyTest {
        public static void main(String[] args) {
            MyTest myTest = new MyTest();
            myTest.test();
        }
    
        public void test() {
            try {
                // 读取测试文件
                MyTest test = new MyTest();
                InputStream is = test.getClass().getResourceAsStream("testFile.txt");
                // 字节输入流转字符输入流
                InputStreamReader isr = new InputStreamReader(is);
                // CharStreams.toString()方法转换字符输入流
                String str = CharStreams.toString(isr);
                System.out.println(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    MySQL 触发器
    MySQL视图
    MySQL中的存储过程和函数
    Mysql数据库连接查询
    数据库(MySQL)表基本操作
    Spring MVC---数据绑定和表单标签
    Spring MVC---基于注解的控制器
    Spring基于AOP的事务管理
    Ubuntu下通过wine安装HeidiSQL
    chmod命令详解
  • 原文地址:https://www.cnblogs.com/zhiyin1209/p/12600499.html
Copyright © 2011-2022 走看看