有一个 byte值,值为 0x80 (即:128),在ASCII扩展表中对应有可见字符的。
  现在我想在JAVA程序中把这个byte值写入一个日志文本文件中。
    如何写入?    我希望用 UltraEdit-32 打开这个日志文本文件,查看ASCII码,还是:0x80 。------------------------------------------------------------------------------------------
  我测试的结果是:new String( b ) 后,写入文本文件,查看ASCII值,变成了 0x3F  使用VC写的程序如下:// mytest.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>int main(int argc, char* argv[])
{
    FILE *fp = fopen("D:\\my.txt", "a");

    unsigned short us1 ;
us1 = (unsigned short)strlen("PLC001") | 0x08000;
    u_short   us = htons(us1);

printf("(short)strlen(\"PLC001\") | 0x08000 =%d\n",us1); fprintf(fp,"%s",&us,sizeof(u_short));
    fclose(fp);
return 0;
}   查看ASCII码,前面两个字节是 0x80 , 0x06 ,说明C是可以真实写入的。

解决方案 »

  1.   

    DataOutputStream中的方法  writeByte
      

  2.   

    写一个byte进去不行么。
    有可能和字符集有关。
    .
      

  3.   

    java中字符都是双字节的,所以不能用string,或者char 回复人: nimifeng(学海无涯.......苦啊苦啊苦作舟....理解是美!!!) ( ) 信誉:100  参照说的即可
      

  4.   

    谢谢各位的提示!!!!成功了。这次让我知道了什么叫流(Stream)
    ------------------------------------------------------------------
          FileOutputStream fos = null;
          File myFile = new File("D:\\test.txt");
          byte[] b = new byte[1];
          b[0] = (byte)0x80;
          try{
            if (!myFile.exists()) {
              myFile.createNewFile();
            }
            fos = new FileOutputStream(myFile);
            fos.write(b);
          }
          catch (Exception e) {
            e.printStackTrace();
          }