有一个 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是可以真实写入的。
现在我想在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是可以真实写入的。
有可能和字符集有关。
.
------------------------------------------------------------------
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();
}