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. 【CMD】

    1.dir 2. set (不带参数) 查看环境变量. SET [variable=[string]] variable  指定环境变量名. string    指定要指派给变量的一系列字符串. 3.

  2. js限制文本框只能输入数字方法小结(转)

    这篇文章主要分享下js代码限制文本框中只能输入数字的多个实例,学习下js控制文本框中输入数字的方法,需要的朋友可以参考下   有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数 ...

  3. 《Java应用程序(Application)》

    在编写Java应用程序(Application)时可以这样: 1,定义包名. 2, 导入相关的包. 3, 定义一个类. 4,定义相关变量. 5,定义构造函数.(在构造函数内调用init()方法和add ...

  4. 第九章 硬件抽象层:HAL

    这一章介绍HAL,全称为Hardware Abstract Layer,即硬件抽象层,它是建立在Linux驱动之上的一套程序库,程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.为A ...

  5. 【zz】matlab 直方图匹配

    原文地址:http://www.cnblogs.com/tiandsp/archive/2012/12/19/2825418.html 直方图匹配或叫做直方图规定化都可以,是把原图像的直方图按照给定的 ...

  6. Log4j简介

    Log4J日志配置详解   一.Log4j简介Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和 ...

  7. oracle length and lengthb

    LENGTH──返回以字符为单位的字符串长度. LENGTHB──返回以字节为单位的字符串长度,它和类型定义中的长度是一个概念,比如你定义的varchar2(10)中的10.在不同的数据库,因为字符集 ...

  8. 每天学习一点点--word-break和word-wrap用法和区别

    有时候一个又臭又长的单词出现在一个并不宽到足以容纳这个单词时会出现内容溢出容器这种情况: <!DOCTYPE html> <html lang="en"> ...

  9. c++ float能到小数点后多少位

    float xiaoshu=0.0000000000000000000000000000000000000000000001; cout<<"xiaoshu"<& ...

  10. 在Web大作业——红十字会管理系统里出现的一个Error

    工程描述:根据用户在前端网页的操作对后台数据库进行查询或更新. 错误描述:当对网页进行多次操作后,网页会报错:“数据库超过最大连接数”. 错误分析:每次打开某一网页,都会运行一段JAVA代码连接数据库 ...