Java中的InputStream和FileInputStream
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
大家是否对InputStream和FileInputStream 有过疑问勒,InputStream不可以读取文件,它是一个Abstract的类,根本不可能实例化,是所有输入流的基类。
而FileInputStream是InputStream的一个实现类,用于读取诸如图像数据之类的原始字节流。
要读取字符流,请考虑使用FileReader 。
下面我举例说明,InputStream的基本用法我也列出来了。
package com.io.Stream;
//InputStream 的基本用法:
//读取一个字节并以证书的形式返回(0~255)如果返回-1已到输入流的结尾。
int read()throws IoException
//读取一系列字节并存储到一个数组buffer,返回实际读取的字节数,如果读取前已到输入流的末尾返回-1 int read(byte[]buffer)throws IoException
//读取length个字节并存储到一个字节数组buffer,从length位置开始返回实际读取的字节数,如果读取前以到输入流的末尾返回-1 int read(byte[] buffer,int offset,int length)throws IoException
//关闭释放内存资源void close()throws IoException
//跳过n个字节不读,返回实际跳过的字节数long skip(long n)throws IoException
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.stream.FileCacheImageInputStream;
public class InputStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream files=null;
int b = 0;
try{
files=new FileInputStream("F:/bb/ii/tt.txt");
}catch(FileNotFoundException e){
System.out.println("系统文件不存在");
System.exit(-1);
}
try{
long num=0;
while((b= files.read())!=-1){
System.out.print((char)b);
num++;
}
files.close();
System.out.println();
System.out.println("读取成功");
System.out.println("共读取了"+num+"个字节");
}catch(IOException e){
System.out.println("此文件读取出错");
System.exit(-1);
}
}
}
下面那段文字是我tt.txt中的内容
结果是:
c.remove(new Name("f1","f2"));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.size());
System.out.println(c);// toString
Collection v=new HashSet();
v.add(new Name("li","lu"));
v.add(new Name("ji","jj"));
???????ò
读取成功
共读取了280个字节
备注:
//上面的?号是因为中文读不出把它改成Filereader就好了它是读取字符When you are old and grey and full of sleep,
And nodding by the fire, take down this book,
And slowly read, and dream of the soft look
Your eyes had once, and of their shadows deep;
How many loved your moments of glad grace,
And loved your beauty with love false or true,
But one man loved the pilgrim soul in you,
And loved the sorrows of your changing face;
And bending down beside the glowing bars,
Murmur, a little sadly, how love fled
And paced upon the mountains overhead
And hid his face amid a crowd of stars.
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don't know that
I love you.
The furthest distance in the world
Is not when I stand in front of you
Yet you can't see my love
But when undoubtedly knowing the love from both Yet cannot be together.
The furthest distance in the world
Is not being apart while being in love
But when I plainly cannot resist the yearning
Yet pretending you have never been in my heart. The furthest distance in the world
Is not struggling against the tides
But using one's indifferent heart
To dig an uncrossable river
For the one who loves you.。