1. String --> InputStream
InputStream String2InputStream(String str){
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
return stream;
}
2. 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();
}
今天从网上看到了另一种方法,特拿来分享
String all_content=null;
try {
all_content =new String();
InputStream ins = 获取的输入流;
ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
byte[] str_b = new byte[1024];
int i = -1;
while ((i=ins.read(str_b)) > 0) {
outputstream.write(str_b,0,i);
}
all_content = outputstream.toString();
} catch (Exception e) {
e.printStackTrace();
}
此两种方法上面一种更快,但是比较耗内存,后者速度慢,耗资源少
3、File --> InputStream
InputStream in = new InputStream(new FileInputStream(File));
4、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 对象和 Excel 文档之间进行转换的迅速而灵活的工具 1、Excel导出:支持Java对象装换为Excel,...2、Excel导入:支持Excel转换为Java对象,并且支持File、InputStream、文件路径、Workbook等多种导入方式;
有两种创建XmlToJson对象的方法:从String或从InputStream 。 String xmlString; // some XML String previously created XmlToJson xmlToJson = new XmlToJson . Builder (xmlString) . build(); 或者 ...
第一:创建HttpURLConnection 第二:打开URL,创建一个InputStream 第三:逐行(逐字节)读取,如果需要,转换编码,放入字符串。 好,一下就开始代码吧: 代码如下:public String getUrlContent(String path){ ...
此文档主要的内容是byte类型与各类型(String、boolean、int、inputStream等)之间的转化
public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte...
List<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>( 0);// 存放其他数据集合 for (int i = startRow; i ; i++) {// 循环行 // ;i的范围是xls坐标中的数字(A‘1’,C‘10’) ...
6 String toString() 把此Object对象转换成String类的对象 7 void notify() 唤醒一个等待的线程 8 void notifyAll() 唤醒所有等待的线程 9 void wait(int timeout) 使一个线程处于等待直到timeout结束或被...
* 3.String(byte[] bytes) 根据默认字符集将字节数组转换为字符串 * 4.String(byte[] bytes, String charsetName) * 根据默认字符集将字节数组转换为字符串 * * 这里会有乱码问题: ...
sep4J: Simple Excel Processing for Java , 通过一次静态方法调用完成 excel <-> List之间的转换。 你不必手写任何 POI 相关代码。支持 Maven. 基本示例把数据写入Excel Collection users = Arrays.asList...
String sourceFilePath, String fullFileName, String swfToolsPath, String converterFlag) throws Exception{ File sourceFile; //转换源文件 File pdfFile; //PDF媒介文件 File swfFile; //SWF目标文件 ...
public OutHandler(InputStream is, String type) { br = new BufferedReader(new InputStreamReader(is)); this.type = type; } /** * 重写线程销毁方法,安全的关闭线程 */ @...
inputStream2String, string2InputStream : inputStream 与 string 按编码互转 outputStream2String, string2OutputStream: outputStream 与 string 按编码互转 bitmap2Bytes, bytes2Bitmap : bitmap 与 byteArr 互...
name : string in : InputStream out : OutputStream } import { MutatorIO } from 'mutator-io' import * as MqttInputStream from 'mutator-io-plugin-in-mqtt' import * as DynamoDBOutputStream from '...
// 把要解析的XML文档转化为输入流,以便DOM解析器解析它 InputStream is = new FileInputStream("test.xml"); // 解析XML文档的输入流,得到一个Document Document doc = dombuilder.parse(is); // 得到XML...
平时的java项目中会存在各种对象的互相转换的情况,本项目记录一些常见对象转换的方法,例如:文件转换、日期时间转换、stream流转换、集合对象转换等 文件 Java 为文件操作设计了很多的类,有数据相关的 IO Stream ...
public static Document parseForDoc(final InputStream is) throws SAXException, IOException, ParserConfigurationException, IllegalArgumentException { try { DocumentBuilderFactory factory = ...
则需要在构造函数之外链接递归数据更有力你可以编写转换器代码来在类型之间进行转换,所以基本上你可以拥有所有的东西,所以基本上你可以在解析时注入任何你想要的代码: lazy val ApiTimeStringToDate = ...
StringBufferInputStream 已经被Deprecated,本身就不应该出现在InputStream 部分,主要因为String 应该属于字符流的范围。已经被废弃了,当然输出部分也没有必要需要它了!还允许它存在只是为了保持版本的向下兼容...
//要把流程图转换成java对象 InputStream is=new FileInputStream("D://java_dianli//jbpm//src//leave//leave.zip"); ZipInputStream zis=new ZipInputStream(is); ProcessDefinition pd=ProcessDefinition....