java读取输入流
java读取输入流两种
private static byte[] readStream(InputStream in){
if(in==null){
return null;
}
byte[] buffer = null;
try {
int availableLength = 0;
while(availableLength==0){
availableLength = in.available();//可获取的字节流长度
if(availableLength==0&&in.read()==-1){//防止读取流返回为空造成死循环
break;
}
}
buffer = new byte[availableLength];
int readCount = 0;
while(readCount<availableLength){
readCount += in.read(buffer, readCount, availableLength-readCount);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(in!=null){
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer;
}
public static byte[] readStream1(InputStream in) {
byte[] b = null;
ByteArrayOutputStream outSteam = null;
try {
byte[] buffer = new byte[1024 * 4];
outSteam = new ByteArrayOutputStream();
int len = -1;
while ((len = in.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
b = outSteam.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(outSteam!=null){
outSteam.close();
outSteam = null;
}
if(in!=null){
in.close();
in = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return b;
}
java读取输入流的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)
1.实现目标 读取文件,将文件中的数据一行行的取出. 2.代码实现 1).方式1: 通过BufferedReader的readLine()方法. /** * 功能:Java读取txt文件的内容 步骤: ...
- java读取properties配置文件信息
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...
- Java 读取文件到字符串
Java的io操作比较复杂 package cn.outofmemory.util; import java.io.BufferedReader; import java.io.FileInputSt ...
- Java读取txt文件
package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...
- java 读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- java读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- [Java]读取文件方法大全(转)
[Java]读取文件方法大全 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** ...
随机推荐
- Reveal - UI 分析工具
一.安装和简介 a) download url b) Reveal 使用的方法有两种: Static Library Intefration, Dynamic Library Intefration. ...
- python httprequest, locust
r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...
- SEO实战宝典阅读笔记
1. 对搜索引擎更友好 1.1 sitemap sitemap自动生成 https://www.xml-sitemaps.com 谷歌 sitemap.xml 百度 sitemap.html 1.2 ...
- 去除magento多店铺URL地址中的“___from_store=”
magento 的多店铺功能,大多数情况下是根据语言来进行选择的,当添加了多店铺之后,一般情况下我们会选择开启添加store code到url地址中. Magento 自带的这种功能算是比较不错了,但 ...
- 临时存存储页面上的数据---Web存储
HTML5 Web存储的两种方法使用 localStorage和sessionStorage 参考: http://www.cnblogs.com/taoweiji/archive/2012/12/0 ...
- wordpres 自定义comment样式
http://wange.im/diy-wordpress-comment-style.html function mytheme_comment($comment, $args, $depth) { ...
- 使用clearInterval清除计时循环时,最后一次循环还是会执行解决办法
原代码: var interv=setInterval(function(){ alert("setInterval执行"); },2000) clearInterval(inte ...
- LeetCode Hamming Distance
原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta
我直接在父类的构造方法中调用了sessionj结果就报这个错误 搜了好久 让改web.config 可是不起作用 代码如下: public class BasePage:System.Web.UI.P ...
- R12.2.6 installation failed with - Unable to rename database
报错信息: 日志信息:/data/ebsdb/VIS/12.1.0/appsutil/log/VIS_ebstest/12222150.log Phase 3 Rename Database Exec ...