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的更多相关文章

  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. startActivityForResult案例

    Info:startActivty 与 startActivityForResult区别 (1):startActivity 启动了其他Activity之后不会再回调过来,此时启动者与被启动者在启动后 ...

  2. Eric Pement的单行awk命令收集

    超实用的. awk1line_zh-CN.txt 可惜中文版的网址好像不能访问了.. HANDY ONE-LINE SCRIPTS FOR AWK 30 April 2008 Compiled by ...

  3. Why SignalR does not use WebSockets?

    Why SignalR does not use WebSockets?   As you probably know SignalR supports multiple transports. Th ...

  4. 【转】android使用File Explorer无法访问系统内部文件--不错

    原文网址:http://blog.csdn.net/yangqicong11/article/details/8747042 设备:Samsung GT-P3110 系统:Android 4.1.1 ...

  5. codevs1033 蚯蚓的游戏问题

    题目描述 Description 在一块梯形田地上,一群蚯蚓在做收集食物游戏.蚯蚓们把梯形田地上的食物堆积整理如下: a(1,1)  a(1,2)…a(1,m) a(2,1)  a(2,2)  a(2 ...

  6. [置顶] 如何更改CSDN博客高亮代码皮肤的样式,使博客看起来更有范(推荐)

    由于本人写博客的时候,也没有配置博客的相关属性,因此贴出来的代码块都是CSDN默认的,因此代码背景色都是白色的,如下所示: 但是本人在浏览他人博客的时候,发现有些博客的代码块看起来比较有范,整个代码库 ...

  7. ESB 企业服务总线

    整理的OSChina 第 38 期高手问答 —— ESB 企业服务总线,嘉宾为@肖俊_David . @肖俊_David 恒拓开源架构师,热衷于JAVA开发,有多年的企业级开发经验.曾参和设计和开发基 ...

  8. JS(一)

    循环还是很有意思的: 1) 安全数的作业: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  9. Gulp-livereload:实时刷新编码

    实现功能 监听指定目录下的所有文件,实时动态刷新页面 安装(Install) 功能的实现是借助 gulp-connect 插件完成的;所以,首先通过下面命令完成插件安装: npm install -- ...

  10. 类型兼容原则(C++)

    类型兼容原则是指在需要基类对象的任何地方,都可以使用公有派生类的对象来替代. 通过公有继承,派生类得到了基类中除构造函数.析构函数之外的所有成员.这样,公有派生类实际具备了基类的所有功能,凡是基类能解 ...