import java.io.*;
class BinFile
{
public static void main(String args[]) throws Exception
{
OutputStream o=new FileOutputStream("TEST.data");
DataOutputStream out=new DataOutputStream(o);
String str="TEST_CODE";
out.writeChars(str);
out.close();
o.close();
}
}
这样生成的二进制文件,对吗? 为什么通过UtralEdit 打开还依然可见。TEST_CODE
谢谢!
class BinFile
{
public static void main(String args[]) throws Exception
{
OutputStream o=new FileOutputStream("TEST.data");
DataOutputStream out=new DataOutputStream(o);
String str="TEST_CODE";
out.writeChars(str);
out.close();
o.close();
}
}
这样生成的二进制文件,对吗? 为什么通过UtralEdit 打开还依然可见。TEST_CODE
谢谢!
from www.kingofcoder.com 編程王網站
String str = "TEST_CODE";
byte bytes[] = str.getBytes();
FileOutputStream fos = new FileOutputStream("TEST.data");
fos.write(bytes, 0, bytes.length);
fos.close();
byte bytes[] = str.getBytes();
FileOutputStream fos = new FileOutputStream("TEST.data");
fos.write(bytes, 0, bytes.length);
fos.close();
这个方式也不对!
String test ="你好";
xzwsun:
以你的输入流来确定,用何种方式读。
class BinFile
{
public static void main(String args[]) throws Exception
{
OutputStream o=new FileOutputStream("TEST.data");
DataOutputStream out=new DataOutputStream(o);
String str="java_zhaidp 你好";
out.writeChars(str);
out.close();
o.close();
}
}
RandomAccessFile rf = new RandomAccessFile(f, "rw");
rf.writeBytes(str);
rf.close();
String str = "TEST_CODE"; int ss = Integer.parseInt("T");提示:java.lang.NumberFormatException: For input string: "d"呵呵!看来是比较费劲!
你用二进制方式写文件,但是你用文本方式打开文件(notepad,ue等都是用文本方式打开文件的),还是能看到原来的内容的。
二进制写文件,并不是把字符串的内容转成相应的二进制字符串再写文件,而是直接把字符串以二进制的方式写到文件,注意这里的区别。
计算机只认识二进制,所以硬盘里存的数据也都是二进制,不管你把字符串“A”以二进制的方式还是以文本的方式写到文件,计算机都是把“A”的二进制代码(41)写到硬盘。当打开文件的时候,以二进制方式读取时,会得到41这样的二进制代码,当你文本方式读取时,文件系统会根据ASCII表把二进制41转换为A字符。所以当你以notepad打开文件时,就会显示出A。但并不代表你用二进制写文件无效。这个7楼就提到了。
如果对于“A”字符串,LZ想在文件中的到的结果是41(这个是“41”字符串,就不再是“A”了),那么就只能自己转换(即把A转换为41),然后再写到文件。但是,你读文件的时候也必须要自己再还原回原来的字符串(即把41转换为A),否则读出的结果就不再是原来的内容了。
for exampleimport java.io.*;
class BinFile {
public static void main(String args) {
String fileName = System.getProperty(user.dir) + "/test.data";
String data = "TEST_DATA";
writeByte(fileName, data);
data = readByte(fileName);
System.out.println("data from file:" + data);
} public static void writeByte(String fileName, String data) {
try {
OutputStream os = new FileOutputStream(fileName);
char[] ch = data.toCharArray();
StringBuilder sb = new StringBuilder();
String buf;
for (int i=0; i<ch.length; i++) {
buf = Integer.toHexString(ch[i]>>8); //高8位
if (buf.length>2) buf = buf.substring(buf.length-2, buf.length);
sb.append(buf);
buf = Integer.toHexString(ch[i]); //低8位
if (buf.length>2) buf = buf.substring(buf.length-2, buf.length);
sb.append(buf);
}
os.write(buf.toString().getBytes()); //
os.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static String readByte(String fileName) {
try {
InputStream is = new FileInputStream(fileName);
byte[] bt = new byte[new File(fileName).length()];
is.read(bt);
StringBuilder sb = new StringBuilder();
String buf = new String(bt);
char ch;
for (int i=0; i<buf.length()-4; i+=4) {
ch = (byte)Integer.parseInt(buf.substring(i, 2), 16);
ch <<= 8; //高8位
ch |= (byte)Integer.parseInt(buf.substring(i+2, 2), 16); //低8位
sb.append(buf);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
随手写的代码,不一定正确,LZ可以参考修改。
try {
InputStream is = new FileInputStream(fileName);
byte[] bt = new byte[new File(fileName).length()];
is.read(bt);
StringBuilder sb = new StringBuilder();
String buf = new String(bt);
char ch;
for (int i=0; i<buf.length()-4; i+=4) {
ch = (byte)Integer.parseInt(buf.substring(i, 2), 16);
ch <<= 8; //高8位
ch |= (byte)Integer.parseInt(buf.substring(i+2, 2), 16); //低8位
sb.append(ch); //这里笔误
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public class Test { public static void main(String[] args) {
String s = "abcdef";
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < s.length();i++){
int temp = s.charAt(i);
String ss = Integer.toBinaryString(temp);
String sss = null;
if(ss.length()%2 != 0){
ss = "0" + ss;
}
for(int j = 1 ; 2*j <= ss.length() ;j++){
sss = ss.substring((j-1)*2, 2*j);
sb.append(sss + " ");
}
sb.append("\n");
}
System.out.println(sb.toString());
//将 sb.toString()写入文件
}}
OutPut:
难道LZ要这种效果:
01 10 00 01
01 10 00 10
01 10 00 11
01 10 01 00
01 10 01 01
01 10 01 10
你的程序我做了下修改,可以运行但好像只写了末尾的 字母。我再仔细读读程序。
你说的没错,我可能是误导大家了,实际上我的 out.writeChars()的方法是对的。但我有一点不是很明白,就是我下面串,是逐个写入还是
分开写入? 没试过。
String data = "TEST_DATA";
非常感谢!
只是将 一个String = "TEST_DATA2008" 这样的字符串,生成一个二进制文件即可。
好像需要把 String 转换成 byte数组才行!下面是从一个二进制文件读出来,然后再写进一个新的文件。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = new File("c:\\xzw\\EPKMF_TR_20071121_01.data");
byte[] content=readFile(file);
System.out.println(content);
writeBytes("c:\\xzw\\EPKMF_TR_20071121_02.data",content);
}
static byte[] readFile(File file) throws Exception {
if (file.exists() && file.isFile()) {
long fileLength = file.length();
if (fileLength > 0L) {
BufferedInputStream fis = new BufferedInputStream(
new FileInputStream(file));
byte[] b = new byte[(int) fileLength];
while (fis.read(b)!= -1) {
}
fis.close();
fis = null; return b;
}
} else {
return null;
}
return null;
}
static boolean writeBytes(String filePath, byte[] content)
throws IOException {
File file = new File(filePath);
synchronized (file) {
BufferedOutputStream fos = new BufferedOutputStream(
new FileOutputStream(filePath));
fos.write(content);
fos.flush();
fos.close();
}
return true; }
}
synchronized (file) { //这个synchronized无意义
BufferedOutputStream fos = new BufferedOutputStream(
new FileOutputStream(filePath));
fos.write(content);
fos.flush();
fos.close();
} 改为
static synchronized boolean writeBytes(String filePath, byte[] content)
如果可以的话,你用C语言试试看用二进制方式写字符'A'和文本方式写字符'A',你看最后结果是什么。你用ue以二进制方式打开,看看里面有什么不同。
import java.io.*;class BinFile {
public static void main(String[] args) {
String fileName = System.getProperty("user.dir") + "/Diff/data.da";
String data = "TEST_CODE";
writeByte(fileName, data);
data = readByte(fileName);
System.out.println("data from file:" + data);
test();
}
public static void test() {
try {
FileOutputStream fileOutputStream =
new FileOutputStream(System.getProperty("user.dir") + "/Diff/test.da");
int a = 5; //注意和字符的区别,这里对应的二进制是FFFFFF05
fileOutputStream.write(5);
byte[] b = new byte[4];
b[0] = (byte)a;
b[1] = (byte)(a>>8);
b[2] = (byte)(a>>16);
b[3] = (byte)(a>>24);
fileOutputStream.write(b);
char c = 'A'; //注意和整形的区别,这里二进制是FF41
fileOutputStream.write(c);
byte[] d = new byte[2];
d[0] = (byte)c;
d[1] = (byte)(c>>8);
fileOutputStream.write(d);
fileOutputStream.close(); } catch (Throwable e) {
e.printStackTrace();
}
} public static synchronized void writeByte(String fileName, String data) {
try {
OutputStream os = new FileOutputStream(fileName);
char[] ch = data.toCharArray();
StringBuilder sb = new StringBuilder();
String buf = null;
for (int i=0; i<ch.length; i++) {
buf = "00" + Integer.toHexString(ch[i]>>8); //高8位
if (buf.length()>2) buf = buf.substring(buf.length()-2, buf.length());
sb.append(buf);
buf = "00" + Integer.toHexString(ch[i]); //低8位
if (buf.length()>2) buf = buf.substring(buf.length()-2, buf.length());
sb.append(buf);
}
os.write(sb.toString().getBytes());
os.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static String readByte(String fileName) {
try {
InputStream is = new FileInputStream(fileName);
byte[] bt = new byte[(int)new File(fileName).length()];
is.read(bt);
StringBuilder sb = new StringBuilder();
char ch;
String buf;
for (int i=0; i<=bt.length-4; i+=4) {
buf = new String(bt, i, 2);
ch = (char)Integer.parseInt(buf, 16);
ch <<= 8; //高8位
buf = new String(bt, i+2, 2);
ch |= (byte)Integer.parseInt(buf, 16); //低8位
sb.append(ch);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
---------------------
文件本来就是二进制的,你写进去的是字符串,看到的当然就是字符串了,如果你写进去一个int 5,你再看看你看到的是什么?
int a = 5; //注意和字符的区别,这里对应的二进制是00000005
char c = 'A'; //注意和整形的区别,这里二进制是0041
TO :阿宝我理解你的意思:int 和 字符 A 写进去没有问题,这个我确认了。 问题的焦点是我的方法是否正确!
你的方法没问题,其实跟wirte(byte[])方法效果一样的,只不过你的方法是以UTF-16BE的方式保存的而已
一下给出对比程序,你自己运行后对比一下结果就知道了。
import java.io.*;class BinFile {
public static void main(String[] args) throws Exception {
String fileName = System.getProperty("user.dir") + "/file/encode.da";
String data = "测试TEST_CODE";
//这里可以对比以下二进制
System.out.println("--------char mode----------");
char[] ch = data.toCharArray();
for (int i=0; i<ch.length; i++) {
System.out.printf("%04x", new Object[]{new Integer((int)ch[i])});
}
System.out.println();
System.out.println("--------byte mode----------");
byte[] bt = data.getBytes("UTF-16BE");
//byte[] bt = data.getBytes("UTF-16LE");
//byte[] bt = data.getBytes();
for (int i=0; i<bt.length; i++) {
System.out.printf("%02x", new Object[]{new Byte(bt[i])});
}
System.out.println();
//这种是保存二进制字符串
System.out.println("----------wirte as binary string----------");
writeEncode(fileName, data);
data = readEncode(fileName);
System.out.println("data from encode file:" + data);
//LZ的方法,是二进制写文件
System.out.println("----------wirte in binary mode(writeChars(String))----------");
fileName = System.getProperty("user.dir") + "/file/bin.da";
writeBin(fileName, data);
data = readBin(fileName);
System.out.println("data from bin file:" + data);
//普通write(byte[])方法,是二进制写文件
System.out.println("----------wirte in binary mode(write(byte[]))----------");
fileName = System.getProperty("user.dir") + "/file/byte.da"; //LZ可以比较这个文件和你自己方法的文件
writeByte(fileName, data);
data = readBin(fileName);
System.out.println("data from byte file:" + data);
}
//LZ的方法
public static synchronized void writeBin(String fileName, String data) { //LZ的方法
try {
OutputStream o = new FileOutputStream(fileName);
DataOutputStream out = new DataOutputStream(o);
out.writeChars(data);
out.close();
o.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
public static String readBin(String fileName) {
try {
InputStream is = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(is);
byte[] bt = new byte[(int)new File(fileName).length()];
dis.read(bt);
StringBuilder sb = new StringBuilder();
char ch;
for (int i=0; i<bt.length; i+=2) {
ch = (char)(0x00FF & bt[i]);
ch <<= 8;
ch |= (char)(0x00FF & bt[i+1]);
sb.append(ch);
}
return sb.toString();
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
//普通write(byte[])方法,也是二进制写文件
public static synchronized void writeByte(String fileName, String data) {
try {
OutputStream os = new FileOutputStream(fileName);
byte[] bt = data.getBytes("UTF-16BE"); //LZ的结果实际就是这个
//byte[] bt = data.getBytes(); //其实区别就在一个编码问题,写方式还是二进制的
os.write(bt);
os.close();
os.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
//二进制字符串
public static synchronized void writeEncode(String fileName, String data) {
try {
OutputStream os = new FileOutputStream(fileName);
char[] ch = data.toCharArray();
StringBuilder sb = new StringBuilder();
String buf = null;
for (int i=0; i<ch.length; i++) {
buf = "00" + Integer.toHexString(ch[i]>>8); //高8位
if (buf.length()>2) buf = buf.substring(buf.length()-2, buf.length());
sb.append(buf);
buf = "00" + Integer.toHexString(ch[i]); //低8位
if (buf.length()>2) buf = buf.substring(buf.length()-2, buf.length());
sb.append(buf);
}
os.write(sb.toString().getBytes());
os.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static String readEncode(String fileName) {
try {
InputStream is = new FileInputStream(fileName);
byte[] bt = new byte[(int)new File(fileName).length()];
is.read(bt);
StringBuilder sb = new StringBuilder();
char ch;
String buf;
for (int i=0; i<=bt.length-4; i+=4) {
buf = new String(bt, i, 2);
ch = (char)(0x00FF & Integer.parseInt(buf, 16));
ch <<= 8; //高8位
buf = new String(bt, i+2, 2);
ch |= (0x00FF & (byte)Integer.parseInt(buf, 16)); //低8位
sb.append(ch);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
是这样的,我手中一个对应文本的格式的二进制文件。我按照要求不能生成该格式的文件,我生成的结果跟下面的不一致,所以我才怀疑自己的方法是否不正确,下面的二进制就是通过二进制编辑器生成的。其中 PRODUCT_CODE=NC100266
SERIAL_NUMBER=100116 为EBCDIC 编码! 每个行有换行符/n。 如果不对,则没有,需求正在确认中。 具体如下: 文本内容: PRODUCT_CODE=NC100266
SERIAL_NUMBER=100116
DISPLAY_PRODUCT_CODE=AP3C33S
CE1_COMPANY_ID=CE1CO
CE1_EMPLOYEE_NUMBER=1234567
CE1_FROM_CHARGE_DATE=20061122
CE1_TO_CHARGE_DATE=20071111
CE1_TERRITORY_CODE1=CE1T00001
CE1_FROM_POSITION_DATE1=20061122
CE1_TO_POSITION_DATE1=20071022
CE1_TERRITORY_CODE2=CE1T00002
CE1_FROM_POSITION_DATE2=20071023
CE1_TO_POSITION_DATE2=20071111
CE2_COMPANY_ID=CE2CO
CE2_EMPLOYEE_NUMBER=1234568
CE2_FROM_CHARGE_DATE=20071112
CE2_TO_CHARGE_DATE=20081119
CE2_TERRITORY_CODE1=CE2T00001
CE2_FROM_POSITION_DATE1=20071112
CE2_TO_POSITION_DATE1=20080512
CE2_TERRITORY_CODE2=CE2T00002
CE2_FROM_POSITION_DATE2=20080513
CE2_TO_POSITION_DATE2=20081119
KEY_OPERATOR_NAME=川崎二郎
DEPARTMENT_SECTION_NAME=OS開発部二課
CUSTOMER_GRADE=C
RESPONSE_GRADE=R
ASSIGNMENT=
UPDATE_FLAG=MA
二进制文件文本: 00000000: d5c3 f1f0 f0f2 f6f6 f1f0 f0f1 f1f6 c1d7 ................
00000010: f3c3 f3f3 e240 c3c5 f1c3 d6f1 f2f3 f4f5 .....@..........
00000020: f6f7 f2f0 f0f6 f1f1 f2f2 f2f0 f0f7 f1f1 ................
00000030: f1f1 c3c5 f1e3 f0f0 f0f0 f1f2 f0f0 f6f1 ................
00000040: f1f2 f2f2 f0f0 f7f1 f0f2 f2c3 c5f1 e3f0 ................
00000050: f0f0 f0f2 f2f0 f0f7 f1f0 f2f3 f2f0 f0f7 ................
00000060: f1f1 f1f1 c3c5 f2c3 d6f1 f2f3 f4f5 f6f8 ................
00000070: f2f0 f0f7 f1f1 f1f2 f2f0 f0f8 f1f1 f1f9 ................
00000080: c3c5 f2e3 f0f0 f0f0 f1f2 f0f0 f7f1 f1f1 ................
00000090: f2f2 f0f0 f8f0 f5f1 f2c3 c5f2 e3f0 f0f0 ................
000000a0: f0f2 f2f0 f0f8 f0f5 f1f3 f2f0 f0f8 f1f1 ................
000000b0: f1f9 c0ee baea c6f3 cfba 2020 2020 2020 ..........
000000c0: 2020 a3cf a3d3 b3ab c8af c9f4 c6f3 b2dd ..............
000000d0: 2020 c3d9 4040 d4c1 ..@@..
http://www-1.ibm.com/support/docview.wss?uid=csc1d5ec754de55073354825700400218652在java中,好像String.getBytes("Cp1047"); //Cp1047编码好像跟EBCDIC能对应
http://forum.java.sun.com/thread.jspa?threadID=530813&messageID=2559063不过,测试了一下还是没得到你想要的结果。
在网上查看了一下EBCDIC编码,基本上要做个变换表,然后自己变换public class BCDConvertor {
/**
* ASCII->EBCDIC Convert Table
*/
private static byte[] AToE={
/*0 */ 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
/*16 */ 16, 17, 18, 63, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
/*32 */ 64, 90, 127, 123, 91, 108, 80, 125, 77, 93, 92, 78, 107, 96, 75, 97,
/*48 */ -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, 122, 94, 76, 126, 110, 111,
/*64 */ 124, -63, -62, -61, -60, -59, -58, -57, -56, -55, -47, -46, -45, -44, -43, -42,
/*80 */ -41, -40, -39, -30, -29, -28, -27, -26, -25, -24, -23, 63, 63, 63, 63, 109,
/*96 */ -71, -127, -126, -125, -124, -123, -122, -121, -120, -119, -111, -110, -109, -108, -107, -106,
/*112*/ -105, -104, -103, -94, -93, -92, -91, -90, -89, -88, -87, 63, 79, 63, 63, 7,
/*128*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
/*144*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
/*160*/ 64, 63, 74, 123, 63, 63, 63, 63, 63, 63, 63, 63, 95, 96, 63, 63,
/*176*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
/*192*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
/*208*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
/*224*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
/*240*/ 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63
}; /**
* EBCDIC->ASCII Convert Table
*/
private static byte[] EToA={
/*0 */ 0, 1, 2, 3, 26, 9, 26, 127, 26, 26, 26, 11, 12, 13, 14, 15,
/*16 */ 16, 17, 18, 26, 26, 10, 8, 26, 24, 25, 26, 26, 28, 29, 30, 31,
/*32 */ 26, 26, 28, 26, 26, 10, 23, 27, 26, 26, 26, 26, 26, 5, 6, 7,
/*48 */ 26, 26, 22, 26, 26, 30, 26, 4, 26, 26, 26, 26, 20, 21, 26, 26,
/*64 */ 32, 26, 26, 26, 26, 26, 26, 26, 26, 26, -94, 46, 60, 40, 43, 124,
/*80 */ 38, 26, 26, 26, 26, 26, 26, 26, 26, 26, 33, 36, 42, 41, 59, -84,
/*96 */ 45, 47, 26, 26, 26, 26, 26, 26, 26, 26, 26, 44, 37, 95, 62, 63,
/*112*/ 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 58, 35, 64, 39, 61, 34,
/*128*/ 26, 97, 98, 99, 100, 101, 102, 103, 104, 105, 26, 26, 26, 26, 26, 26,
/*144*/ 26, 106, 107, 108, 109, 110, 111, 112, 113, 114, 26, 26, 26, 26, 26, 26,
/*160*/ 26, 26, 115, 116, 117, 118, 119, 120, 121, 122, 26, 26, 26, 26, 26, 26,
/*176*/ 26, 26, 26, 26, 26, 26, 26, 26, 26, 96, 26, 26, 26, 26, 26, 26,
/*192*/ 26, 65, 66, 67, 68, 69, 70, 71, 72, 73, 26, 26, 26, 26, 26, 26,
/*208*/ 26, 74, 75, 76, 77, 78, 79, 80, 81, 82, 26, 26, 26, 26, 26, 26,
/*224*/ 26, 26, 83, 84, 85, 86, 87, 88, 89, 90, 26, 26, 26, 26, 26, 26,
/*240*/ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 26, 26, 26, 26, 26, 26
}; /**
* ascii to ebcdic
*/
public static String ascii2ebcdic(String ascii){
byte[] asciibyte = ascii.getBytes();
byte[] tobytes = new byte[asciibyte.length];
for(int i=0;i<asciibyte.length;i++) {
tobytes[i] = (byte)(AToE[asciibyte[i] & 0xff] & 0xff);
}
return new String(tobytes);
} /**
* ebcdic to ascii
*/
public static String ebcdic2ascii(String ebcdic){
byte[] ebcdicbyte = ebcdic.getBytes();
byte[] tobytes = new byte[ebcdicbyte.length];
for(int i=0;i<ebcdicbyte.length;i++) {
tobytes[i] = (byte)(EToA[ebcdicbyte[i] & 0xff] & 0xff);
}
return new String(tobytes);
}
public static void main(String[] args){
try {
String data = "PRODUCT_CODE=NC100266\n"
+ "SERIAL_NUMBER=100116";
System.out.println("-----before ascii2ebcdic------");
System.out.println(data);
for (int i=0; i<data.length(); i++) {
System.out.printf("%04x", new Object[] {new Integer(data.charAt(i))});
}
System.out.println();
System.out.println("-----after ascii2ebcdic------");
data = ascii2ebcdic(data);
System.out.println(data);
for (int i=0; i<data.length(); i++) {
System.out.printf("%04x", new Object[] {new Integer(data.charAt(i))});
}
System.out.println();
System.out.println("-----after ebcdic2ascii------");
data = ebcdic2ascii(data);
System.out.println(data);
for (int i=0; i<data.length(); i++) {
System.out.printf("%04x", new Object[] {new Integer(data.charAt(i))});
}
System.out.println();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
根据变换表作了一下测试,还是没得到你的结果。(LZ确信结果没问题?)
我对EBCDIC编码也不是很熟悉,你可以在网上查查资料,或者跟你的客户寻求些支持。
再次感谢阿宝,我会尽快跟客户确认。还希望能继续跟你交流,也希望您能继续关注本贴!
为什么我生成的文件的字节数,那么大能?比如说:
String data = "AP3C33S"; 我生成后二进制文件长度,是 14字节。但我感觉二进制文件没有那么长? 因为上面要求整个二进制文件的长度是216个字节。 被这个搞糊涂了补充:我的字符集是EUC-JP。谢谢!
但是问题是,我目前,根据我的方法。还是没有得到他的结果。我对照了一下http://ruby.gfd-dennou.org/products/ruby-dcl/ruby-dcl-doc/misc1/node6.html 的 EBCDIC コード表 发现它的二进制文件是对的,而且所有的数据项二进制文本文件都是符合EBCDIC(不光是我说的那两项PRODUCT_CODE=NC100266 SERIAL_NUMBER=100116为EBCDIC): 具体对应如下:d5c3 的 d5 是 N , c3 是 C
f1f0 的 f1 是 1 , f0 是 0
f0f2 的 f0 是 0 , f2 是 2
f6f6 的 f6 是 6 , f6 是 6以上是 NC100266 的 EBCDIC 编码! 我生成的二进制文本为:
FF 95 FF 83 E1 6B E0 B1 E5 1D E1 6B E0 B0 E1 71
FF 95 对应的是小写 n FF 83 对应的是小写 c E1 6B 对应的是空白
DEPARTMENT_SECTION_NAME=OS開発部二課 为:EUC-JP 字符集
这个问题,有空我再帮你研究吧。
public static void main(String[] args) {
String fileName = System.getProperty("user.dir") + "/Diff/data.da";
String[] data = {"PRODUCT_CODE=NC100266\n",
"SERIAL_NUMBER=100116\n",
"DISPLAY_PRODUCT_CODE=AP3C33S\n",
"CE1_COMPANY_ID=CE1C\n",
"CE1_EMPLOYEE_NUMBER=1234567\n",
"CE1_FROM_CHARGE_DATE=20061122\n",
"CE1_TO_CHARGE_DATE=20071111\n",
"CE1_TERRITORY_CODE1=CE1T00001\n",
"CE1_FROM_POSITION_DATE1=20061122\n",
"CE1_TO_POSITION_DATE1=20071022\n",
"CE1_TERRITORY_CODE2=CE1T00002\n",
"CE1_FROM_POSITION_DATE2=20071023\n",
"CE1_TO_POSITION_DATE2=20071111\n",
"CE2_COMPANY_ID=CE2CO\n",
"CE2_EMPLOYEE_NUMBER=1234568\n",
"CE2_FROM_CHARGE_DATE=20071112\n",
"CE2_TO_CHARGE_DATE=20081119\n",
"CE2_TERRITORY_CODE1=CE2T00001\n",
"CE2_FROM_POSITION_DATE1=20071112\n",
"CE2_TO_POSITION_DATE1=20080512\n",
"CE2_TERRITORY_CODE2=CE2T00002\n",
"CE2_FROM_POSITION_DATE2=20080513\n",
"CE2_TO_POSITION_DATE2=20081119\n",
"KEY_OPERATOR_NAME=川崎二郎\n",
"DEPARTMENT_SECTION_NAME=OS開発部二課\n",
"CUSTOMER_GRADE=C\n",
"RESPONSE_GRADE=R\n",
"ASSIGNMENT=UPDATE_FLAG=MA"
};
System.out.println("---------write file----------");
int lines = 0;
for (int i=0; i<data.length; i++) {
String[] str = data[i].split("=");
if (str.length > 1) {
if (lines == 0) {
writeBin(fileName, str[1], "Cp930", false); //Cp1047 or Cp930
} else {
writeBin(fileName, str[1], "Cp930", true); //Cp1047 or Cp930
}
lines++;
}
}
System.out.println("---------read file----------");
String fileData = readBin(fileName, "Cp930");
System.out.println("data from file:\n" + fileData);
} public static synchronized void writeBin(String fileName, String data, String charset, boolean append) {
try {
OutputStream os = new FileOutputStream(fileName, append);
//data = new String(data.getBytes(), "EUC-JP");
byte[] bt = data.getBytes(charset); // use charset
for (int i=0; i<bt.length; i++) {
System.out.printf("%02x", new Object[]{new Byte(bt[i])});
}
System.out.println();
os.write(bt);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static String readBin(String fileName, String charset) {
try {
InputStream is = new FileInputStream(fileName);
byte[] bt = new byte[(int)new File(fileName).length()];
is.read(bt);
String buf = new String(bt, charset);
//buf = new String(buf.getBytes("EUC-JP"));
return buf;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
运行结果
---------write file----------
d5c3f1f0f0f2f6f615
f1f0f0f1f1f615
c1d7f3c3f3f3e215
c3c5f1c315
f1f2f3f4f5f6f715
f2f0f0f6f1f1f2f215
f2f0f0f7f1f1f1f115
c3c5f1e3f0f0f0f0f115
f2f0f0f6f1f1f2f215
f2f0f0f7f1f0f2f215
c3c5f1e3f0f0f0f0f215
f2f0f0f7f1f0f2f315
f2f0f0f7f1f1f1f115
c3c5f2c3d615
f1f2f3f4f5f6f815
f2f0f0f7f1f1f1f215
f2f0f0f8f1f1f1f915
c3c5f2e3f0f0f0f0f115
f2f0f0f7f1f1f1f215
f2f0f0f8f0f5f1f215
c3c5f2e3f0f0f0f0f215
f2f0f0f8f0f5f1f315
f2f0f0f8f1f1f1f915
0e4567457f4542456e0f15
0e42d642e246cd464b4595454249d60f15
c315
d915
e4d7c4c1e3c56dc6d3c1c7
---------read file----------
data from file:
NC100266
100116
AP3C33S
CE1C
1234567
20061122
20071111
CE1T00001
20061122
20071022
CE1T00002
20071023
20071111
CE2CO
1234568
20071112
20081119
CE2T00001
20071112
20080512
CE2T00002
20080513
20081119
川崎二郎
OS開発部二課
C
R
UPDATE_FLAG
以下两处回复一下!http://topic.csdn.net/u/20071130/15/76349259-3fee-4a24-bd7c-a82e9e35aca5.htmlhttp://topic.csdn.net/u/20071130/14/5b450a87-93d4-42e2-9526-83e3a5eddf80.html