Java I/O 教程(四) FileInputStream 类
Java FileInputStream class 从一个文件读取字节数据。
用于从图像,音频,视频等文件中读取字节类型数据。
Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system
FileInputStream(String name)
Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.
int read() 从输入流读取字节
int read(byte[] b) 从输入流读取b.length长度的字节
int read(byte[] b, int off, int len) 从输入流每次读取b.length长度的字节
void close() 关闭文件输入流
用于从图像,音频,视频等文件中读取字节类型数据。
类定义
public class FileInputStream extends InputStream
常用构造函数
FileInputStream(File file)Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system
FileInputStream(String name)
Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.
常用方法
int available() 返回输入流中可读取的字节大小int read() 从输入流读取字节
int read(byte[] b) 从输入流读取b.length长度的字节
int read(byte[] b, int off, int len) 从输入流每次读取b.length长度的字节
void close() 关闭文件输入流
例子1
package com.dylan.io;
import java.io.FileInputStream;
/**
* @author xusucheng
* @create 2017-12-31
**/
public class FileInputStreamReadAllChars {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("d:\\testout.txt");
int i=0;
while ((i=fin.read())!=-1){
System.out.print((char) i);
}
fin.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
测试效果截图
下一章:
Java I/O 教程(五) BufferedOutputStream 类