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读取输入流的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)

    1.实现目标 读取文件,将文件中的数据一行行的取出. 2.代码实现 1).方式1: 通过BufferedReader的readLine()方法. /** * 功能:Java读取txt文件的内容 步骤: ...

  3. java读取properties配置文件信息

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

  4. Java 读取文件到字符串

    Java的io操作比较复杂 package cn.outofmemory.util; import java.io.BufferedReader; import java.io.FileInputSt ...

  5. Java读取txt文件

    package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...

  6. java 读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  7. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  8. java读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  9. [Java]读取文件方法大全(转)

    [Java]读取文件方法大全   1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**     ...

随机推荐

  1. Mvc自定义路由让支持.html的格式

    前言 在大多时候,我们都需要自定义路由,当我们设置为/list/1.html的时候,有的时候会出现以下异常. routes.MapRoute( "category", // 路由名 ...

  2. MySql连接Visual studio Code First插件

    到mySql官网Downloads==> MySQL on Windows==>MySQL for Visual Studio 下载插件安装即可

  3. CSS3 媒体查询移动设备尺寸 Media Queries for Standard Devices (包括 苹果手表 apple watch)

    /* ----------- iPhone 4 and 4S ----------- */ /* Portrait and Landscape */ @media only screen and (m ...

  4. LeetCode Find All Duplicates in an Array

    原题链接在这里:https://leetcode.com/problems/find-all-duplicates-in-an-array/ 题目: Given an array of integer ...

  5. web前端开发和后端开发有什么区别?

    web前端分为网页设计师.网页美工.web前端开发工程师 首先网页设计师是对网页的架构.色彩以及网站的整体页面代码负责 网页美工只针对UI这块儿的东西,比如网站是否做的漂亮 web前端开发工程师是负责 ...

  6. mac brew 安装php扩展报错:parent directory is world writable but not sticky

    $ brew install php70-mcrypt 报错: Error: parent directory is world writable but not sticky 搜索到github的答 ...

  7. Oracle 正则表达式函数-REGEXP_INSTR 使用例子

    原文在这 戳 REGEXP_INSTR 6个参数 第一个是输入的字符串 第二个是正则表达式 第三个是标识从第几个字符开始正则表达式匹配.(默认为1) 第四个是标识第几个匹配组.(默认为1) 第五个是指 ...

  8. category用法

    1.今天在复习之前的category的时候,遇到一个这样子的问题.查来一下,原来苹果的官方文档中有说明,苹果时不提倡我们在类别中重写原文件的内容,如果要重写,就继承他,然后重写,但是在项目中,有很多前 ...

  9. ubuntu配置tftp服务

    ubuntu配置TFTP服务: TFTP是用来下载远程文件的最简单的网络协议,基于UDP协议.xinetd是新一代的网络守护进程服务程序,经常用于管理多种轻量型internet服务. sudo apt ...

  10. xhtml 和 html 的区别

    xhtml遵循xml文档规则,对于书写比较严格,相对于html来说,主要有以下不同: 标签不能重叠,可以嵌套标签与属性都要小写标签都要有始有终,要么以</p>形式结束,要么以<br ...