一:inputStream转换

1、inputStream转为byte

//方法一  org.apache.commons.io.IOUtils包下的实现(建议)
IOUtils.toByteArray(inputStream);
//方法二 用java代码实现(其实就是对上面方法一的解析)
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}

2、inputStream转为file

    public static void inputstreamtofile(InputStream ins, File file) throws IOException {
OutputStream os = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}

3、inputStream转为String

//方法一  使用org.apache.commons.io.IOUtils包下的方法
IOUtils.toString(inputStream); //方法二
/**
* 将InputStream转换成某种字符编码的String
*
* @param in
* @param encoding
* @return
* @throws Exception
*/
public static String inputStreamToString(InputStream in, String encoding) {
String string = null;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
try {
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
} catch (IOException e) {
logger.error("io异常", e.fillInStackTrace());
} try {
string = new String(outStream.toByteArray(), encoding);
} catch (UnsupportedEncodingException e) {
logger.error("字符串编码异常", e.fillInStackTrace());
}
return string;
}

二:byte[]转换

1、byte[]转为inputStream

InputStream sbs = new ByteArrayInputStream(byte[] buf); 

2、byte[]转为File

  public static void byte2File(byte[] buf, String filePath, String fileName)
{
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try
{
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory())
{
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (bos != null)
{
try
{
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

3、byte[]转String

//先将byte[]转为inputStream,然后在转为String
InputStream is = new ByteArrayInputStream(byte[] byt);
//然后在根据上文提到的将inputStream转为String的方法去转换

三、File的转换

1、file转inputStream

FileInputStream fileInputStream = new FileInputStream(file);

inputStream、File、Byte、String等等之间的相互转换的更多相关文章

  1. C# Enum Name String Description之间的相互转换

    最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...

  2. 包装类、基本数据类型及String类之间的相互转换

    包装类:8种基本数据类型对应一个类,此类即为包装类 一.基本数据类型 包装类 及String之间的转换 1.基本数据类型转化为包装类:调用包装类的构造器      int i=10;     Inte ...

  3. 关于InputStream 和String对象之间的相互转换

    代码如下: package com.xin.stream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  4. c++中char*\wchar_t*\string\wstring之间的相互转换

    string U2A(const wstring& str)//Unicode字符转Ascii字符 { string strDes; if ( str.empty() ) goto __end ...

  5. C#中List〈string〉和string[]数组之间的相互转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...

  6. java int和String类型之间的相互转换

    String --> int 第一种方法:int i = Integer.parseInt(s); 第二种方法:int i = Integer.valueOf(s).intValue(); 两种 ...

  7. android开发之Bitmap 、byte[] 、 Drawable之间的相互转换

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  8. data和string类型之间的相互转换

    package main; import java.text.SimpleDateFormat;import java.util.Date; import freemarker.core.ParseE ...

  9. List<string>和string[]数组之间的相互转换,需要的朋友可以参考下

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...

随机推荐

  1. appium 3-31626 toast识别

    1.toast弹窗,普通方式不能获取 例如使用getPageSource是无法找到toast的信息,uiautomatorViewer加载页面时间较长,也很难采集到toast信息 2.通过curl命令 ...

  2. 协同过滤CF算法之入门

    数据规整 首先将评分数据从 ratings.dat 中读出到一个 DataFrame 里: >>> import pandas as pd In [2]: import pandas ...

  3. 第12章 网络基础(2)_数据封装和IP地址

    4. 数据封装和IP地址 (1)数据封装 (2)IP地址 ①在TCP/IP网络中,每个主机都有唯一的地址,它是通过IP协议族实现的. ②IP协议要求在每次与TCP/IP网络建立连接时,每台主机都必须为 ...

  4. storm的流分组

    用的是ShuffleGrouping分组方式,并行度设置为3 这是跑下来的结果 参考代码StormTopologyShufferGrouping.java package yehua.storm; i ...

  5. Jenkins 邮件发送设置(jenkins自带邮件设置)

    首先进入系统设置,找到Jenkins Location部分 这里设置 系统管理员邮件地址,然后设置邮件通知部分,这里为了方便我使用了QQ邮箱(作为发送邮件地址) 这里的 用户名 必须与上面的 系统管理 ...

  6. aspx后台代码写在前段

    合并cs的代码 <%@ Page Language="C#" AutoEventWireup="true" Inherits="System.W ...

  7. robot framework添加库注意事项

    添加库 假设你的项目结构是这样: 项目 ..myLib(库目录) ..目录1 ..测试用例套件1 此时你需要在“测试用例套件1”中用相对路径添加库myLib,你应该填:../myLib/ 特别注意后面 ...

  8. maven 在pom.xml 中指定仓库位置

    ...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...

  9. VBA 选择文件

    Private Function SelectFile(ByVal strFilter As String) As String    Dim FileName As Variant     '打开文 ...

  10. Life is in the little things --- Spreading wildly on Facebook

    这是在FaceBook上疯传的一组图 简笔画的图画的不算精细 但却狠狠地击中许多人的心灵   有时候生活中简单的一件小事, 恰恰是使得你的人生变得更有意义的一件大事! 别人总告诉你 人生是这样的 ▼ ...