InputStream 基类,抽象类

    FileInputStream  读取文件的字节流

    BufferedInputStream  缓冲输入字符流

  1. package file;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6.  
  7. public class test {
  8. public static void main(String[] args) throws IOException {
  9. // readTest1();
  10. // readTest2();
  11. // readTest3();
  12. readTest4(); //效率最高
  13. }
  14.  
  15. //方式4:使用缓冲数据配合循环读取
  16. public static void readTest4() throws IOException {
  17. long startTime = System.currentTimeMillis();
  18. File file = new File("F:\\a.txt");
  19. FileInputStream fileInputStream = new FileInputStream(file);
  20. //建立缓冲字节数组,读取文件的数据。
  21. byte[] buf = new byte[1024];
  22. int content = 0;
  23. while((content = fileInputStream.read(buf) ) != -1){
  24. System.out.println("内容是:"+ content);
  25. }
  26. fileInputStream.close();
  27. long endTime = System.currentTimeMillis();
  28. System.out.println("读取时间是:"+ (endTime-startTime));
  29. }
  30.  
  31. //方式3、使用 缓冲数据 读取 无法读取完整一个文件的数据:
  32. public static void readTest3() throws IOException {
  33. File file = new File("F:\\a.txt");
  34. FileInputStream fileInputStream = new FileInputStream(file);
  35. //建立缓冲字节数组,读取文件的数据。
  36. byte[] buf = new byte[1024];
  37. int length = fileInputStream.read(buf); //数据已经存储到了字节数组中了,read返回值是本次读取了几个字节数据到字节数组中
  38. System.out.println("length:" + length);
  39. //使用字节数组构建字符串
  40. String content = new String(buf,0,length); //读取长度个,一定要加length,因为不加length,会覆盖,比如,aaaabbb,读取之后会变成aaaabbba。
  41. System.out.println("内容是:"+content);
  42. fileInputStream.close();
  43. }
  44.  
  45. //方式2、使用循环读取文件的数据
  46. public static void readTest2() throws IOException {
  47. File file = new File("F:\\a.txt");
  48. FileInputStream fileInputStream = new FileInputStream(file);
  49. //读取文件的数据
  50. int content = 0;
  51. while((content = fileInputStream.read())!=-1) {
  52. System.out.print((char)content);
  53. }
  54. fileInputStream.close();
  55. }
  56.  
  57. //方式1、
  58. public static void readTest1() throws IOException {
  59. //1.找到目标文件
  60. File file = new File("F:\\a.txt");
  61. //建立数据的输入通道
  62. FileInputStream fileInputStream = new FileInputStream(file);
  63. //读取文件中的数据
  64. int content = fileInputStream.read(); //每次读取一个字节
  65. System.out.println("读取的内容.."+ content);
  66. //关闭资源
  67. fileInputStream.close();
  68. }
  69. }

FileInputStream的更多相关文章

  1. 复制文件的问题:使用FileInputStream和FileOutputStream实现文件复制

    public class Test{ public static void main(String [] args) { Test t=new Test(); t.upload(); } public ...

  2. FileOutputStream和FileInputStream的用法

    public static void show() { File f=new File("d:"+File.separator+"1.txt"); FileOu ...

  3. 用FileInputStream实现文本复制

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 用f ...

  4. FileInputStream、FileReader、FileInputStream、FileWriter使用小结

    本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 InputStream包含如下三个方法: int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转化为i ...

  5. java中FileInputStream和FileOutputStream对图片操作的例子

    package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...

  6. FileInputStream和BufferedInputStream的区别

    FileInputStream 属于数据源 BufferedInputStream 属于FileInputStream的一个装饰 BufferedInputStream 有个内部缓冲区当read时会先 ...

  7. Java字节流:FileInputStream FileOutputStream

    ----------------------------------------------------------------------------------- FileInputStream ...

  8. 字节流与字符流(FileInputStream类和FileOutputStream类)

    FileInputStream类和FileOutputStream类中,第一个类的源端和第二个类的目的端都是磁盘文件,它们的构造方法允许通过文件的路径名来构造相应的流.例如: FileInputSte ...

  9. 文件读写方法1.FileInputStream和FileOutputStream

    package fileTest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  10. Java中FileOutputStream和FileInputStream使用例子

    package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...

随机推荐

  1. pm2 安装使用

    pm2 是全新开发的进程守护服务, 同时集成了负载均衡功能. 以及开机启动, 自动重启有问题进程. 还可以查看各服务进程状态. 使用方法参照:https://github.com/Unitech/pm ...

  2. 可用版本的host

    http://blog.csdn.net/ljphhj/article/details/11939591 http://my.oschina.net/lvkun0223/blog/282356 两者的 ...

  3. ~/.bashrc的常用alias设置

    centos6.5系统中,alias定义在/etc/bashrc,分别写在/etc/profile.d/*.sh中,可以在此目录添加my.sh, alias attrib='chmod'alias c ...

  4. hdu-1598

    思路: 首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧.然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度 ...

  5. HDU-1176(基础方程DP)

    Problem Description 都 说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉, 就掉落在他 ...

  6. 原生js实现轮播

    1,效果图 https://ga20.github.io/slider/ 2,原理图 分三步: 将视口元素设置overflow:hidden 将其图片子元素设置float:left 让橙色的框(包裹) ...

  7. 【iOS基础】iOS 线程相关技术

    零.线程的注意点(掌握)1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程, ...

  8. Java 之文件目录操作

    1.判断文件是否存在 File file = new File("d:\\study\\temp\\test.java"); boolean bl = file.exists(); ...

  9. linux服务器内存占用太高-释放内存

    修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...

  10. jquery向下取整

    var currentMoney =Math.round((_memberCurrentPoints/_pointVsMoney)*Math.pow(10,2))/Math.pow(10,2) * 2 ...