Java FileInputStream
1. Java FileInputStream类
Java FileInputStream类是java.io包的一部分。
FileInputStream从文件系统中的文件获取输入字节。
FileInputStream用于读取原始字节流,例如图像数据。
FileInputStream是InputStream类的子类。
2. FileInputStream类层次结构
3. FileInputStream构造函数
FileInputStream提供了许多从文件读取字节的方法,我们可以使用以下构造函数创建FileInputStream的实例。
FileInputStream(File file):通过使用指定的文件对象打开与实际文件的连接来创建FileInputStream对象。
FileInputStream(FileDescriptor fdObj):使用指定的文件描述符fdObj创建FileInputStream对象,该对象表示与文件系统中实际文件的现有连接。
FileInputStream(String name):通过打开到一个实际文件的连接来创建一个FileInputStream对象,该文件以指定的名称命名,该名称代表文件的路径。
4. FileInputStream方法
让我们看一下FileInputStream类的以下方法。
available():此方法返回可以从输入流读取的剩余字节数的估计值,而不会被该输入流的方法的下一次调用阻塞。close():此方法关闭此文件输入流并释放与该流关联的所有系统资源。
如果此流具有关联的通道,则通道也将关闭。finalize():该方法确保在没有更多引用时调用此文件输入流的close()方法。getChannel():此方法返回与此文件输入流关联的唯一FileChannel对象。
返回的通道的初始位置将等于从文件读取的字节数,并且从此流中读取字节将增加通道的位置。getFD():此方法返回FileDescriptor对象,该对象表示与此FileInputStream使用的文件系统中实际文件的连接。
read():此方法从此输入流中读取一个字节的数据,并返回下一个数据字节;如果到达文件末尾,则返回-1。
read(byte [] b):此方法从此输入流中读取直到指定数组(b.length)长度的数据字节到指定的字节数组中,并返回读入缓冲区的字节总数或者-如果到达文件末尾,则为1。read(byte [] b,int off,int len):此方法从该输入流中读取直到指定len的数据字节到指定的字节数组。
如果指定的len不为零,则该方法将阻塞,直到有一些输入可用;否则,该方法将停止。
否则,不读取任何字节,并返回0。
它返回读入缓冲区的字节数;如果到达文件末尾,则返回-1。Skip(long n):此方法跳过并丢弃输入流中指定的n个字节的数据,并返回实际跳过的字节数。
如果指定的n为负数,则抛出IOException。
5. Java FileInputStream示例
让我们看一下FileInputStream类的一些示例程序。
5.1)使用FileInputStream读取文件
package com.theitroad.examples;
import java.io.File;
import java.io.FileInputStream;
/**
* Java Read file using FileInputStream
*
* @author hyman
*
*/
public class ReadFileUsingFileInputStream {
public static void main(String[] args) {
File file = null;
FileInputStream fileInputStream = null;
try {
file = new File("D:/data/file.txt");
fileInputStream = new FileInputStream(file);
System.out.println("Available bytes in file: "+fileInputStream.available());
int line;
while ((line=fileInputStream.read()) != -1) {
System.out.print((char)line);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
输出:
Available bytes in file: 48 Hello world. This is a FileInputStream Program.
注意," file.txt"是上述程序中使用的文件,其内容已打印出来。
您可以创建任何其他文件并通过进行所需的更改来运行该程序。
Java 7提供带资源的try,这是一个声明一个或者多个资源的try语句。
资源是程序完成后必须关闭的对象。
try with resource语句可确保在语句末尾关闭每个资源。
让我们看一下下面的示例程序,它使用try资源。
package com.theitroad.examples;
import java.io.File;
import java.io.FileInputStream;
/**
* Java Read file using FileInputStream with Try with Resource
*
* @author hyman
*
*/
public class FileInputStreamTryWithResource {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileInputStream fileInputStream = new FileInputStream(file)){
System.out.println("Available bytes in file: "+fileInputStream.available());
int line;
while ((line=fileInputStream.read()) != -1) {
System.out.print((char)line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
它将产生与早期程序相同的结果。
还要检查Java读取文本文件,以获取有关如何在Java中读取文本文件的更多信息。
5.2)finalize()
FileInputStream的finalize()方法可确保在没有更多引用时调用此文件输入流的close()方法。
如果在调用finalize()之后尝试访问FileInputStream,我们将得到一个异常。
package com.theitroad.examples;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* Java FileInputStream Finalize method example
*
* @author hyman
*
*/
public class FileInputStreamFinalizeExample extends FileInputStream{
public FileInputStreamFinalizeExample(String name) throws FileNotFoundException {
super(name);
}
public static void main(String[] args) {
FileInputStreamFinalizeExample file = null;
try {
file = new FileInputStreamFinalizeExample("D:/data/file.txt");
System.out.println("Available bytes in the file: "+file.available());
int line;
while ((line=file.read()) != -1) {
System.out.print((char)line);
}
//calling finalize method
file.finalize();
System.out.println("\n");
System.out.println("------After finalize called--------");
System.out.println(file.available());
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (file != null) {
file.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
上面程序的输出如下:
Available bytes in the file: 48 Hello world. This is a FileInputStream Program. ------After finalize called------- java.io.IOException: Stream Closed at java.io.FileInputStream.available(Native Method) at com.theitroad.examples.FileInputStreamFinalizeExample.main(FileInputStreamFinalizeExample.java:31)
5.3)getFD()
FileInputStream getFD()返回FileDescriptor对象,该对象表示与此FileInputStream使用的文件系统中实际文件的连接。
让我们看下面的示例程序。
package com.theitroad.examples;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
/**
* Java FileInputStream FileDescriptor Example
*
* @author hyman
*
*/
public class FileInputStreamFileDescripterExample {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileInputStream fileInputStream = new FileInputStream(file)){
FileDescriptor fileDescriptor = fileInputStream.getFD();
boolean valid = fileDescriptor.valid();
System.out.println("Valid file: "+valid);
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面程序的输出是:
Valid file: true
5.4)read(byte [] b,int off,int len)
此方法从此输入流中读取指定长度的数据字节到指定的字节数组中。
如果指定的长度不为零,则该方法将阻塞,直到有一些输入可用;否则,该方法将停止。
否则,不读取任何字节,并返回0。
它返回读入缓冲区的字节数;如果到达文件末尾,则返回-1。
package com.theitroad.examples;
import java.io.FileInputStream;
/**
* Java Read file using read(byte[] b, int off, int len) method
*
* @author hyman
*
*/
public class FileInputStreamRead {
public static void main(String[] args) {
try (FileInputStream fileInputStream = new FileInputStream("D:/data/file.txt")) {
byte[] b = new byte[10];
int i = fileInputStream.read(b, 0, 8);
System.out.println("Available bytes in file: " + fileInputStream.available());
System.out.println("Number of bytes in read from file: " + i);
for (byte bs : b) {
char c = (char) bs;
if (bs == 0) {
c = '-';
}
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
Available bytes in file: 40 Number of bytes in read from file: 8 Hello wo-
5.5)skip(long n)
FileInputStream skip(long n)方法跳过并从输入流中丢弃指定的n个字节的数据,并返回实际跳过的字节数。
如果指定的n为负数,则抛出IOException。
package com.theitroad.examples;
import java.io.FileInputStream;
/**
* Java Read file with skip(long n) method example
*
* @author hyman
*
*/
public class FileInputStreamSkip {
public static void main(String[] args) {
try (FileInputStream fileInputStream = new FileInputStream("D:/data/file.txt")) {
System.out.println("Available bytes in the file: " + fileInputStream.available());
int line;
//skip 2 bytes
fileInputStream.skip(2);
System.out.println("Available bytes in the file after skip: " + fileInputStream.available());
while ((line=fileInputStream.read()) != -1) {
System.out.print((char)line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面程序的输出是:
Available bytes in the file: 12 Available bytes in the file after skip: 10 llo world.
5.6)getChannel()
此方法返回与此文件输入流关联的唯一FileChannel对象。
返回的通道的初始位置将等于从文件读取的字节数,并且从此流中读取字节将增加通道的位置。
package com.theitroad.examples;
import java.io.FileInputStream;
import java.nio.channels.FileChannel;
/**
* Java Read file with getChannel method example
*
* @author hyman
*
*/
public class FileInputStreamChannel {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileChannel fileChannel = null;
try {
fileInputStream = new FileInputStream("D:/data/file.txt");
System.out.println("Available bytes in file: " + fileInputStream.available());
int line;
while ((line=fileInputStream.read()) != -1) {
fileChannel = fileInputStream.getChannel();
System.out.print("No of bytes read: "+fileChannel.position());
System.out.println("; Char read: "+(char)line);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileChannel != null) {
fileChannel.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
输出:
Available bytes in file: 12 No of bytes read: 1; Char read: H No of bytes read: 2; Char read: e No of bytes read: 3; Char read: l No of bytes read: 4; Char read: l No of bytes read: 5; Char read: o No of bytes read: 6; Char read: No of bytes read: 7; Char read: w No of bytes read: 8; Char read: o No of bytes read: 9; Char read: r No of bytes read: 10; Char read: l No of bytes read: 11; Char read: d No of bytes read: 12; Char read: .

