package IOStreamTest;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;public class InputStreamReaderTest {
public static void main(String[] args) {
try{
int rs;
File file = new File("F:\\","file2.txt");
if(!file.exists()){
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
System.out.println("The content of file2.txt is:");
while ((rs = isr.read())!=-1){
System.out.print((char)rs);
}
isr.close();

}catch (IOException e){
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    字节流读出来就是byte,能不能转换成字符说不准。字符流的话,应该是要看编码来具体转换成字符吧。
      

  2.   

    public class InputStreamReaderextends ReaderInputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。 每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:  BufferedReader in
       = new BufferedReader(new InputStreamReader(System.in));read
    public abstract int read()
                      throws IOException从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。 
    子类必须提供此方法的一个实现。 
    返回:
    下一个数据字节;如果到达流的末尾,则返回 -1。 
    抛出: 
    IOException - 如果发生 I/O 错误。
      

  3.   

    我试着把InputStream isr = new InputStreamReader(fis)这行去掉,然后把后边的isr全部改成fis,这样运行的结果是一样的。既然是一样的,为什么还要把字节流转换成字符流?不知道while后面的条件语句中rs = isr.read()怎么理解,isr.read()括号里为什么没有用于存储读取的内容的数组参数,比如为什么不这样写:Byte []bytes = new Byte[512];while((rs = isr.read(bytes,0,512))!=-1){       String s = new String(bytes,0,rs);
           
           System.out.println(s);
    }rs是不是可以理解成当前成功读取的字节的数目?若已读到流结尾,返回值-1?我是新手,还望高手给我指条明路,谢谢了!
      

  4.   

    to "wclxyn":
    那我这样写为什么运行结果一样的呢?
    int rs;
    byte bytes[] = new byte[512];
    File file = new File("F:\\","file2.txt");
    if(!file.exists()){
    file.createNewFile();
    }
    FileInputStream fis = new FileInputStream(file);
    // InputStreamReader isr = new InputStreamReader(fis);
    System.out.println("The content of file2.txt is:");
    while ((rs = fis.read(bytes,0,512))!=-1){
    // System.out.print((char)rs);
    String str = new String(bytes,0,rs);
    System.out.println(str);
    }
    fis.close();
      

  5.   

            int rs;
            File file = new File("F:\\","file2.txt");
            if(!file.exists()){
                file.createNewFile();
            }
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            System.out.println("The content of file2.txt is:");
            while ((rs = isr.read())!=-1){
                System.out.println((char)rs);
            }
            isr.close();
    我试了下上面这个代码,发现这个输出是这样进行的:执行while条件语句,读取一个字符,判断条件为真,输出这个字符,接着再返回while条件语句,继续读取下一个字符,再判断条件为真,再输出.....直到判断条件为假才返回-1。我刚查了下,这个isr.read()是表示读入一个字符,但是不知道这个返回的rs到底是什么?是当前读取的这个字符吗?
      

  6.   

    lz需要先理解下Java的byte和char类型。
      

  7.   

    http://www.ibm.com/developerworks/cn/java/j-lo-javaio/index.html?ca=drs-