c语言读取文件到inputstream,跟我学IO(FileInputStream类)

FileInputStream 从文件系统中的某个文件中获得输入字节。FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。

构造FileInputStream输入流的方式如下:

1、通过文件对象File来构造。如:FileInputStream(File file)// 构建一个file

File file = new File("document/stream_test.txt");

// 根据file对象创建文件输入流

FileInputStream input = new FileInputStream(file);

2、直接使用文件在系统中的URI进行创建// 根据URI创建输入流

FileInputStream input = new FileInputStream("document/stream_test.txt");

3、通过文件描述对象来创建输入流FileDescriptor fd = new FileDescriptor();

FileInputStream input = new FileInputStream(input);

FileInputStream.java源码分析:public class FileInputStream extends InputStream {

// 文件描述符对象

private FileDescriptor fd;

// 文件通道

private FileChannel channel = null;

public FileInputStream(String name) throws FileNotFoundException {

// 内部也是用的File对象来获取输入流

this(name != null ? new File(name) : null);

}

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();

open(name);

}

public FileInputStream(FileDescriptor fdObj) {

SecurityManager security = System.getSecurityManager();

if (fdObj == null) {

throw new NullPointerException();

}

if (security != null) {

security.checkRead(fdObj);

}

fd = fdObj;

}

public int read(byte b[]) throws IOException {

return readBytes(b, 0, b.length);

}

public int read(byte b[], int off, int len) throws IOException {

return readBytes(b, off, len);

}

// 关闭文件流

public void close() throws IOException {

if (channel != null)

channel.close();

close0();

}

// 获取文件描述对象

public final FileDescriptor getFD() throws IOException {

if (fd != null)

return fd;

throw new IOException();

}

// 获取文件通道,这个通道用于建立文件NIO操作

public FileChannel getChannel() {

synchronized (this) {

if (channel == null)

channel = FileChannelImpl.open(fd, true, false, this);

return channel;

}

}

// 初始化

static {

initIDs();

}

protected void finalize() throws IOException {

if (fd != null) {

if (fd != fd.in) {

close();

}

}

}

// 本地方法(即通过C/C++来实现)

private native void open(String name) throws FileNotFoundException;

public native int read() throws IOException;

private native int readBytes(byte b[], int off, int len) throws IOException;

public native long skip(long n) throws IOException;

public native int available() throws IOException;

private static native void initIDs();

private native void close0() throws IOException;

}

从上面的源码可以看出,读取文件使用的是本地方法readBytes。这就说明java底层并不是用java代码直接操作文件,而是通过本地代码,如:C语言等进行文件操作。

实例:根据文件的URI获取文件输入流,然后输出文件内容到控制台。FileInputStream input = null;

try {

// 这个路径是根据当前路径的相对路劲

File file = new File("document/stream_test.txt");

input = new FileInputStream(file);

int read = -1;

while( (read = input.read()) != -1 ){

System.out.print( (char)read );

}

} catch(Exception e) {

e.printStackTrace();

} finally {

if ( null != input ) {

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

input = null;

}

}

}

实例:根据文件对象创建一个文件输入流,然后在控制台输出文件内容。FileInputStream input = null;

try {

input = new FileInputStream("document/stream_test.txt");

int read = -1;

while( (read = input.read()) != -1 ){

System.out.print( (char)read );

}

} catch(Exception e) {

e.printStackTrace();

} finally {

if ( null != input ) {

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

input = null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值