Java I/O 教程(四) FileInputStream 类

Java FileInputStream class 从一个文件读取字节数据。
用于从图像,音频,视频等文件中读取字节类型数据。

类定义


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 类






  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值