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 ????public ????????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[] b2 = new ????????int ????????int ????????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 ????public ????????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[] b2 = new ????????int ????????int ????????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 ????public ????????String s = File.separator; ????????File file = new File("d:"+s+"temp"+s+"test1.txt"); ????????InputStream input = new FileInputStream(file); ????????byte[] b = new ????????int ????????int ????????while((temp=input.read())!=-1){ ????????????b[foot++]=(byte)temp; ????????} ????????System.out.println(temp); ????????System.out.println("["+new String(b)+"]"); ????????input.close(); ????} } |
?