java IO流 之 输入流 InputString()的使用
本文主要给大家介绍java的InputStream流的使用。
(1)FileInputstream:子类,读取数据的通道
使用步骤:
1.获取目标文件:newFile()
2.建立通道:newFileInputString()
3.读取数据:read()
4.释放资源:close()
//一些默认要导入的包 importjava.io.File; importjava.io.FileInputStream; importjava.io.IOException;
publicstaticvoidmain(String[]args)throwsIOException{ //TODOAuto-generatedmethodstub //分别调用方法查看效果 test1(); System.out.println("-------------------------------------------"); test2(); System.out.println("-------------------------------------------"); test3(); System.out.println("-------------------------------------------"); test4(); }
(2)读取数据的三种方式
1.直接读取(一次只能一个字节)
intdate=fileInputStream.read(); chardate3=(char)fileInputStream.read();
//方式一直接打印 publicstaticvoidtest1()throwsIOException{ //(1)获取目标文件路径 Filefile=newFile("C:\\Users\\joke\\Desktop\\Demo1.java"); //(2)根据目标文件路径建立通道:newFileInputStream(file) FileInputStreamfileInputStream=newFileInputStream(file); //(3)读取数据:read(); intdate=fileInputStream.read();//这里是int类型 intdate2=fileInputStream.read();// chardate3=(char)fileInputStream.read();//以char类型显示 System.out.println(date+"\\"+date2+"\\"+date3); //(4)释放资源 fileInputStream.close(); }
2.单独使用for循环(效率低)
for(inti=0;i<file.length();i++){ System.out.print((char)fileInputStream.read()); }
//方式二循环遍历 publicstaticvoidtest2()throwsIOException{ //通过时间测试效率 longstartTime=System.currentTimeMillis(); Filefile=newFile("C:\\Users\\joke\\Desktop\\Demo1.java"); FileInputStreamfileInputStream=newFileInputStream(file); //for循环 for(inti=0;i<file.length();i++){ System.out.print((char)fileInputStream.read()); } fileInputStream.close(); longendTime=System.currentTimeMillis(); System.out.println("读取文件所花时间:"+(endTime-startTime)); }
3.Byte[]缓冲区(只能读取指定的字节数不能读取一个完整的文件)
byte[]bt=newbyte[1024]; intcount=fileInputStream.read(bt); System.out.println(newString(bt,0,count));
//方式三创建缓冲区(只能读取制定的大小,不能读取一个完整的文件) publicstaticvoidtest3()throwsIOException{ Filefile=newFile("C:\\Users\\joke\\Desktop\\Demo1.java"); FileInputStreamfileInputStream=newFileInputStream(file); //创建缓冲区,加快读取数据,确定要读取的字节大小 byte[]bt=newbyte[1024]; //read()读取字节 intcount=fileInputStream.read(bt); System.out.println(count);//显示读取到的字节数 System.out.println(newString(bt,0,count));//将字节转为字符串显示 fileInputStream.close(); }
4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
byte[]bt=newbyte[1024]; intcount=0; while((count=fileInputStream.read(bt))!=-1){ System.out.println(newString(bt,0,count)); }
//方式四循环与缓冲区结合(效率高) publicstaticvoidtest4()throwsIOException{ //通过时间测试效率 longstartTime=System.currentTimeMillis(); Filefile=newFile("C:\\Users\\joke\\Desktop\\Demo1.java"); FileInputStreamfileInputStream=newFileInputStream(file); //缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高 byte[]bt=newbyte[1024]; intcount=0; //read返回-1时,证明已经遍历完 while((count=fileInputStream.read(bt))!=-1){ //字符串型显示(从bt中的第0个字节开始遍历count个长度) System.out.println(newString(bt,0,count)); } fileInputStream.close(); longendTime=System.currentTimeMillis(); System.out.println("读取文件所花时间:"+(endTime-startTime)); }
陌陌说:
在以上,对比第二个和第四个方法,会发现方法四的效率是比较高的,所以推荐使用的四个方法
在这里我们是直接抛出异常,除了抛出之外我们还可以使用
try{}cater{}finally{}
的方式来处理异常
以上所述是小编给大家介绍的javaIO流之输入流InputString()的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!