FileInputStream
InputStream 基类,抽象类
FileInputStream 读取文件的字节流
BufferedInputStream 缓冲输入字符流
- package file;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- public class test {
- public static void main(String[] args) throws IOException {
- // readTest1();
- // readTest2();
- // readTest3();
- readTest4(); //效率最高
- }
- //方式4:使用缓冲数据配合循环读取
- public static void readTest4() throws IOException {
- long startTime = System.currentTimeMillis();
- File file = new File("F:\\a.txt");
- FileInputStream fileInputStream = new FileInputStream(file);
- //建立缓冲字节数组,读取文件的数据。
- byte[] buf = new byte[1024];
- int content = 0;
- while((content = fileInputStream.read(buf) ) != -1){
- System.out.println("内容是:"+ content);
- }
- fileInputStream.close();
- long endTime = System.currentTimeMillis();
- System.out.println("读取时间是:"+ (endTime-startTime));
- }
- //方式3、使用 缓冲数据 读取 无法读取完整一个文件的数据:
- public static void readTest3() throws IOException {
- File file = new File("F:\\a.txt");
- FileInputStream fileInputStream = new FileInputStream(file);
- //建立缓冲字节数组,读取文件的数据。
- byte[] buf = new byte[1024];
- int length = fileInputStream.read(buf); //数据已经存储到了字节数组中了,read返回值是本次读取了几个字节数据到字节数组中
- System.out.println("length:" + length);
- //使用字节数组构建字符串
- String content = new String(buf,0,length); //读取长度个,一定要加length,因为不加length,会覆盖,比如,aaaabbb,读取之后会变成aaaabbba。
- System.out.println("内容是:"+content);
- fileInputStream.close();
- }
- //方式2、使用循环读取文件的数据
- public static void readTest2() throws IOException {
- File file = new File("F:\\a.txt");
- FileInputStream fileInputStream = new FileInputStream(file);
- //读取文件的数据
- int content = 0;
- while((content = fileInputStream.read())!=-1) {
- System.out.print((char)content);
- }
- fileInputStream.close();
- }
- //方式1、
- public static void readTest1() throws IOException {
- //1.找到目标文件
- File file = new File("F:\\a.txt");
- //建立数据的输入通道
- FileInputStream fileInputStream = new FileInputStream(file);
- //读取文件中的数据
- int content = fileInputStream.read(); //每次读取一个字节
- System.out.println("读取的内容.."+ content);
- //关闭资源
- fileInputStream.close();
- }
- }
FileInputStream的更多相关文章
- 复制文件的问题:使用FileInputStream和FileOutputStream实现文件复制
public class Test{ public static void main(String [] args) { Test t=new Test(); t.upload(); } public ...
- FileOutputStream和FileInputStream的用法
public static void show() { File f=new File("d:"+File.separator+"1.txt"); FileOu ...
- 用FileInputStream实现文本复制
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 用f ...
- FileInputStream、FileReader、FileInputStream、FileWriter使用小结
本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 InputStream包含如下三个方法: int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转化为i ...
- java中FileInputStream和FileOutputStream对图片操作的例子
package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...
- FileInputStream和BufferedInputStream的区别
FileInputStream 属于数据源 BufferedInputStream 属于FileInputStream的一个装饰 BufferedInputStream 有个内部缓冲区当read时会先 ...
- Java字节流:FileInputStream FileOutputStream
----------------------------------------------------------------------------------- FileInputStream ...
- 字节流与字符流(FileInputStream类和FileOutputStream类)
FileInputStream类和FileOutputStream类中,第一个类的源端和第二个类的目的端都是磁盘文件,它们的构造方法允许通过文件的路径名来构造相应的流.例如: FileInputSte ...
- 文件读写方法1.FileInputStream和FileOutputStream
package fileTest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...
- Java中FileOutputStream和FileInputStream使用例子
package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...
随机推荐
- pm2 安装使用
pm2 是全新开发的进程守护服务, 同时集成了负载均衡功能. 以及开机启动, 自动重启有问题进程. 还可以查看各服务进程状态. 使用方法参照:https://github.com/Unitech/pm ...
- 可用版本的host
http://blog.csdn.net/ljphhj/article/details/11939591 http://my.oschina.net/lvkun0223/blog/282356 两者的 ...
- ~/.bashrc的常用alias设置
centos6.5系统中,alias定义在/etc/bashrc,分别写在/etc/profile.d/*.sh中,可以在此目录添加my.sh, alias attrib='chmod'alias c ...
- hdu-1598
思路: 首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧.然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度 ...
- HDU-1176(基础方程DP)
Problem Description 都 说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉, 就掉落在他 ...
- 原生js实现轮播
1,效果图 https://ga20.github.io/slider/ 2,原理图 分三步: 将视口元素设置overflow:hidden 将其图片子元素设置float:left 让橙色的框(包裹) ...
- 【iOS基础】iOS 线程相关技术
零.线程的注意点(掌握)1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程, ...
- Java 之文件目录操作
1.判断文件是否存在 File file = new File("d:\\study\\temp\\test.java"); boolean bl = file.exists(); ...
- linux服务器内存占用太高-释放内存
修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...
- jquery向下取整
var currentMoney =Math.round((_memberCurrentPoints/_pointVsMoney)*Math.pow(10,2))/Math.pow(10,2) * 2 ...