简体   繁体   中英

Appending Chars to a file

I have the following String[] :

63,64,65,66,67,68,69,6A,73,74,75,76,77,78,79,7A,
83,84,85,86,87,88,89,8A,92,93,94,95,96,97,98,99

Each element is the HEX ASCII code of a character:

63 -> 'c'
64 -> 'd'
etc..

So here the relevant part of my code, it's supposed to write the corresponding ASCII characters in a file:

private static HashMap<String, Integer> HEXMAP;
static {
    HEXMAP = new HashMap<String, Integer>();
    HEXMAP.put("0", 0);
    HEXMAP.put("1", 1);
    HEXMAP.put("2", 2);
    HEXMAP.put("3", 3);
    HEXMAP.put("4", 4);
    HEXMAP.put("5", 5);
    HEXMAP.put("6", 6);
    HEXMAP.put("7", 7);
    HEXMAP.put("8", 8);
    HEXMAP.put("9", 9);
    HEXMAP.put("A", 10);
    HEXMAP.put("B", 11);
    HEXMAP.put("C", 12);
    HEXMAP.put("D", 13);
    HEXMAP.put("E", 14);
    HEXMAP.put("F", 15);
}


public static void main(String[] args) {
    try {
        PrintWriter writer = new PrintWriter("resultFile");
        for (String str : myString) {
            append(str, writer);
        }
        writer.close();
    }
    catch(Exception e) {

    }
}

private static int strToHex(String str) {
    return HEXMAP.get(str.substring(0, 1)) * 16 + HEXMAP.get(str.substring(1, 2));
}

private static void append(String hex, PrintWriter writer) {
    writer.print((char) strToHex(hex));
}

The problem is that, instead of having this:

在此处输入图片说明

I have the following in my result file:

在此处输入图片说明

(the screen shots above are from a hex editor)

A java.io.Writer performs a translation from Java 'char' to a particular character encoding in the file. When this encoding is not explicitly specified, it will be the default encoding for your computer (depends on your operating system's "Countries & Languages" settings)

In your case, you already know which bytes you want to write to the file, so you should not use a Writer byte you should use an java.io.OutputStream . The subclass of OutputStream that writes to a file is java.io.FileOutputStream .

Replace Writer with FileOutputStream in your example, and the call to writer.print( with .write( and it should work.

Alternatively, if you know the encoding that your data is in, you can add that encoding as the second argument to the new PrintWriter constructor invocation. It looks like you may be using ISO-8859-1 encoding, so you can also say new PrintWriter("resultFile", "ISO-8859-1") . But you should only do that if you really intend to write characters in that encoding, not because it just happens to work - there is a clear difference between a Writer and an OutputStream.

You need to debug it with a debugger like in Eclipse. I imagine the problem is with your parsing of the input into the myString array. As a result, you get an unexpected value in it after 7A, then your append function throws, and since you have an empty catch block the exception is swallowed. You could start by adding e.printStackTrace() into the catch block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM