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. 当你刷新当前Table时,刷新后如何回到你上一次所在位置呢?

    第一: 在你刷新前保存所在位置的行号 procedure XXXClass.LockPositionEx;begin DisableControls; FHistoryRecNo := 0; FHis ...

  2. x.2

    某些原因,和女朋友分手了,难过 订的M18XR3居然提前了半个多月到货,开心 想想一个人的孤单,还是有点难过 转眼间人生已经过去小半,剩下的除去苟延残喘20年,也就不到20年时间蹦跶.都说人生如戏,既 ...

  3. java反射机制深入详解

    java反射机制深入详解  转自:http://www.cnblogs.com/hxsyl/archive/2013/03/23/2977593.html 一.概念 反射就是把Java的各种成分映射成 ...

  4. 用jQuery判断一个元素的各种状态

    用jQuery判断一个元素是否显示   用jQuery判断一个元素是否显示:$(element).is(":visible"); 类似的,判断一个元素是不是第一个子元素:$(ele ...

  5. document与window的区别

    [window对象] 它是一个顶层对象,而不是另一个对象的属性,即浏览器的窗口. 属性 defaultStatus 缺省的状态条消息 document 当前显示的文档(该属性本身也是一个对象) fra ...

  6. 文本编辑器的使用(ckeditor/ueditor)

    1.下载ckeditor或ueditor 2.编写示例代码 注意:使用ueditor时,需要特殊配置图片路径(图片的配置路径在ueditor/php/config.json文件中) 3.结果 (需要源 ...

  7. Autorun.inf文件(2):改变硬盘分区图标

    改变F盘图标. 原理:在f盘下新建一个Autorun.inf文件,文件内容是 [AutoRun]icon=favicon.ico准备名为favicon.ico图标文件,将其放在工程目录里,设计程序将它 ...

  8. js 文本框只能输入数字

    <input type="text" value="" style="ime-mode:Disabled"  onkeyup=&quo ...

  9. capwap DTSL 加密分析

    1.概述 DTLS即Datagram Transport Layer Security (RFC4347),AP加入AC前,先进行DTLS验证,当AP与AC之间的DTLS握手成功后,AP发出Join请 ...

  10. 学习RaphaelJS矢量图形包--Learning Raphael JS Vector Graphics中文翻译(一)

    (原文地址:http://www.cnblogs.com/idealer3d/p/LearningRaphaelJSVectorGraphics.html) 前面3篇博文里面,我们讲解了一本叫做< ...