import java.io.*;public class TestIO
{
   public static void main(String [] args){
   
      InputStreamReader r = new InputStreamReader(System.in);
      OutputStreamWriter w = new OutputStreamWriter(System.out);
      char[] buf = new char[100];
      int n = 0;      try{
while((n = r.read(buf)) > 0){
    System.out.println(new String(buf,0,n));
    w.write(buf,0,n);
}
      }catch(IOException e){
System.out.println("error " + e);
System.exit(1);
      }
   }
}
你的程序没有问题呀,你把输出的语句给注释了干吗?

解决方案 »

  1.   

    晕,注释掉的那句是我自己调试用的,我想在注释后让w.write(buf,0,n);这句显示结果,可是不知道为什么不行
      

  2.   

    System.out.println(new String(buf,0,n));就可以了不需要w.write(buf,0,n);
    我还提供了一输入生成新文件的程序:
    import java.io.*;public class CreateFile{
    public static void main(String[] args){
    String strFilename;
    DataInputStream stdIn = new DataInputStream(new BufferedInputStream(System.in));

    try{
    if (args.length == 0){
    System.out.print("Please Input the File Name:");
    strFilename = stdIn.readLine();
    }
    else{
    strFilename = args[0];
    }
    DataOutputStream NewFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(strFilename)));
    int strTemp;
    System.out.println("Please input the context,End with \'#\'");
    strTemp = stdIn.read();
    while(strTemp != '#'){
    NewFile.write(strTemp);
    strTemp = stdIn.read();
    }
    System.out.println(NewFile);
    NewFile.close();

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

  3.   

    import java.io.*;public class TestIO
    {
       public static void main(String [] args){
       
          InputStreamReader r = new InputStreamReader(System.in);
          OutputStreamWriter w = new OutputStreamWriter(System.out);
          char[] buf = new char[100];
          int n = 0;      try{
    while((n = r.read(buf)) > 0){
        w.write(buf,0,n);
        w.flush();
    }
          }catch(IOException e){
    System.out.println("error " + e);
    System.exit(1);
          }
       }
    }
      

  4.   

    try{
    while((n = r.read(buf)) > 0){
        w.write(buf,0,n);
        w.flush();
    }
      

  5.   

    试过了,flush()之后显示了