Je dispose d'un fichier plat qui est tiré à partir d'une table DB2, le fichier plat contient des enregistrements dans le format Char ainsi que le format décimal emballé.Comment convertir les données emballées en une chaîne Java.Il y a-t-il un moyen de Convertissez le fichier plat entier en format ASCII. P>
4 Réponses :
Lire le fichier sous forme de chaîne, écrivez-le comme EBCDIC. Utilisez la sortie de sortie et INPUTREAMWriter et donnez le codage dans le constructeur. P>
EBCDIC est une famille de codages. Vous aurez besoin de savoir plus dans les détails que l'encodage EbcDic vous après.
Java a un certain nombre de Encodages pris en charge , y compris: p>
Essayez ceux et voyez ce que vous obtenez. Quelque chose comme: p>
Il convient de noter que cette liste est des codages pris en charge par Oracle JDK i>. La liste des codages requis pour être pris en charge par Tous B> JVMS est beaucoup plus court .
Suivant de PAP, CP037 est le codage EbcDic américain. P>
Regardez également projet Jrecord . Il vous permet de lire un fichier avec une description COBOL ou XML et gérera EBCDIC et Comp-3. P>
Enfin ici est une routine pour convertir des octets décimaux emballés en chaîne Voir la méthode
Partage d'un exemple de code par moi pour votre référence:
package mypackage; import java.io.UnsupportedEncodingException; import java.math.BigInteger; public class EtoA { public static void main(String[] args) throws UnsupportedEncodingException { System.out.println("########"); String edata = "/ÃÃÃ"; //Some EBCDIC string ==> here the OP can provide the content of flat file which the OP pulled from DB2 table System.out.println("ebcdic source to ascii:"); System.out.println("ebcdic: " + edata); String ebcdic_encoding = "IBM-1047"; //Setting the encoding in which the source was encoded byte[] result = edata.getBytes(ebcdic_encoding); //Getting the raw bytes of the EBCDIC string by mentioning its encoding String output = asHex(result); //Converting the raw bytes into hexadecimal format byte[] b = new BigInteger(output, 16).toByteArray(); //Now its easy to convert it into another byte array (mentioning that this is of base16 since it is hexadecimal) String ascii = new String(b, "ISO-8859-1"); //Now convert the modified byte array to normal ASCII string using its encoding "ISO-8859-1" System.out.println("ascii: " + ascii); //This is the ASCII string which we can use universally in JAVA or wherever //Inter conversions of similar type (ASCII to EBCDIC) are given below: System.out.println("########"); String adata = "abcd"; System.out.println("ascii source to ebcdic:"); System.out.println("ascii: " + adata); String ascii_encoding = "ISO-8859-1"; byte[] res = adata.getBytes(ascii_encoding); String out = asHex(res); byte[] bytebuff = new BigInteger(out, 16).toByteArray(); String ebcdic = new String(bytebuff, "IBM-1047"); System.out.println("ebcdic: " + ebcdic); //Converting from hexadecimal string to EBCDIC if needed System.out.println("########"); System.out.println("hexcode to ebcdic"); String hexinput = "81828384"; //Hexadecimal which we are converting to EBCDIC System.out.println("hexinput: " + hexinput); byte[] buffer = new BigInteger(hexinput, 16).toByteArray(); String eout = new String(buffer, "IBM-1047"); System.out.println("ebcdic out:" + eout); //Converting from hexadecimal string to ASCII if needed System.out.println("########"); System.out.println("hexcode to ascii"); String hexin = "61626364"; System.out.println("hexin: " + hexin); byte[] buff = new BigInteger(hexin, 16).toByteArray(); String asciiout = new String(buff, "ISO-8859-1"); System.out.println("ascii out:" + asciiout); } //This asHex method converts the given byte array to a String of Hexadecimal equivalent public static String asHex(byte[] buf) { char[] HEX_CHARS = "0123456789abcdef".toCharArray(); char[] chars = new char[2 * buf.length]; for (int i = 0; i < buf.length; ++i) { chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4]; chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F]; } return new String(chars); } }
Je pense que ce serait plus utile pour les visiteurs op et autres, lorsque vous ajoutez une explication à votre intention.