本文主要给大家介绍java的InputStream 流的使用。
(1)FileInputstream: 子类,读取数据的通道
使用步骤:
1.获取目标文件:new File()
2.建立通道:new FileInputString()
3.读取数据:read()
4.释放资源:close()
1 2 3 4 | import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 | public static void main(String[] args) throws IOException {
test1();
System.out.println( "-------------------------------------------" );
test2();
System.out.println( "-------------------------------------------" );
test3();
System.out.println( "-------------------------------------------" );
test4();
}
|
登录后复制
(2)读取数据的三种方式
1.直接读取 (一次只能一个字节)
1 2 | int date = fileInputStream.read();
char date3 = (char)fileInputStream.read();
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void test1() throws IOException{
File file = new File( "C:\\Users\\joke\\Desktop\\Demo1.java" );
FileInputStream fileInputStream = new FileInputStream(file);
int date = fileInputStream.read();
int date2 = fileInputStream.read();
char date3 = (char)fileInputStream.read();
System.out.println( date + "\\" +date2+ "\\" +date3);
fileInputStream.close();
}
|
登录后复制
2.单独使用for循环(效率低)
1 2 3 | for (int i = 0; i < file.length();i++){
System.out. print ((char)fileInputStream.read());
}
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void test2() throws IOException{
long startTime = System.currentTimeMillis();
File file = new File( "C:\\Users\\joke\\Desktop\\Demo1.java" );
FileInputStream fileInputStream = new FileInputStream(file);
for (int i = 0; i < file.length();i++){
System.out. print ((char)fileInputStream.read());
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println( "读取文件所花时间:" +(endTime-startTime));
}
|
登录后复制
3.Byte[ ] 缓冲区(只能读取指定的字节数不能读取一个完整的文件)
1 2 3 | byte[] bt = new byte[1024];
int count = fileInputStream.read(bt);
System.out.println( new String (bt,0, count ));
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 | public static void test3() throws IOException{
File file = new File( "C:\\Users\\joke\\Desktop\\Demo1.java" );
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bt = new byte[1024];
int count = fileInputStream.read(bt);
System.out.println( count );
System.out.println( new String (bt,0, count ));
fileInputStream.close();
}
|
登录后复制
4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
1 2 3 4 5 | byte[] bt = new byte[1024];
int count = 0;
while (( count = fileInputStream.read(bt)) != -1){
System.out.println( new String (bt,0, count ));
}
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void test4() throws IOException{
long startTime = System.currentTimeMillis();
File file = new File( "C:\\Users\\joke\\Desktop\\Demo1.java" );
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bt = new byte[1024];
int count = 0;
while (( count = fileInputStream.read(bt)) != -1){
System.out.println( new String (bt,0, count ));
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println( "读取文件所花时间:" +(endTime-startTime));
}
|
登录后复制
在以上,对比第二个和第四个方法,会发现方法四的效率是比较高的,所以推荐使用的四个方法
在这里我们是直接抛出异常,除了抛出之外我们还可以使用
try{ }cater{ }finally{ }
的方式来处理异常
以上所述是小编给大家介绍的java IO流 之 输入流 InputString()的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHP中文网的支持!
更多java IO流 之 输入流 InputString()的使用相关文章请关注PHP中文网!