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" ...
随机推荐
- 【转载】webstorm-前端javascript开发神器中文教程和技巧分享
webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载:百度网盘下载:http://pan.baidu. ...
- php session保存到memcache或者memcached中
本教程叫你如何将php 的session存储在 memcached中,参考了好多网页,发现错误百出,最后自己还是测试成功了,现在将结果结果分享. 1-)系统环境 : elastix2.4 (cento ...
- Lua C++互传结构体实例
转自:http://bbs.csdn.net/topics/350261649 =====main.cpp======= #include "stdio.h" extern &qu ...
- 自己写的一个jQuery对联广告插件
效果图: 文件的位置摆放: 插件的代码: ;(function($){ $.extend({ dLAdv:function(options){ var defaults={ leftType:0,// ...
- JVM内部细节之三:字符串及字符串常量池
本人最近正在面试,然后注意到总是有公司喜欢考String的问题,如字符串连接有几种方式,它们之间有什么不同等问题:要不就是给一段代码问创建了几个对象.那么该不该问呢?我认为当面试有一定工作经验的求职者 ...
- [Python] Hermite 插值
# -*- coding: utf-8 -*- #Program 0.5 Hermite Interpolation import matplotlib.pyplot as plt import nu ...
- python中键值叫唤例子
>>> myDict = {'a':'A','b':'B','c':'C'} >>> myDict {'a': 'A', 'c': 'C', 'b': 'B'} & ...
- django-媒体文件,图片存储
1.settings.py # 媒体文件 MEDIA_ROOT = 'media/'
- android提权
Android的内核就是Linux,所以Android获取root其实和Linux获取root权限是一回事儿. 你 想在Linux下获取root权限的时候就是执行sudo或者su,接下来系统会提示你输 ...
- (16/24) webpack打包后的调试方法
在程序开发中,调试程序是最频繁的,那使用了webpack后,所有的代码都打包到了一起,这给调试带来了困难,但是webpack在设计时就已经考虑好了这点,它支持生产Source Maps来方便我们的调试 ...