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 类
分类:
Java-Core
标签:
FileInputStream
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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语句:使用策略模式优化代码结构