import java.io.*;
public class FileIODemo2
{
public static void main(String args []) throws Exception
{

   FileOutputStream out = new FileOutputStream("C:\\javawork\\test2.txt");
   out.write(53);
            out.close();
}
}为什么写入test2.txt的内容是5啊??不明白,请大家解释一下啊!小弟初学,最好详细点好!

解决方案 »

  1.   

    void write(int b) 
              Writes the specified byte to this file output stream. 
    53对应ASCII Chart中的字符5,你写入的是字节,而不是整数!
      

  2.   

    FileOutputStream要求输入的参数是byte类型的对象或者数组。而53其实有两个byte,所以楼主输出的时候只有一个5。请楼主参考以下例程,它把一个String转成byte【】之后再输出import java.io.*;
    public class FileIODemo2
    {
    public static void main(String args []) 
    {

    String test="3424324"; try{
       FileOutputStream out = new FileOutputStream(new File("g:\\test2.txt"));
       out.write(test.getBytes()  );
      out.close();}
    catch(Exception exc){
    exc.printStackTrace(); 
             
    }
    }
    }
      

  3.   

    楼上说的对public void write(int b)
               throws IOException
    Writes the specified byte to this file output stream. Implements the write method of OutputStream. Parameters:
    b - the byte to be written. FileOutputStream写入的不是int的值,而是相应数值的字符
      

  4.   

    FileOutputStream的参数是byte类型的对象或数组。而53是两个byte,但又没有放到byte数组中,所以导致3丢失,可以"53".getBytes()转化为字节数组才写到文件中
      

  5.   

    错了
    是 xiaohuasz() 说的对
    如果想要输出53,可以这样
    out.write(53 + "");
      

  6.   

    53对应ASCII是5,所以写入的是5.
      

  7.   

    congliu(取次花丛都不顾,半缘修道半缘君。) 
     wangdong_hk(王栋)
    说的是错的,不信,可以调用out.write(5); 试一下,看看结果先。
      

  8.   

    write(int b) 
              Writes the specified byte to this file output stream.
    这个方法到底怎么啦?write方法后面明明写的int b啊  怎么就write(53) 后结果输出就成了ASCII码对应的字符了???xiaohuaaz 你能不能再说清楚点啊。“你写入的是字节,而不是整数!”我不明白你这句话。
      

  9.   

    write
    public abstract void write(int b)
                        throws IOException
    Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. 
    Subclasses of OutputStream must provide an implementation for this method. 
    Parameters:
    b - the byte. 
    Throws: 
    IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    上面是jdk中OutputStream的write方法的描述,它是个抽象方法,具体在FileOutputStream实现
    OutputStream是二进制流,int是32位,53换算为二进制为00000000 00000000 00000000 00110101,根据The 24 high-order bits of b are ignored,则实际流中传输的为00110101,即一个字节,而其输入到文件中显示为字符的话就是5
      

  10.   

    public abstract void write(int b)
    53对应ASCII是5,所以写入的是5.
    同意这种说法。
      

  11.   

    多谢xiaohuasz的详细解答!多给点分你  呵呵