Stream 工具方法

inputstream 转 string

1、使用字符流 
InputStream is = TestZhimaCustomerCertificationInitialize.class.getClassLoader().getResourceAsStream("config/rsa_private_key_pkcs8.pem");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}2、使用字节流
InputStream is = TestZhimaCustomerCertificationInitialize.class.getClassLoader().getResourceAsStream("config/rsa_private_key_pkcs8.pem");
byte b = new byte[1024];
int n;
StringBuffer sb = new StringBuffer();
while((n = is.read(b)) != -1) {
sb.append(new String(b,0,n))
}

 

 

package com.hy.springmvc.utils;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class StreamUtil {public static InputStream stringToInputStream(String str) {InputStream is = null;ByteArrayInputStream bais = null;try {bais = new ByteArrayInputStream(str.getBytes());is = bais;} catch (Exception e) {e.printStackTrace();} finally {try {bais.close();} catch (IOException e) {e.printStackTrace();}}return is;}public static String inputStreamToString(InputStream is) {String ret = null;byte[] b = new byte[1024];int n;ByteArrayOutputStream baos = new ByteArrayOutputStream();try {while ((n = is.read(b)) != -1) {baos.write(b, 0, n);}ret = new String(baos.toByteArray());} catch (Exception e) {e.printStackTrace();} finally {try {baos.close();} catch (IOException e) {e.printStackTrace();}}return ret;}public static OutputStream inputStreamToOutputStream(InputStream is)throws IOException {OutputStream os = new ByteArrayOutputStream();int ch;while ((ch = is.read()) != -1) {os.write(ch);}os.flush();os.close();return os;}public static InputStream outputStreamToInputStream(OutputStream os) {InputStream is = null;ByteArrayOutputStream baos = new ByteArrayOutputStream();baos = (ByteArrayOutputStream) os;is = new ByteArrayInputStream(baos.toByteArray());return is;}}

 

转载于:https://www.cnblogs.com/hy87/p/6519264.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372747.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从0开始学习 GitHub 系列汇总笔记

本文学习自Stromzhang, 原文地址请移步:从0开始学习 GitHub 系列汇总 我的笔记: 0x00 从0开始学习GitHub 系列之[初识GitHub] GitHub 影响力 a.全球顶级科技公司纷纷加入 GitHub ,并贡献他们自己的项目代码 Google: https://github.com/goog…

Drools Guvnor –管理访问

外部化业务或技术规则对于可伸缩应用程序非常重要,但是应该管理BRMS服务访问。 guvnor使用基于角色的授权提供控件UI访问和操作。 在drools-guvnor参考手册中列出了几种权限类型。 具有所有权限的管理员。 分析师或只读分析师:特定类别的分析师权限。 软…

java文件操作和_JAVA文件操作类和文件夹的操作

JAVA文件操作类和文件夹的操作package com.gamvan.tools;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import jav…

CCNA基础知识摘录

cisco设备的启动要点:1、检测硬件(保存在rom)2、载入软件(IOS)(保存在Flash)3、调入配置文件(密码,IP地址,路由协议都保存在此)(此文件保存在NVRAM&#xff0…

【VS开发】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)

1、htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short netshort); ntohl( )-----网络顺序转换成主机顺序 u_long PASCAL FAR htonl (u_long hostlong); u_short PASCAL FAR htons (u_short hostshort); htonl ()-----主机顺序转…

SOA示例应用程序

SOA描述了一组用于创建松散耦合的,基于标准的,与业务相关的服务的模式,由于描述,实现和绑定之间的关注点分离,因此提供了新的灵活性。 近年来,至少在参与大多数信息技术活动的人们中,面向服务的…

java 分贝_java11教程--jhsdb命令

您可以使用该jhsdb工具将Java进程或崩溃的Java虚拟机(JVM)的核心转储连接。概要jhsdb clhsdb [--pid pid | --exe executable --core coredump]jhsdb debugd [options] (pid | executable coredump) [server-id]jhsdb hsdb [--pid pid | --exe executable --core coredump]jhsd…

Oracle使用startup与startup force启动的区别

