java-Java 里把 InputStream 转换成 String 的几种方法

2024-05-25 13:18

本文主要是介绍java-Java 里把 InputStream 转换成 String 的几种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 使用 JDK 5 的 Scanner

package cc.unmi.test;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;/*** * @author Unmi* @Creation date: 2013-02-01*/
public class Test {/*** @param args* @throws FileNotFoundException */public static void main(String[] args) throws FileNotFoundException {InputStream inputStream = new FileInputStream("d:/sample.txt");Scanner scanner = new Scanner(inputStream, "UTF-8");String text = scanner.useDelimiter("\\A").next();System.out.println(text);scanner.close();}
}

2. JDK1.4 及之前的 BufferedReader 法

package cc.unmi.test;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;/*** * @author Unmi* @Creation date: 2013-02-01*/
public class Test {/*** @param args* @throws IOException */public static void main(String[] args) throws IOException {InputStream inputStream = new FileInputStream("d:/sample.txt");StringBuilder stringBuilder = new StringBuilder();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));boolean firstLine = true;String line = null; ;while((line = bufferedReader.readLine()) != null){if(!firstLine){stringBuilder.append(System.getProperty("line.separator"));}else{firstLine = false;}stringBuilder.append(line);}System.out.println(stringBuilder.toString());}
}

中间那些判断是不是第一行来决定是否加换行符是些杂音。

3. JDK1.4 及之前的 readBytes 法

package cc.unmi.test;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;/*** * @author Unmi* @Creation date: 2013-02-01*/
public class Test {/*** @throws IOException */public static void main(String[] args) throws IOException {InputStream inputStream = new FileInputStream("d:/sample.txt");byte[] buffer = new byte[2048];int readBytes = 0;StringBuilder stringBuilder = new StringBuilder();while((readBytes = inputStream.read(buffer)) > 0){stringBuilder.append(new String(buffer, 0, readBytes));}System.out.println(stringBuilder.toString());}
}

缓冲区的大小自己根据实际来调,比 BufferedReader 还简洁些,不需管换行符的事情。

本文原始链接 http://unmi.cc/java-convert-inputstream-to-string/, 来自 隔叶黄莺 Unmi Blog

4. Apache commons IOUtils.toString 法

package cc.unmi.test;import java.io.*;import org.apache.commons.io.IOUtils;/*** * @author Unmi* @Creation date: 2013-02-01*/
public class Test {/*** @throws IOException */public static void main(String[] args) throws IOException {InputStream inputStream = new FileInputStream("d:/sample.txt");String text = IOUtils.toString(inputStream);System.out.println(text);}
}

第三方库就是第三方库,人家充分考虑到了你的感受,你对 JDK 库的抱怨,多简洁,一行搞定。IOUtils 还能把内容拷入其他的 Writer 中,如 IOUtils.copy(inputStream, new StringWriter())。

5. Google guava 的  CharStreams 方法

package cc.unmi.test;import java.io.*;import com.google.common.io.CharStreams;/*** * @author Unmi* @Creation date: 2013-02-01*/
public class Test {/*** @throws IOException */public static void main(String[] args) throws IOException {InputStream inputStream = new FileInputStream("d:/sample.txt");String text = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));System.out.println(text);}
}

CharSteams 不是直接作用在 InputSteam 上的,还要靠 InputStreamReader 拱个桥。

6. JDK 7 的 NIO readAllBytes

package cc.unmi.test;import java.io.IOException;
import java.nio.file.*;/*** * @author Unmi* @Creation date: 2013-02-01*/
public class Test {/*** @throws IOException */public static void main(String[] args) throws IOException {byte[] bytes = Files.readAllBytes(Paths.get("d:/sample.txt"));String text = new String(bytes);System.out.println(text);}
}

这让我们相信 JDK  一直还有人在管,虽然不可能象动态语言的方法那么快捷,上面的  readAllBytes 在处理大文件时肯定会很被动的。而 Files.readAllLines 会把文件的内容读入一个 List<String> 对象中,往内存不断放东西就得掂量下内存会不会被爆。在 java.nio.file.* 还有很多新事物可供发掘。

这篇关于java-Java 里把 InputStream 转换成 String 的几种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1001614

相关文章

Spring Boot整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更

Java Lettuce 客户端入门到生产的实现步骤

《JavaLettuce客户端入门到生产的实现步骤》本文主要介绍了JavaLettuce客户端入门到生产的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录1 安装依赖MavenGradle2 最小化连接示例3 核心特性速览4 生产环境配置建议5 常见问题

使用python生成固定格式序号的方法详解

《使用python生成固定格式序号的方法详解》这篇文章主要为大家详细介绍了如何使用python生成固定格式序号,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录生成结果验证完整生成代码扩展说明1. 保存到文本文件2. 转换为jsON格式3. 处理特殊序号格式(如带圈数字)4

Java使用Swing生成一个最大公约数计算器

《Java使用Swing生成一个最大公约数计算器》这篇文章主要为大家详细介绍了Java使用Swing生成一个最大公约数计算器的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下... 目录第一步:利用欧几里得算法计算最大公约数欧几里得算法的证明情形 1:b=0情形 2:b>0完成相关代码第二步:加

Java 的ArrayList集合底层实现与最佳实践

《Java的ArrayList集合底层实现与最佳实践》本文主要介绍了Java的ArrayList集合类的核心概念、底层实现、关键成员变量、初始化机制、容量演变、扩容机制、性能分析、核心方法源码解析、... 目录1. 核心概念与底层实现1.1 ArrayList 的本质1.1.1 底层数据结构JDK 1.7

Java Map排序如何按照值按照键排序

《JavaMap排序如何按照值按照键排序》该文章主要介绍Java中三种Map(HashMap、LinkedHashMap、TreeMap)的默认排序行为及实现按键排序和按值排序的方法,每种方法结合实... 目录一、先理清 3 种 Map 的默认排序行为二、按「键」排序的实现方式1. 方式 1:用 TreeM

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代