in.read() 返回的就是你实际读的字节啊,
out.write(c)只把低8位写出。

解决方案 »

  1.   

    From JDK API Document:
    public int read()
             throws IOExceptionReads a byte of data from this input stream. This method blocks if no input is yet available.
    Overrides:
    read in class InputStream
    Returns:
    the next byte of data, or -1 if the end of the file is reached.
    Throws:
    IOException - if an I/O error occurs.
    -----------------------------------------------------------------
    read return a byte code read from stream. That's why using write(c) can write a correct character to the outputstream.
    You can use if(c == '\n') to check if the character is a return carrier.
      

  2.   

    farawayzheng_necas老兄,能说详细一点吗?
    in.read()被定义为int啊,返回的不是读出数据的长度吗??
    (in.read()).equals("\n")这个写法不能成立啊,假如强制转换in.read()的类型,那它也只是一个数字字符(比如"8"),永远也不可能等于回车符的,请详细解释.
    解决后马上给分
      

  3.   

    import java.io.*;
    public class TestWrite {
       public static void main(String args[]){
        try{
          File file=new File("1.txt");
      FileInputStream in=new FileInputStream(file);
          PrintStream out = new PrintStream(new FileOutputStream("2.txt"));
          int c;
    while((c=in.read())!=-1)
    {
          out.write(c);
          out.flush();
    }    }catch (IOException e){
      System.out.println("IOException");
      }
     }
    }
      

  4.   

    out.write(c)是写长度为c的字符流,所以可以写入"abc"。
      

  5.   

    上面这位老兄张贴的代码除了使用了PrintStream来写数据之外,对于我的问题有任何帮助吗?恕兄弟眼拙,还没看出来
      

  6.   

    namowen(寒号不已)老兄解决了第二个问题,暂记40分
    请问如何判断in.read()读出来的是什么自符??(该问60分)
      

  7.   

    in.read()得到的是输入字符的ASCII码值。
    如果要与‘\n’比较,应该这么写
    (in.read()).equals('\\n');
      

  8.   

    ok,我回去看看是change(程序人生)老兄的(c=="\n")对还是namowen(寒号不已)兄的答案对,明天来结帖.
    谢谢诸位
      

  9.   

    import java.io.*;public class TestWrite {

       public static void main(String args[]){
        try{
          File file=new File("testwrite1.txt");
      FileInputStream in=new FileInputStream(file);
          PrintStream out = new PrintStream(new FileOutputStream("testwrite2.txt"));
          //File newFile=new File("2.txt");      //不支持中文
      //FileWriter out=new FileWriter(newFile);      int c;
    while((c=in.read())!=-1)
    {
    System.out.println(c);
    if(c==13){
    out.print("<br>");
    }else{
    out.write(c);
           out.flush();
    }       
    }    }catch (IOException e){
      System.out.println("IOException");
      }
     }
    }
    你想这样码?
      

  10.   

    ok,我回去看看是change(程序人生)老兄的(c=="\n")对还是namowen(寒号不已)兄的答案对,明天来结帖.
    谢谢诸位
      

  11.   

    jimok(Jim)兄请解释一下,你的意思是in.read()返回的是字符的ascII
    值吗??
    wrox出版的<java网络编成指南>说in.read()返回读取的长度,莫非真是它错了?
      

  12.   

    是啊,你可以用System.out.println(c)看看啊,你一看就知道怎么做了
      

  13.   

    ok,谢谢各位,I'll be back
      

  14.   

    FileInputStream input = new FileInputStream("1.txt");
    FileOutputStream output = new FileOutputStream("2.txt");
    byte[]     buffer = new byte[128];
    int        charsRead;
    charsRead = input.read(buffer);
    但是你如果用流的方式读,就会返回流的长度了,你自研究一下拉
      

  15.   

    没有这么麻烦,用BufferedReader就可以
    例:BufferedReader reader=new BufferedReader(new FileReader(filename));
    String line="";
             String buf="";
    while((line=reader.readLine())!=null){
                    buff+=line+"<br>";  
             }
      .....
    然后再写将buf写入文件
      

  16.   

    〉out.write(c)是写长度为c的字符流,所以可以写入"abc"。
      哪个流有这种用法啊?
      

  17.   

    不好意思。我错了。:)
    out.write(c)就是写入c对应的字符。这里的c是in.read()从in中依次读出来的,直到文件结尾时返回-1为止,所以可以写入"abc"。
      

  18.   

    > farawayzheng_necas老兄,能说详细一点吗?
    > in.read()被定义为int啊,返回的不是读出数据的长度吗??
         返回的不是读出数据的长度, 它返回的是你读出的字节(byte),强制转化为int的。你看的书害人不浅。> (in.read()).equals("\n")这个写法不能成立啊,假如强制转换in.read()的
      in.read返回int型,equals是Object的方法,当然不能用于int型。
    > 类型,那它也只是一个数字字符(比如"8"),永远也不可能等于回车符的,请详
      读入的int的低8位就是ascii码(文本格式的情况),对应的out.write(c)写出的也是c的低8位(就是ascii码),out.write("abc")要看你用的什么流,一般的流是没有这个方法的,你可以用PrintStream。PrintStream写String的一般方法是把String中的Char(16位)提取出来,如果高8位为0,则只输出低8位,否则全部输出。
          
    > 细解释.
    > 解决后马上给分
      同意:)
      

  19.   

    in.read()读出的是int,由于java中的char类型与int类型可以进行比较操作,实质上这两者都是ascii码(与C,C++一致),而回车符(char)对应的ascii字符为'n',所以可用if(in.read() == '\n')进行判断。(注意,此处是单引号,而非双引号)。