码迷,mamicode.com
首页 > 其他好文 > 详细

IO:InputStream

时间:2016-09-24 23:35:21      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/j-j-c-c/p/5904437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!