字节流-输入字节流-FileInputStream
来源:互联网 发布:开淘宝店页面图片 编辑:程序博客网 时间:2024/04/29 12:06
FileInputStream 文件字节输入流,常用于从系统上读取数据,以字节的方式。它继承了 InputStream 类,InputStream是字节输入流的抽象类。
FileInputStream 常用的构造方法,它们都抛出一个FileNotFoundException的异常,当文件路径不正确的时候会触发。这个在编码的时候要处理掉,使用自定义异常来处理,不能直接往外抛。
FileInputStream(String name):该方法传入一个字符串类型的参数,要读取文件的绝对路径。
public FileInputStream(String name) throws FileNotFoundException { this(name != null ? new File(name) : null); }
示例:
InputStream is = new FileInputStream("e:/app/f.txt");
FileInputStream(File file):该方法传入一个file类型的数据。
public FileInputStream(File file) throws FileNotFoundException { String name = (file != null ? file.getPath() : null); SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkRead(name); } if (name == null) { throw new NullPointerException(); } fd = new FileDescriptor(); fd.incrementAndGetUseCount(); open(name); }
示例:
File file = new File("e:/app/f.txt"); InputStream ins = new FileInputStream(file);
FileInputStream 的常用方法,read方法,它有几个重载,int readBytes(byte b[], int off, int len) 是基础,它是native 方法。read(byte b[]) 方法调用了 int readBytes(byte b[], int off, int len) 方法。read方法返回一个-1表示还没有读取结束,常将它的结果3和人-1比较,不等于-1继续读取。
操作完成后要释放资源,掉用 close() 方法,关闭流。
看下面一个例子,该示例使用FileInputStream 读取文件中的内容,并在控制台上打印出来。
该示例中使用了 FileInputStream 的 read(byte [] b) 方法,为了避免浪费内存,该数据的大小根据要读取文件的大小来指定。
package cmc.com.jer.cmc;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class Demo3 { public static void main(String[] args) { // 声明 file File file = new File("e:/app/f.txt"); // 这个数组的大小根据文件来指定,避免内存浪费 ,该数组用来存储读取的字节 byte [] b = new byte[(int)file.length()]; // 字节输入流 FileInputStream InputStream in =null; try { in = new FileInputStream(file); try { //把读取到的字节新放入 数组 b中 in.read(b); System.out.println(new String(b)); } catch (IOException e) { e.printStackTrace(); } finally { if(in !=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } }}
0 0
- 字节输入流(FileInputStream)
- 字节流-输入字节流-FileInputStream
- FileInputStream(输入流)字节流
- 字节输入流FileInputStream类
- 文件字节输入流--FileInputStream
- java的输入字节流 FileInputStream类
- IO之字节输入流FileOutputStream、字节输出流FileInputStream
- 输入字节流(FileInputStream)和输出字节流(FileInputStream)的用法
- 字节流之文件输入流FileInputStream(上)
- 字节流之文件输入流FileInputStream(下)
- [Java] FileInputStream/FileOutputStream 字节流
- Java - 字节流 - FileInputStream - FileOutputStream
- 字节流文件的复制-FileInputStream-FileOutputStream
- Java 字节流InputStream、OutputStream、FileInputStream、FileOutputStream
- IO<字节流>FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream
- 字节流的简单读写 FileOutputStream,FileInputStream
- Java 字节流InputStream、OutputStream、FileInputStream、FileOutputStream
- Java 字节流InputStream、OutputStream、FileInputStream、FileOutputStream
- 十大绝招快速提高微信公众号图文消息阅读量
- SQL学习与练习
- SAP ABAP程序优化(我也来谈谈程序优化)
- cocos2d-x-3.13 cpp-tests项目 eclipse 编译
- 【Fragment】fragment之间的切换,add、show、hide
- 字节流-输入字节流-FileInputStream
- tesseract 训练 + 识别
- 疑难杂症之----------setContentView()报java.lang.OutOfMemoryError错误
- [BZOJ3362][Usaco2004 Feb]Navigation Nightmare 导航噩梦(加权并查集)
- bug集合
- DrawerLayout
- Nginx + Tomcat 反向代理 负载均衡 集群 部署指南
- iOS中的谓词(NSPredicate)使用
- spring配置datasource三种方式