在Java程序中,通常会使用IO的输入输出,这里的输入输出是相对你的程序而言的。比如你写一个程序要读取一个文本文件,这里有两个对象,你的程序,还有就是文本文件。你的程序需要数据,那么用的应该是输入流(InputStream).
public class ReadText {
public static void main(String[] args){
try {
FileInputStream input = new FileInputStream("test.txt");
/*对于我接下来的程序,我要读取数据,但我完全不需要考虑数据从何
* 而来,我只要知道有个input对象,它给我了读取数据的方法、途径*/
int data;
while((data = input.read())!= -1){
System.out.print((char)data);
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
你可以把自己想象成站在海边,然后波涛汹涌的海水(数据)正像你涌来。下面是InputStream的源码:
public abstract class InputStream implements Closeable {
private static final int SKIP_BUFFER_SIZE = 2048;
private static byte[] skipBuffer;
/*
* 从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经
* 到达流末尾而没有可用的字节,则返回值-1。在输入数据可用、检测到流末尾或者抛出异
* 常前,此方法一直阻塞。 子类必须提供此方法的一个实现。
*/
public abstract int read() throws IOException;
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte) c;
int i = 1;
try {
for (; i < len; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte) c;
}
} catch (IOException ee) {
}
return i;
}
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
public long skip(long n) throws IOException {
long remaining = n;
int nr;
if (skipBuffer == null)
skipBuffer = new byte[SKIP_BUFFER_SIZE];
byte[] localSkipBuffer = skipBuffer;
if (n <= 0) {
return 0;
}
while (remaining > 0) {
nr = read(localSkipBuffer, 0,
(int) Math.min(SKIP_BUFFER_SIZE, remaining));
if (nr < 0) {
break;
}
remaining -= nr;
}
return n - remaining;
}
public int available() throws IOException {
return 0;
}
public void close() throws IOException {
}
public synchronized void mark(int readlimit) {
}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
}
分享到:
相关推荐
深入剖析tomcat第三章SocketInputStream源码 * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/StringManager.java,v 1.2 2001/07/22 20:25:14 pier Exp $ * $Revision: 1.2...
这篇文章主要给大家介绍了关于通过JDK源码学习InputStream的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
NULL 博文链接:https://cuisuqiang.iteye.com/blog/1434416
Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款... InputStream is=ftpClient.list(); //得到服务器目录与文件列表输入流 StringBuffer info=new StringBuffer(); /...
Java 源码包 Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会... InputStream is=ftpClient.list(); //得到服务器目录与文件列表输入流 StringBuffer info=new ...
NULL 博文链接:https://shihuan830619.iteye.com/blog/2094068
借助scrcpy.jar的server开发Swing版本的GUI投屏软件源码
InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...
NULL 博文链接:https://superherosk123.iteye.com/blog/611933
String[] getStringArray InputStream String String getString InputStream String String getColor InputStream String String getDimens InputStream String 使用方法类似 getResources getStringArray
笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!此时此...
Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来模拟银行...
Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来模拟银行...
Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来模拟银行...
协议相当于相互通信的程序间达成的一种约定,它规定了分组报文...另外,每个Socket实例会关联一个InputStream和OutputStream对象,我们通过将字节写入套接字的OutputStream来发送数据,并通过从InputStream来接收数据。
NULL 博文链接:https://j2ee2009.iteye.com/blog/707532
Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来模拟银行ATM机...
Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来模拟银行ATM机...
NULL 博文链接:https://nethub2.iteye.com/blog/2023937
Struts Updownload 源码 给出其中一个Action 包中含有整个框架的源码 package com.icss.action; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io....