Java InputStream、String、File相互转化
String --> InputStream
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
InputStream --> String
String inputStream2String(InputStream is){
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line);
}
return buffer.toString();
}
File --> InputStream
InputStream in = new FileInputStream(file);
InputStream --> File
public void inputstreamtofile(InputStream ins,File file){
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
Java InputStream、String、File相互转化的更多相关文章
- Java InputStream转File
文件处于磁盘上或者流处于内存中 在输入流有已知的和预处理的数据时,如在硬盘上的文件或者在流处于内存中.这种情况下,不需要做边界校验,并且内存容量条件允许的话,可以简单的读取并一次写入. InputSt ...
- Java:String,int相互转化
int转String int a: a + “” String.valueOf(a) Interger.toString(a) 一般使用以上几种方法进行转化 第一种方法效率不好,ja ...
- Java InputStream、String、File相互转化 --- good
String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...
- inputStream、File、Byte、String等等之间的相互转换
一:inputStream转换 1.inputStream转为byte //方法一 org.apache.commons.io.IOUtils包下的实现(建议) IOUtils.toByteArray ...
- @RequestParam注解使用:Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
详细错误信息 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Re ...
- java中InputStream String
Java 中获取输入流时,有时候须要将输入流转成String,以便获取当中的内容 ,以下总结一下 InputStream 转成String 的方式 方法1: public String conver ...
- InputStream,String相互转化
String --> InputStream InputStream String2InputStream(String str){ ByteArrayInputStream stream = ...
- java常用string inputStream转换
1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInp ...
- Java学习笔记——File类之文件管理和读写操作、下载图片
Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...
随机推荐
- Cocos2d-x-html5之HelloWorld深入分析与调试
Cocos2d-x-html5之HelloWorld深入分析与调试 另:本章所用Cocos2d-x版本为: Cocos2d-html5-v2.1.1 http://cn.cocos2d-x.org/d ...
- hoj2188 WordStack
WordStack My Tags (Edit) Source : Mid-Atlantic 2005 Time limit : 5 sec Memory limit : 32 M S ...
- button 获取 cell
- (void)cellBtnClicked:(id)sender event:(id)event { NSSet *touches =[event allTouches]; ...
- 51Nod 1126 求递推序列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...
- java利用myeclipse自带三大框架搭建三大框架(Hibernate+Struts2+Spring)过程详解
搭建过程因人而异,我的搭建过程大致是这样的: 1.创建一个javaweb项目: 2.导入Spring框架,上图: 2.1: 2.2: 2.3: 3.导入struts2框架,上图: 3.1: 3.2: ...
- [題解]51nod_1515_明辨是非
好久沒有話多了,是覺得有點浪費時間,今天考試和一中用的一樣的題,結果反而考得不好,不過Jackpei一句知恥而後勇點醒夢中人偷偷@Jackpei 就是這樣吧 還有我極度懷疑我的鍵帽打油了......我 ...
- 转 PHP界面显示中文乱码
D:\wamp64\trainning\testD:\wamp64\www\practice php 页面前端显示乱码 在写一个表单提交的项目中,遇到了PHP界面输出无法显示中文界面. 后来查阅相关资 ...
- nodejs 学习(2) 中间件
var connect=require('connect'), morgan=require('morgan'),//日志 bodyparser=require('body-parser'), ses ...
- React 实践记录 02 Flux introduction
Introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出. 一旦稍微多了解一点React,很难避免听到Flux这个名词. Flux是一 ...
- spark常用参数
val conf = new SparkConf().setAppName("WordCount_groupBy").setMaster("local") // ...