难道不是用字节流?
FileOutputStream?
FileOutputStream?
解决方案 »
- 如何将一个JAVA类运行的结果输出到JComboBox选项中
- private的构造函数能直接使用?
- servlet 显示中文问题
- 客户端用Applet浏览,那么客户端也要装jdk吗?
- 我用adsl拨号,每次Ip都不一样,这样是不是算固定IP?
- 请问的流问题,我急呀,是程序员的都来帮一把,谢谢!
- YuLimin(阿敏当兵)100分2次
- Jpanel 自适应大小的问题
- How can I include JVM instructions in my Java Program to increase the efficiency of my code.
- 如何响应JTREE中的节点点击事件??
- 电子白板的实现?
- 高分求助:哪里有聊天室的源代码??送分!!!
04 31 33 30 30 30 30 30 30 30 30 31 00 00 00 00
00 00 00 00 00 31 33 30 30 30 30 30 30 30 30 32
00 00 00 00 00 00 00 00 00难到我得自己判断j占几个字节然后去补足四个字节吗?
write(int) 只写入1个字节,
改用write(byte[],int offset,int len);
write(int) 只写入1个字节,写入一个大于256的数,那数值肯定就不对了,java的这个write(int)是不是有问题啊?
具体为什么函数参数用int,不清楚(可能为了与native实现相匹配有关吧,猜的)
/**
* Writes the specified byte to this file output stream. Implements
* the <code>write</code> method of <code>OutputStream</code>.
* @param b the byte to be written.
*/
public native void write(int b) throws IOException;就这个问题,给段与c++功能相同的代码 byte[] aa = new byte[20];
byte[] b2 = new byte[4];
byte[] t = null;
int n = 4;
try {
FileOutputStream fos = new FileOutputStream(new File("c:\\111.txt"));
b2[0] = (byte) (n);
b2[1] = (byte) (n >> 8);
b2[2] = (byte) (n >> 16);
b2[3] = (byte) (n >> 24);
fos.write(b2, 0, 4);
String s_aa = "13000000001";
String s_bb = "13000000002";
t = s_aa.getBytes();
Arrays.fill(aa, (byte) 0);
for (int i = 0; i < t.length; ++i) {
aa[i] = t[i];
}
fos.write(aa, 0, aa.length);
t = s_bb.getBytes();
Arrays.fill(aa, (byte) 0);
for (int i = 0; i < t.length; ++i) {
aa[i] = t[i];
}
fos.write(aa, 0, aa.length);
fos.close();
}
catch (Exception e) {
}
signed byte 只能表示 -127~127 之间的内容, 所以这里使用了 int除了楼上兄弟的方法, 也可以用 New I/O 解决这个问题import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;public class LowLevelWrite {
public static void main(String[] args) throws IOException {
FileChannel fc = new RandomAccessFile("LowLevelWrite", "rw").getChannel();
ByteBuffer bb = ByteBuffer.allocate(1024);
int i = 4;
String str1 = "13000000001";
String str2 = "13000000002"; bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(i);
bb.put(str1.getBytes("UTF-8"));
bb.position(bb.position() + 20 - str1.length());
bb.put(str2.getBytes("UTF-8"));
bb.position(bb.position() + 20 - str2.length());
bb.flip();
fc.write(bb); // Write file.
fc.close();
}
}