public static void main(String[] args){
File file2=new File(args[0]);
try{
BufferedReader ar=new BufferedReader(new FileReader(file2));

String s;
while((s=ar.readLine())!=null){
System.out.println(s);
    }
ar.close();
}catch(IOException e){
}
         }
////////////////////////////////////////////
public static void main(String[] args){
File file2=new File(args[0]);
try{
BufferedReader ar=new BufferedReader(new FileReader(file2));

String s="";
while(ar.read()!=-1){
s+=(char)ar.read();
    }
System.out.println(s);
ar.close();
}catch(IOException e){
}
         }
为什么两个读出来的都不同呢

解决方案 »

  1.   

    1, ra.readLine()是读一行,打印一行,是打印n个字符串
    2, ra.read()你看一下读出来的是什么类型,但是只是打印一个字符串
    这个我还没有试过,我看一下回答
      

  2.   

    import java.io.*;
    class aa
    {
    public static void main(String[] args){
    File file2=new File(args[0]);

    try{
    BufferedReader ar=new BufferedReader(new FileReader(file2));

    String s="";
    int c = ar.read();
    while(c!=-1){
    s+=(char)c;
    c=ar.read();
    //s+=(char)ar.read();
    }
    System.out.println(s);
    ar.close();
    }catch(IOException e){
    }
             }
    }
    ar.read()的注释为:
    read
    public int read()
             throws IOExceptionRead a single character. Overrides:
      read in class Reader
    Returns:
      The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the  end of the stream has been reached 
    Throws: 
    IOException - If an I/O error occurs
    因为你从中读了两次字节,但输出了一个字节
      

  3.   

    while(ar.read()!=-1){
    s+=(char)ar.read();
        }
    ar.read()读了两边,就不一样了啊。