当前位置: 移动技术网 > IT编程>开发语言>Java > java IO流 之 输入流 InputString()的使用

java IO流 之 输入流 InputString()的使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文主要给大家介绍java的inputstream 流的使用。

(1)fileinputstream: 子类,读取数据的通道

  使用步骤:

    1.获取目标文件:new file()

    2.建立通道:new fileinputstring()

    3.读取数据:read()

    4.释放资源:close()

//一些默认要导入的包
import java.io.file;
import java.io.fileinputstream;
import java.io.ioexception;
public static void main(string[] args) throws ioexception {
// todo auto-generated method stub
//分别调用方法查看效果
test1();
system.out.println("-------------------------------------------");
test2();
system.out.println("-------------------------------------------");
test3();
system.out.println("-------------------------------------------");
test4();
}

(2)读取数据的三种方式

    1.直接读取 (一次只能一个字节)

int date = fileinputstream.read();
      char date3 = (char)fileinputstream.read();
//方式一 直接打印
public static void test1() throws ioexception{
//(1)获取目标文件路径
file file = new file("c:\\users\\joke\\desktop\\demo1.java");
//(2)根据目标文件路径 建立通道: new fileinputstream(file)
fileinputstream fileinputstream = new fileinputstream(file);
//(3)读取数据 :read();
int date = fileinputstream.read();//这里是int类型
int date2 = fileinputstream.read();//
char date3 = (char)fileinputstream.read(); //以char类型显示
system.out.println(date+"\\"+date2+"\\"+date3);
//(4)释放资源
fileinputstream.close();
}

 2.单独使用for循环(效率低)     

for(int i = 0; i < file.length();i++){
        system.out.print((char)fileinputstream.read());
      }
//方式二 循环遍历
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循环
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[ ] 缓冲区(只能读取指定的字节数不能读取一个完整的文件)   

byte[] bt = new byte[1024];
      int count = fileinputstream.read(bt);
      system.out.println(new string (bt,0,count));
//方式三 创建缓冲区(只能读取制定的大小,不能读取一个完整的文件)
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];
//read() 读取字节
int count = fileinputstream.read(bt);
system.out.println(count); //显示读取到的字节数
system.out.println(new string (bt,0,count));//将字节转为字符串显示
fileinputstream.close();
}

4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高  

byte[] bt = new byte[1024];
      int count = 0;
      while((count = fileinputstream.read(bt)) != -1){
        system.out.println(new string (bt,0,count));
      }
//方式四 循环与缓冲区结合(效率高)
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);
//缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
byte[] bt = new byte[1024];
int count = 0;
//read返回 -1 时,证明已经遍历完
while((count = fileinputstream.read(bt)) != -1){
//字符串型显示(从bt中的第0个字节开始遍历count个长度)
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()的使用,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网