IO:InputStream

时间:2023-03-09 16:09:01
IO:InputStream

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();

    }

}

IO:InputStream

 

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();

    }

}

IO:InputStream

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