InputStream类(java.io.InputStream)

public abstract class InputStream extends Object implements Closeable

构造方法:public InputStream()

普通方法:

public abstract int read()throws IOException

依次读取单个字节数据,如果没有内容则返回-1

public int read(byte[] b) throws IOException

读出输入流的数据,写入字节数组,返回实际读取到的长度,如果没有内容则返回-1

public int read(byte[] b, int off, int len) throws IOException

读出输入流指定长度的数据,写入字节数组的指定位置,返回实际读取到的长度,如果没有内容则返回-1。但是当指定长度为0时返回0;

public void close()throws IOException

关闭数据流

 

 

FileInputStream(java.io.FileInputStream)

public class FileInputStream extends InputStream

构造方法:

public FileInputStream(File file) throws FileNotFoundException

从文件创建输入流(File类)

public FileInputStream(String name) throws FileNotFoundException

从文件创建输入流(String类)

 

实例:

test1.txt的内容

0123456789abcdefghijklmn

Test2.txt的内容

01234

 

package wiki.jjcc.test.ips;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

 

public
class Test1 {

    public
static
void main(String[] args) throws Exception {

        String s = File.separator;

        File file1 = new File("d:"+s+"temp"+s+"test1.txt");

        File file2 = new File("d:"+s+"temp"+s+"test2.txt");

        InputStream input = new FileInputStream(file1);

        byte[] b1 = new
byte[10];

        byte[] b2 = new
byte[10];

        int
num1 = input.read(b1,3,6);

        int
num2 = input.read(b2,3,6);

        System.out.println(num1);

        System.out.println("["+new String(b1)+"]");

        System.out.println(num2);

        System.out.println("["+new String(b2)+"]");

        input.close();

    }

}

 

package wiki.jjcc.test.ips;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

 

public
class Test1 {

    public
static
void main(String[] args) throws Exception {

        String s = File.separator;

        File file1 = new File("d:"+s+"temp"+s+"test1.txt");

        File file2 = new File("d:"+s+"temp"+s+"test2.txt");

        InputStream input = new FileInputStream(file2);

        byte[] b1 = new
byte[10];

        byte[] b2 = new
byte[10];

        int
num1 = input.read(b1,3,6);

        int
num2 = input.read(b2,3,6);

        System.out.println(num1);

        System.out.println("["+new String(b1)+"]");

        System.out.println(num2);

        System.out.println("["+new String(b2)+"]");

        input.close();

    }

}

package wiki.jjcc.test.ips;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

 

public
class Test1 {

    public
static
void main(String[] args) throws Exception {

        String s = File.separator;

        File file = new File("d:"+s+"temp"+s+"test1.txt");

        InputStream input = new FileInputStream(file);

        byte[] b = new
byte[30];

        int
foot = 0;

        int
temp = 0;

        while((temp=input.read())!=-1){

            b[foot++]=(byte)temp;

        }

        System.out.println(temp);

        System.out.println("["+new String(b)+"]");

        input.close();

    }

}

 

IO:InputStream的更多相关文章

  1. struts2文件下载 出现Can not find a java.io.InputStream with the name的错误

    成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontN ...

  2. 关于Mysql数据库longblob格式数据的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V问题分析

    当数据库字段为blob类型时 ,我们如果使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法需要注意 在向blob字段类型中插入数据时 ...

  3. struts2文件下载出现Can not find a java.io.InputStream with the name的错误

    今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...

  4. 【java】io流之字节输入流:java.io.InputStream类及子类java.io.FileInputStream

    package 文件操作; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impor ...

  5. Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData()

    项目运行的时候,如果报错 Error: Default interface methods are only supported starting with Android N (--min-api ...

  6. java.lang.NoSuchMethodError: org.springframework.util.StreamUtils.emptyInput()Ljava/io/InputStream;

    今天写用spring的MockMvc测试controller的demo时出现了这个错误,条件反射的进行了百度,没有搜到匹配的答案,但给了一些解决问题的思路:首先NoSuchMethodError要不就 ...

  7. ava.io.InputStream & java.io.FileInputStream

    java.io.InputStream & java.io.FileInputStream java.io.InputStream,这个抽象类是表示字节输入流的超类,这个抽象类的共性的方法有: ...

  8. Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

    1.错误描写叙述 八月 14, 2015 4:22:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Excepti ...

  9. java io InputStream 转 byte

    InputStream is ; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024] ...

随机推荐

  1. C# Lamada表达式

    Lambda表达式 "Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量.它可以包含表达式和语句,并且可用于创建委托 ...

  2. 北大poj- 1045

    Bode Plot Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14060   Accepted: 8820 Descri ...

  3. [Linux学习]Shell脚本(1)--函数,输入,if判断(持续更新)+删除空行 +保存当前路径

    1)接收输入 check_status() { echo --Check hi_repo status is ok? y or n read var if [ "$var" != ...

  4. 发布网站时报错:未能将文件xxx复制到xxx,问题处理

    发布时报错提示: 错误 1 未能将文件 UpLoad\images\73CDC40ECCA44550BA8201D2AC187A46.jpg 复制到 obj\Debug\Package\Package ...

  5. Thinkphp模型问题(一)

    很长一段时间,脑海里没有模型和控制器的区别,几乎把所有代码都敲在控制器里边了. 来自于ThinkPhp3.2.3手册模型定义 ​​​​​​​模型类的作用大多数情况是操作数据表的,如果按照系统的规范来 ...

  6. js 正则表达式 转至(七郎's Blog)

    //匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线 var re =new RegExp("^[a-zA-Z][a-zA-Z0-9_]{5,19}$"); if( ...

  7. Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置: String strValue ...

  8. swf格式文件如何修改里面的动作路径或者动作脚本(没有源文件的情况)

    一.UrlActionEditor汉化版,这个工具是非常的简单和使用,直接把你需要需要修改的swf格式的flash文件在这里面打开 二.如果需要更加详细的修改可以下载一个SWFDecompiler4. ...

  9. 十分钟了解分布式计算:Spark

    Spark是一个通用的分布式内存计算框架,本文主要研讨Spark的核心数据结构RDD的设计思路,及其在内存上的容错.内容基于论文 Zaharia, Matei, et al. "Resili ...

  10. CentOS 6.5 升级 GCC 4.9.3

    1. GUN官网下载源代码安装包: gcc-4.9.3.tar.gz 2. 解压安装包,并进入解压后的文件夹: tar -zxvf gcc-4.9.3.tar.gz 3. 使用压缩包中的工具下载依赖: ...