inputStream、File、Byte、String等等之间的相互转换
一: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等等之间的相互转换的更多相关文章
- C# Enum Name String Description之间的相互转换
最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...
- 包装类、基本数据类型及String类之间的相互转换
包装类:8种基本数据类型对应一个类,此类即为包装类 一.基本数据类型 包装类 及String之间的转换 1.基本数据类型转化为包装类:调用包装类的构造器 int i=10; Inte ...
- 关于InputStream 和String对象之间的相互转换
代码如下: package com.xin.stream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- c++中char*\wchar_t*\string\wstring之间的相互转换
string U2A(const wstring& str)//Unicode字符转Ascii字符 { string strDes; if ( str.empty() ) goto __end ...
- C#中List〈string〉和string[]数组之间的相互转换
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
- java int和String类型之间的相互转换
String --> int 第一种方法:int i = Integer.parseInt(s); 第二种方法:int i = Integer.valueOf(s).intValue(); 两种 ...
- android开发之Bitmap 、byte[] 、 Drawable之间的相互转换
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- data和string类型之间的相互转换
package main; import java.text.SimpleDateFormat;import java.util.Date; import freemarker.core.ParseE ...
- List<string>和string[]数组之间的相互转换,需要的朋友可以参考下
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
随机推荐
- post 中文数据到elasticsearch restful接口报json_parse_exception 问题
我们的客户端程序直接调用es 的restful接口, 通过post json数据去查询, 但post数据有中文的时候,有些中文会报异常,有些中文不会 {"error":{" ...
- 2015ACM-ICPC长春E题(hdu5531)题解
一.题意 No response.T_T 二.思路 分$n$为奇数或者偶数讨论. 如果$n$是奇数,列出不等式组:$r_1+r_2=d_{1},r_2+r_3=d_{2},r_3+r_4=d_{3}, ...
- Ubuntu14.04配置jdk1.8.0_25,可切换版本
下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 解压: sudo m ...
- unity3d中物体的控制
一.物体的循环移动和旋转 思路:通过对时间的计算,每隔一段时间让物体旋转,实现来回移动. float TranslateSpeed = 0.02f; float TranslateSpeedTime ...
- IIS7根据PID查找对应的站点
runas /user:administrator cmd cd \Windows\System32\inetsrv appcmd.exe list wp
- springBoot----@ConditionalOnxxx相关注解总结
下面来介绍如何使用@Condition public class TestCondition implements Condition { /** * 只有返回true,才会启用配置 */ pub ...
- 在oracle下如何创建database link全面总结
物理上存放于网络的多个ORACLE数据库,逻辑上可以看成一个单一的大型数据库,用户可以通过网络对异地数据库中的数据进行存取,而服务器之间的协同处理对于工作站用户及应用程序而言是完全透明的,开发人员无需 ...
- Dark theme for Texstudio - TeX - LaTeX
Dark theme for Texstudio ~~~ 1.window系统如下操作 ~~~ 1. texstudio的配置文件texstudio 的配置文件在~/.config/texstudi ...
- windows,linux,esxi系统判断当前主机是物理机还是虚拟机?查询主机序列号命令
参考网站:https://blog.csdn.net/yangzhenping/article/details/49996765 查序列号: http://www.bubuko.com/infodet ...
- JAVA 常用注解( JDK, Spring, AspectJ )
JDK自带注解 @Override 表示当前方法覆盖了父类的方法 @Deprecation 表示方法已经过时,方法上有横线,使用时会有警告 @SuppviseWarnings ...