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. Unreal Engine Plugin management

    Be aware to remove any dependencies to any modules related to Editor, or else it will end up with fa ...

  2. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

  3. MFC中ClistCtrl的=NM_CUSTOMDRAW消息

    =NM_CUSTOMDRAW是你点击列表内部是的消息映射: 例如:我想在我删除一行列表的数据,但是删除后下一行数据继续保持高亮状态 void CChildView::OnDel() { int cou ...

  4. docker 源码分析 六(基于1.8.2版本),Docker run启动过程

    上一篇大致了解了docker 容器的创建过程,其实主要还是从文件系统的视角分析了创建一个容器时需要得建立 RootFS,建立volumes等步骤:本章来分析一下建立好一个容器后,将这个容器运行起来的过 ...

  5. centos6.3安装MySQL 5.6(转)

    1.下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择“Source Code”, 用已经注册好的oracle账户登录后才能 ...

  6. windows下scrapy安装

    C:\users\XXXX>easy_install scrapy 出现错误 fatal error C1083: Cannot open include file: 'openssl/aes. ...

  7. CodeVS 线段覆盖1~5

    #include <bits/stdc++.h> using namespace std; ; struct Info{int l,r;}P[Maxn]; int n,Cnt,F[Maxn ...

  8. 当C++多继承遇上类型转换[转]

      1 由来 客户用陈旧的VC++6.0进行项目开发,有一块功能需要我来实现.让一个早就习惯了VS2013的人去使用C++支持不太好的VC6去做开发实在是非常不爽,于是另辟蹊径,打算使用VC++201 ...

  9. Questa Functional Verification-autocheck

    1.AutoCheck analysis introduce Autocheck是自动对RTL代码使用形式验证进行规则检查的检查器,是Questa Verify tools的一部分.Autochenc ...

  10. 在同台电脑上再次安装MySql需要注意的事项

    今天安装了一下mysql,出现的问题主要是在最后一步: msyql 安装到最后一步 start service 错误解决方法 1, 到控制面板里面先把 mysql 删除 . 2. 到 c 盘 C:\P ...