难道不是用字节流?
FileOutputStream?

解决方案 »

  1.   

    我用fieoutputstream写了一个程序,可惜还是差了三个字节,c++写整数的时候是写4个字节,而java不是,16进制代码如下:
    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占几个字节然后去补足四个字节吗?
      

  2.   

    ww_file.Write(&i,sizeof(int));//sizeof(int)=4,所以写入4个字节,04 00 00 00java中
    write(int) 只写入1个字节,
    改用write(byte[],int offset,int len);
      

  3.   


    write(int) 只写入1个字节,写入一个大于256的数,那数值肯定就不对了,java的这个write(int)是不是有问题啊?
      

  4.   

    help中很明确,写一个byte; 
    具体为什么函数参数用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) {
         }
      

  5.   

    不用 byte 而用 int 是因为 Java 里面没有 unsigned 类型
    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();
        }
    }