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 ...
随机推荐
- MySQL原生HA方案 – Fabric体验之旅
http://www.csdn.net/article/2014-08-20/2821300
- VCC、VDD、VEE、VSS
转载自:http://www.cnblogs.com/asus119/archive/2011/10/11/2206841.html 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circ ...
- 【HDOJ】1494 跑跑卡丁车
DP,将能量映射为0~14,注意当选择这圈加速的时候,这圈就不能再储存能量,同时能量14可能转化为10. #include <cstdio> #include <cstring> ...
- gcc编译时头文件库文件搜索顺序(转)
原文: http://blog.csdn.net/silentfly1987/article/details/6119195
- HDU_2040——判断亲和数
Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为: 1+2+4+5+10+11+20+22+44+55+110=284. ...
- eclipse与myeclipse工程互转
eclipse转myeclipse: 在Myeclipse中新建web工程导入即可. myeclipse转eclipse: 在eclipse中新建web工程,把myeclipse项目中代码手动拷贝到 ...
- Android开发中Handler的经典总结--转载至网络
一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释:当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件 ...
- JS-Date日期内置对象
1.基本用法 <script> var date = new Date(); document.write(date+"<br/>");//获取具体时间 d ...
- Highcharts 基本曲线图
基本曲线图实例 文件名:highcharts_line_basic.htm <html> <head> <meta charset="UTF-8" / ...
- bat文件调用shutdown命令不生效问题原因
背景: 本人使用云桌面办公,但是用于登陆云桌面的终端运行卡顿,每次开机要20min才能登陆云桌面,所以: 1)在BIOS设置了定时开关,让终端提前开机 2)在系统上层,开机启动项增加一个bat文件(s ...