FileOutputStream有一个方法: public native void write(int b) throws IOException;这个方法的参数是int b (在Java中是四个字节),并且这个参数b就是要写入的字节。
但是这个方法的说明文档中说:
Writes the specified byte to this file output stream. Implements the <code>write</code> method of <code>OutputStream</code>.
也就是说这个方法是每次写入一个指定的字节.我想问,是不是将int的最后一个字节(最低字节)写入文件中,前三个字节都不要了。我有一个可证明这种想法的例子,就是在DataOutputStream中有一个writeInt(int v)的源代码:
public final void writeInt(int v) throws IOException {
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(4);
}
我们可以看到,通过>>>位操作,将整形v中四个字节的每一个字节都移动到最后一个字节(最低字节),并且write出了。是不是可以说明需要写入的就是最后一个字节(最低字节)。
但是这个方法的说明文档中说:
Writes the specified byte to this file output stream. Implements the <code>write</code> method of <code>OutputStream</code>.
也就是说这个方法是每次写入一个指定的字节.我想问,是不是将int的最后一个字节(最低字节)写入文件中,前三个字节都不要了。我有一个可证明这种想法的例子,就是在DataOutputStream中有一个writeInt(int v)的源代码:
public final void writeInt(int v) throws IOException {
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(4);
}
我们可以看到,通过>>>位操作,将整形v中四个字节的每一个字节都移动到最后一个字节(最低字节),并且write出了。是不是可以说明需要写入的就是最后一个字节(最低字节)。
解决方案 »
- 多线程更新Jtable时报错,求助
- 关于执行SQL的存储过程
- c.set(Calendar.MONTH, 4); System.out.println(c.get(Calendar.MONTH));
- 怎么样按照要求组合数字
- 在注册表中遍历指定文件夹下的子目录?
- JSP小问题
- 有尝求JCreator3.5的解破版!
- 要去面试了,大家帮忙给出出主意
- java的应用领域!——请正在从事java开发的朋友进来!
- 提示没有参数,找不到适合的方法,关于抽象方法的;请大家帮忙看看,谢谢!
- 有时候他会卡住在line = reader.readLine()) != null
- 我想把字符串"white"转变成Color值
FileOutputStream fo = new FileOutputStream("D:/a.txt");
fo.write(97);
fo.write(97+1<<8);
写入byte,还有另一个方法啊
write(byte[],int,int)
int v = 0x61626364;//0x61为'a' ASCII
out.write(v >>> 24);
out.write(v >>> 16);
out.write(v >>> 8);
out.write(v);//v
System.out.println("" + v + "," + (char) (v & 0xff));
out.close();
/*run:
1633837924,d
成功生成(总时间:2 秒)
*//*D:/a.txt中
abcd
*/
多查查API。这是权威的东西。
谢谢大家的回帖,谢谢楼上的,光看FileInputStream了,没有看祖宗InputStream。上面是有说:
low-order bits of the argument <code>b</code>. The 24 high-order bits of <code>b</code> are ignored.可以结贴了
就是一个a呀,97的int型高24bit全是0,最低一个字节才是97,所以写入了是字符a(ASCII 97)。Windows平台好像默认的是ISO-8859-1编码的。