java 输入输出流 (被采纳为答案者加100分)

java中包含许多输入输出的类象BufferedInputStream, FileInputStream, InputStreamReader等等。请问这些有什么区别? 回答正确后我将给分(因为我的积分很少,不能瞎扣了,请理解)

其中BufferedInputStream是FileInputStream的子类,你可以理解成同样处理一个文件,BufferedInputStream效率更高,原因是BufferedInputStream采用了更高效的字节流处理方式,
BufferedInputStream才用缓冲流把内在的缓冲器连接到I/O流,允许java程序对多个字节同时操作,这样就提高了效率。

inputstreamreader的构造函数带两个参数,一是关联到的文件,二是字符解码方式. 所以实际上通过inputstreamreader实例读出来的东西已经不是磁盘上原始的字节数据了,而是根据你指定的解码方式(如果你没有指定,则使用系统缺省的,win2000下是gbk/gb2312)把字节流转换成了字符流,注意字节流和字符流的区别,一个字节就是8比特位(32位机器上),而一个字符含多少字节则与不同的编码/解码方式有关了,如gbk是一字节,utf-8是1-3的变长字节,utf-16是2个定长字节.
   于是值得你注意的就是当你用inputstreamreader读文件时,你应该知道该文件被存储时是用什么方式编码的,否则你指定错了解码方式,读出来的就是乱码.但是退一步来说,在全英文环境下,问题也没这严重.因为所有的字符集在前七位上都是与ascii兼容的(我猜的,也许有的不是),然而当你的程序涉及中文字符时,肯定是会出错了.
   那么fileinputstream的特点呢?它的构造函数就一个,即关联到的文件,既然没有指定解码方式,那它所做的就是只以字节流的方式读出文件而不做任何处理, 你应该用一个字节数组来接受它,对该数组你以后还可以做任何想做的操作。
给你个例子,自己去测试
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.*;

public class test {

/* public static void main(String[] args) {
String str=new String("Face recognition in the thermal infrared domain has received relatively little attention in the literature in comparison with recognition in visible-spectrum imagery");
StringTokenizer token=new StringTokenizer(str);
Hashtable ht=new Hashtable();
while(token.hasMoreTokens()){
String temp=new String(token.nextToken());
ht.put(temp,temp);
}
Enumeration en=ht.keys();
while(en.hasMoreElements()){
Object obj=en.nextElement();
System.out.print("KEY_NO:"+obj);
System.out.println("="+ht.get(obj));
}
}
*/

public static void main(String[] args){
try {
String file1 ="d:\\1.doc";
String file2 ="d:\\2.doc";
copyFile(file1,file2);
readFile(file2);
//fileCheck("d:\\test1.txt");
// readFile("D:\\test1.txt");
// readFileByte("D:\\test1.txt");
// readFileByFile("D:\\test1.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void testFile() throws IOException{
copyFile("D:\\test1.txt","D:\\test2.txt");

}
public static void copyFile(String inName,String outName) throws IOException
{
File tmp = new File(outName);
if(!tmp.canRead())tmp.createNewFile();

BufferedInputStream in = new BufferedInputStream(new FileInputStream(inName));
BufferedOutputStream out= new BufferedOutputStream(new FileOutputStream(outName));
copyFile(in,out,true);
}
public static void readFile(String inName) throws IOException
{
BufferedReader read = new BufferedReader (new InputStreamReader(new FileInputStream(inName)));
String b ;
while((b=read.readLine())!=null )
print( b);
}
public static void readFileByte(String inName) throws IOException
{
BufferedInputStream read = new BufferedInputStream (new FileInputStream(inName));
int b = 0;
while((b=read.read())!=-1)
System.out.print ((char)b);
}
public static void readFileByFile(String name) throws IOException
{
File tmp = new File (name);
FileReader fr= new FileReader(tmp);
BufferedReader br = new BufferedReader(fr);
String b;
while((b=br.readLine())!=null)
print(b);

}

public static void copyFile(InputStream in ,OutputStream out, boolean close) throws IOException{
int b;
while((b=in.read())!=-1)
{
out.write(b);
}
in.close();
if(close)
out.close();
}
public static void print(Object o)
{
System.out.println(o);
}
public static void fileCheck(String name) throws IOException
{
print("---"+name+"---");
File f= new File(name);

if(!f.exists())
{
print("fle not exist!");
return;
}

print("Canonical name:"+f.getCanonicalPath());
String p= f.getParent();
if(p!=null)
print("Parent directory :"+p);

if(f.canRead())print("file can be read!");

if(f.canWrite())print("file can be writable!");

Date d = new Date();
d.setTime(f.lastModified());
print("last modified time :"+d);

if(f.isFile())
{
print("file size is :"+f.length()+" bytes");
}else if(f.isDirectory()){print("is a directry!");}
else{
print("neither a directory or a file!");
}

print("");

}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-22
Input和Output 1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型: 1.1 以字节为导向的stream 以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种类型: 1) input stream: 1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用 2) StringBufferInputStream:把一个String对象作为InputStream 3) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作 4) PipedInputStream:实现了pipe的概念,主要在线程中使用 5) SequenceInputStream:把多个InputStream合并为一个InputStream 2) Out stream 1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中 2) FileOutputStream:把信息存入文件中 3) PipedOutputStream:实现了pipe的概念,主要在线程中使用 4) SequenceOutputStream:把多个OutStream合并为一个OutStream 1.2 以Unicode字符为导向的stream 以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种类型: 1) Input Stream 1) CharArrayReader:与ByteArrayInputStream对应 2) StringReader:与StringBufferInputStream对应 3) FileReader:与FileInputStream对应 4) PipedReader:与PipedInputStream对应 2) Out Stream 1) CharArrayWrite:与ByteArrayOutputStream对应 2) StringWrite:无与之对应的以字节为导向的stream 3) FileWrite:与FileOutputStream对应 4) PipedWrite:与PipedOutputStream对应 以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。 如CharArrayReader:和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。 1.3 两种不现导向的stream之间的转换 InputStreamReader和OutputStreamReader:把一个以字节为导向的stream转换成一个以字符为导向的stream。 2. stream添加属性 2.1 “为stream添加属性”的作用 运用上面介绍的Java中操作IO的API,我们就可完成我们想完成的任何操作了。但通过FilterInputStream和FilterOutStream的子类,我们可以为stream添加属性。下面以一个例子来说明这种功能的作用。 如果我们要往一个文件中写入数据,我们可以这样操作: FileOutStream fs = new FileOutStream(“test.txt”); 然后就可以通过产生的fs对象调用write()函数来往test.txt文件中写入数据了。但是,如果我们想实现“先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中”的功能时,上面的API就没有一个能满足我们的需求了。但是通过FilterInputStream和FilterOutStream的子类,为FileOutStream添加我们所需要的功能。 2.2 FilterInputStream的各种类型 2.2.1 用于封装以字节为导向的InputSt