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 类






posted @   一锤子技术员  阅读(5)  评论(0编辑  收藏  举报  
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示