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啊??不明白,请大家解释一下啊!小弟初学,最好详细点好!
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啊??不明白,请大家解释一下啊!小弟初学,最好详细点好!
Writes the specified byte to this file output stream.
53对应ASCII Chart中的字符5,你写入的是字节,而不是整数!
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();
}
}
}
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的值,而是相应数值的字符
是 xiaohuasz() 说的对
如果想要输出53,可以这样
out.write(53 + "");
wangdong_hk(王栋)
说的是错的,不信,可以调用out.write(5); 试一下,看看结果先。
Writes the specified byte to this file output stream.
这个方法到底怎么啦?write方法后面明明写的int b啊 怎么就write(53) 后结果输出就成了ASCII码对应的字符了???xiaohuaaz 你能不能再说清楚点啊。“你写入的是字节,而不是整数!”我不明白你这句话。
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
53对应ASCII是5,所以写入的是5.
同意这种说法。