标签:
public abstract class InputStream extends Object implements Closeable
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 | 关闭数据流 |
?
?
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(); ????} } |
?
标签:
原文地址:http://www.cnblogs.com/j-j-c-c/p/5904437.html