1. startup 就是正常启动数据库,没什么好说的。 2. startup force 是shutdown abort startup的组合,即强制关闭数据库 正常启动数据库,想快速重启数据库时胆子大的人用的。 startup force测试 在一个窗口执行startup force 命令 点击(此…

js点击button按钮跳转到页面代码

www.111cn.net 编辑:smoke 来源:转载在网页中button按钮不具备a标签的属性了,如果我们要给button 按钮增加跳转事件我们可以在它的确onclick事件上点击跳转js来实现,下面我来给大家介绍一些常用的方法。点击按钮怎么跳转到另外一个页面呢?我们…

Mac OS X上的Java Memcached

介绍 在本文中,我将解释如何: 在Mac OS X上安装和配置Memcached 在Java应用程序中使用Memcached 我不会过多地介绍在您的应用程序中使用分布式缓存的好处,但是至少让我们提供一些在企业门户(在我的情况下为eXo平台)…

Js实现内容向上无缝循环滚动

当前项目有一个类似公告向上滚动的需求,在网上搜索到一个比较棒的程序,现摘录如下: //document.getElementById()的最简化应用 function $(element) {if (arguments.length > 1) {for (var i 0, length arguments.length, elements [];…

Repeater 中TextBox 触发TextChanged事件

两种方法 1.TextBox 绑定TextChanged 并设置AutoPostBack "true" &#xff0c;如果不设置AutoPostBack "true"则不会触发TextChanged事件&#xff0c;然后在对应的方法里面循环Repeater查找改变项 部分代码 <ItemTemplate ><tr><td style &…

什么是Spring Integration?

随着Spring Integration项目逐渐获得越来越多的采用和兴趣&#xff0c;企业集成或企业开发领域的开发人员很可能会遇到它。 他们可能会发现它很有趣&#xff0c;但并没有完全理解它的含义&#xff0c;所要解决的问题&#xff0c;可以从中获取更多信息以及它在ESB和其他SOA基础结…

java jmap mat_java内存调优之jmap,jstack,mat

查看整个JVM内存状态jmap -heap [pid]要注意的是在使用CMS GC 情况下&#xff0c;jmap -heap的执行有可能会导致JAVA 进程挂起查看JVM堆中对象详细占用情况jmap -histo [pid]可以通过jmap -histo [pid] | head -20查看top20的实例个数以及内存占用情况导出整个JVM 中内存信息&a…

lftp

-f&#xff1a;指定lftp指令要执行的脚本文件&#xff1b; -c&#xff1a;执行指定的命令后退出&#xff1b; --help&#xff1a;显示帮助信息&#xff1b; --version&#xff1a;显示指令的版本号。下载 get当然是可以的&#xff0c;还可以&#xff1a; mget -c *.pdf #把所有…

Apache Shiro第1部分–基础

Apache Shiro &#xff08;最初称为JSecurity&#xff09;是Java安全框架。 它被接受并于2010年成为Apache顶级项目。它的目标是功能强大且易于使用。 该项目正在积极开发中&#xff0c;用户和开发人员的邮件列表均处于活动状态。 最重要的区域记录在其网页上。 但是&#xff0…

javascript 之Object内置对象

Object.defineProperty(obj, prop, descriptor)转载于:https://www.cnblogs.com/lihuali/p/5632460.html

java员工编号程序_用JAVA编写一个employee类 为员工自动产生员工号

差不多就是这个感觉..class Employee {/*** param args*/private int number;private String name;private String birthday;static int IDnumber0;public Employee(String name,String birthday){IDnumber;this.setNumber(IDnumber);this.setName(name);this.setBirthday(birt…

分解质因数-洛谷P3200 [HNOI2009]有趣的数列

https://www.luogu.org/problem/show?pid3200 首先&#xff0c;我们不能保证要求的数的逆元和模域互质&#xff1b; 所以我们要用分解质因数来抵消除法&#xff1b; 其实逆元的话即使可行也会超时&#xff1b; 那么我转载了&#xff0c;实在没什么可以说的&#xff1b; 另…

Java中的安全加密

上一次我写关于密码学的文章时 &#xff0c;我概述了Apache Shiro加密API&#xff0c;并展示了如何使用其两个对称密码。 我还写道&#xff1a;“您不需要在应用程序中对敏感数据进行加密和解密。” 我了解了更多有关密码的知识&#xff0c;发现您需要了解更多信息。 我写的内容…