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 ...
随机推荐
- Linux C判断日期格式是否合法
Title:Linux C判断日期格式是否合法 --2013-10-11 11:54 #include <string.h> // strlen() , strncpy() #includ ...
- js 比较日期大小
//1获取当前时间 var curTime = new Date(); //2把字符串格式转换为日期类 var startTime = new Date(Date.parse(kc.begintime ...
- IIS Server Farms集群负载
序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...
- window.onscroll
http://www.w3help.org/zh-cn/causes/SD9013 1.各浏览器对 document.document.body.document.documentElement 对象 ...
- uva 10014 Simple calculations
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- C51 库函数
C-51软件包的库包含标准的应用程序,每个函数都在相应的头文件(.h)中有原型声明.如果使用库函数,必须在源程序中用预编译指令定义与该函数相关的头文件(包含了该函数的原型声明).例如:#include ...
- [Qt] CFlip 翻页功能实现
由于需要给table制作翻页功能,所以写了一个翻页的类. 看上去总体效果感觉还是不错的,哈哈. //flip.h #ifndef CFLIP_H #define CFLIP_H #include &l ...
- Python进阶(面向对象编程基础)(二)
1.初始化实例属性 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'ziv·chan' #定义Person类的__init__方法 ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Android NOtification 使用(震动 闪屏 铃声)
一. Notification 简介 在 android 系统中,在应用程序可能会遇到几种情况需要通知用户,有的需要用户回应,有的则不需要,例如: * 当保存文件等事件完成,应该会出现一个小的消息,以 ...