Escape Unicode Characters In Java Strings

I was looking for a code snippet that does that for me but couldn’t find one, especially since native2ascii seems to skip a few characters I need to show. If you’re in a similar situation, feel free to use this:

package at.soher.unicode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.FieldPosition;

public class StringEscape {

    public static void main(String[] args) {

        File file = new File("~/file.txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-16"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split("[\t]");
                if (fields.length > 1) {
                    StringBuilder newLine = new StringBuilder();
                    newLine.append(fields[0] + " = ");

                    String input = fields[2];
                    StringBuilder dest = new StringBuilder();
                    for (int i = 0; i < input.length(); i++) {
                        if (input.charAt(i) > 127) {
                            dest.append("\\u" + asHex((long) input.charAt(i)));
                        } else {
                            dest.append(input.charAt(i));
                        }
                    }
                    newLine.append(dest);
                    System.out.println(newLine.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String asHex(long number) {
        String hexString = "0000" + Long.toHexString(number).toUpperCase();
        return hexString.substring(hexString.length() - 4);

    }

}
No votes yet.
Please wait...

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.