Java IO (1) - InputStream

前言

JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包。Java 流在处理上分为字符流和字节流。字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组。

Java 内用 Unicode 编码存储字符,字符流处理类负责将外部的其他编码的字符流和 java 内 Unicode 字符流之间的转换。而类 InputStreamReader 和 OutputStreamWriter 处理字符流和字节流的转换。字符流(一次可以处理一个缓冲区)一次操作比字节流(一次一个字节)效率高。

0. 目录

1. InputStream



InputStream是一个抽象类,是所有InputStream的基类。

1.1. 基本方法

int	available()
Returns an estimate of the number of bytes that can be read (or skipped over)
from this input stream without blocking by the next invocation of a method
for this input stream. void close()
Closes this input stream and releases any system resources associated with the stream. void mark(int readlimit)
Marks the current position in this input stream. boolean markSupported()
Tests if this input stream supports the mark and reset methods. abstract int read()
Reads the next byte of data from the input stream. int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the
buffer array b. int read(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes. void reset()
Repositions this stream to the position at the time the mark method was last
called on this input stream. long skip(long n)
Skips over and discards n bytes of data from this input stream.

最主要的就是这几个read方法。

2. FileInputStream

FileInputStream从文件中读取字节,比较适合读取二进制数据,如果要读取字符文件,最好用FileReader。

2.1. 构造函数

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(FileDescriptor fdObj)
Creates a FileInputStream by using the file descriptor fdObj,
which represents an existing connection to an actual 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.

有三个构造函数,其参数分别是文件、文件描述符、字符串

其主要用法如下:

FileInputStream fis = new FileInputStream("d:/123.txt");
FileInputStream fis1 = new FileInputStream(new File("d:/123.txt"));

文件描述符那个不常用。

通过看源码,发现字符串那个会默认构造成文件

  public FileInputStream(String paramString)
throws FileNotFoundException
{
this(paramString != null ? new File(paramString) : null);
} public FileInputStream(File paramFile)
throws FileNotFoundException
{
String str = paramFile != null ? paramFile.getPath() : null;
SecurityManager localSecurityManager = System.getSecurityManager();
if (localSecurityManager != null) {
localSecurityManager.checkRead(str);
}
if (str == null) {
throw new NullPointerException();
}
if (paramFile.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
this.fd = new FileDescriptor();
this.fd.incrementAndGetUseCount();
this.path = str;
open(str);
}

注意,这两个构造函数都会抛出FileNotFoundException,但如果传的字符串是null,也会抛出空指针异常。

2.2. 主要方法

int	available()
Returns an estimate of the number of remaining bytes that can be read
(or skipped over) from this input stream without blocking by the next
invocation of a method for this input stream. void close()
Closes this file input stream and releases any system resources
associated with the stream. protected void finalize()
Ensures that the close method of this file input stream is called when
there are no more references to it. FileChannel getChannel()
Returns the unique FileChannel object associated with this file input stream. FileDescriptor getFD()
Returns the FileDescriptor object that represents the connection to the
actual file in the file system being used by this FileInputStream. int read()
Reads a byte of data from this input stream. int read(byte[] b)
Reads up to b.length bytes of data from this input stream into an array of bytes. int read(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes. long skip(long n)
Skips over and discards n bytes of data from the input stream.

2.3. 代码示例

	public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("d:/123.txt");
byte[] b = new byte[1000 * 20];
fis.read(b);
System.out.println(new String(b));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

通常情况下,很少有人直接通过FileInputStream读取文本文件,会通过BufferedReader封装一下。

另外,FileInputStream也会遇到乱码问题,用InputStreamReader解决。

Java IO (1) - InputStream的更多相关文章

  1. Java IO 之 InputStream源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  2. JAVA IO流 InputStream流 Read方法

    read()首先我们来看这个没有参数的read方法,从(来源)输入流中(读取的内容)读取数据的下一个字节到(去处)java程序内部中,返回值为0到255的int类型的值,返回值为字符的ACSII值(如 ...

  3. Java IO 之 OutputStream源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  4. [Java] Java IO 概况

    Java IO 是 Java 的一套 API, 用于读入和写出数据(输入和输出).Java IO API 位于 java.io package.实际上 java.io package 没有解决所有的输 ...

  5. 系统学习 Java IO (十六)----这么多类,应该用哪个?

    目录:系统学习 Java IO---- 目录,概览 Java IO目的和功能 Java IO 包含 InputStream,OutputStream,Reader 和 Writer 类的许多子类. 原 ...

  6. struts2文件下载 出现Can not find a java.io.InputStream with the name的错误

    成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontN ...

  7. java IO文件读写例子(OutputStream,InputStream,Writer,Reader)

    一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...

  8. struts2文件下载出现Can not find a java.io.InputStream with the name的错误

    今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...

  9. 【java】io流之字节输入流:java.io.InputStream类及子类java.io.FileInputStream

    package 文件操作; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impor ...

随机推荐

  1. CTO俱乐部下午茶:技术团队管理中的那些事儿

    摘要:"CTO下午茶"是一种有效的集体对话的模式,参加活动的成员在真诚互动和共同学习的宗旨下齐聚一堂,在喝茶聊天氛围下交流工作心得.本期"CTO下午茶"的主题是 ...

  2. 如何使用UIAutomation进行iOS 自动化测试(Part I)

    转自:http://www.cnblogs.com/vowei/archive/2012/08/10/2631949.html 写在前面 研究iOS的自动化测试也有些日子了,刚开始的时候,一直苦于找不 ...

  3. [转载]Python模块学习 ---- subprocess 创建子进程

    [转自]http://blog.sciencenet.cn/blog-600900-499638.html 最近,我们老大要我写一个守护者程序,对服务器进程进行守护.如果服务器不幸挂掉了,守护者能即时 ...

  4. ZOJ 1610 Count the Colors (线段树 成段更新)

    题目链接 题意:成段染色,初始为0,每次改变一个区间的颜色,求最后每种颜色分别有多少段.颜色按照从 小到大输出. 分析:改变了代码的风格,因为看了学长的博客.直接用数组,可以只是记录节点的编号,因为节 ...

  5. HDU 1280 前m大的数【哈希入门】

    题意:中文的题目= =将各种组合可能得到的和作为下标,然后因为不同组合得到的和可能是一样的, 所以再用一个数组num[]数组,就可以将相同的和都记录下来 #include<iostream> ...

  6. Request.Querystring中文乱码问题解决

    现象:近期项目中用到查询字符串传值,如果传递的是英文一切正常,但是传递中文时,使用request.querystring[]得到的是乱码. 原因:不知道为什么,可能是编码不一致问题 解决方法1:修改w ...

  7. 通过CSS禁止Chrome自动为输入框添加橘黄色边框,修改/禁止 chrome input边框颜色,

    1   /*Chrome浏览器 点击input 黄色边框 禁用*/ .NoOutLine:focus{outline: none} <asp:TextBox ID="txtTeleph ...

  8. Java Web编程的主要组件技术——Struts的高级功能

    参考书籍:<J2EE开源编程精要15讲> Struts对国际化的支持 "国际化"(I18N)指一个应用程序在运行时能根据客户端请求所来的国家/地区.语言的不同显示不同的 ...

  9. MongoDB入门分享-笔记整理精选

    最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步> 首先到官网下载,安装MongoDB.(注意MongoDB还有一个可视化管理工具叫: Mong ...

  10. windows安装TortoiseGit详细使用教程【基础篇】

    标签:tortoisegit 环境:win8.1 64bit 安装准备: 首先你得安装windows下的git msysgit1.9.5 安装版本控制器客户端tortoisegit  tortoise ...