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 ...
随机推荐
- HTML5 + CSS3 + JavaScript
http://www.programmer.com.cn/14761/#more-14761 文/李晶 随着互联网产业的爆炸式增长,与之伴生的Web前端技术也在历经洗礼和蜕变.尤其是近几年随着移动终端 ...
- MFC关于VS内存释放的定位
全部在App中完成 1.在 App.h 头文件声明 #ifdef _DEBUGprotected: CMemoryState m_msOld, m_msNew, m_msDiff;#endi ...
- java基本类型作为成员变量时的初始值
package primitivetypedefaultvalue; public class ListDefaultValue { public static void main(String[] ...
- linux awk浅析(转)
Awk 是一种非常好的语言,同时有一个非常奇怪的名称.在本系列(共三篇文章)的第一篇文章中,Daniel Robbins 将使您迅速掌握 awk 编程技巧.随着本系列的进展,将讨论更高级的主题,最后将 ...
- 设计模式 ( 十八 ):State状态模式 -- 行为型
1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理.最直接的解决方案是将这些所有可能发生的情况全都考虑到.然后使用if... ellse语句来做状态判断来进行不同情况的处理.但是对 ...
- 2015第29周二AOP
1.问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案:在每个需要的类函数中重复写上面处理代. 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设 ...
- Misha and Changing Handles
Description Misha hacked the Codeforces site. Then he decided to let all the users change their hand ...
- css3 渐变linear-gradient
background: -moz-linear-gradient(top, #FC641C, #FC761C); 参数:其共有三个参数,第一个参数表示线性渐变的方向,top 是从上到下.left 是从 ...
- JS(五)
感觉JS里面还是有很多小技巧的,知道套路了,其实实现起来其实也还没有想象中的那么复杂.不过我觉得还是要把所学的知识融会贯通吧,不能学了JS就忘了前面的知识,结合起来才会威力无穷. 1.跑马灯:弹弹弹 ...
- Linux下Redis的安装、配置操作说明
Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部分场合可以对关系数据库起到很好的补充作用.它提供了Pytho ...