I am assuming that you want to take the Unicode representation that is on each line of the file and output the actual Unicode character which the code represents.
If we start with your loop that reads each line from the file...
while ((line = br.readLine()) != null){
System.out.println( line );
}
... then what we want to do is convert the input line
to the character, and print that ...
while ((line = br.readLine()) != null){
System.out.println( convert(line) ); <- I just put a method call to "convert()"
}
So, how do you convert(line)
into a character before printing it?
As my earlier comment suggested, you want to take the numeric string that follows the U+
and convert it to an actual numeric value. That, then, is the character value you want to print.
The following is a complete program — essentially like yours but I take the filename as an argument rather than hard-coding it. I've also added skipping blank lines, and rejecting invalid strings -- printing a blank space instead.
Reject the line if it does not match the U+nnnn
form of a Unicode representation — match against "(?i)U\\+[0-9A-F]{4}"
, which means:
(?i)
- ignore case
U\\+
- match U+
, where the +
has to be escaped to be a literal plus
[0-9A-F]
- match any character 0-9 or A-F (ignoring case)
{4}
- exactly 4 times
With your update that includes a linked sample file, which includes #
comments, I have modified my original program (below) so it will now strip comments and then convert the remaining representation.
This is a complete program that can be run as:
javac Reader2.java
java Reader2 inputfile.txt
I tested it with a subset of your file, starting inputfile.txt at line 1 with U+0000
and ending at line 312 with U+0138
import java.io.*;
public class Reader2
{
public static void main(String... args)
{
final String filename = args[0];
try (BufferedReader br = new BufferedReader(
new FileReader(new File( filename ))
)
)
{
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() > 0) { // skip blank lines
//System.out.println( convert(line) );
final Character c = convert(line);
if (Character.isValidCodePoint(c)) {
System.out.print ( c );
}
}
}
System.out.println();
}
catch(Exception e) {
e.printStackTrace();
}
}
private static char convert(final String input)
{
//System.out.println("Working on line: " + input);
if (! input.matches("(?i)U\\+[0-9A-F]{4}(\\s+#.*)")) {
System.err.println("Rejecting line: " + input);
return ' ';
}
else {
//System.out.println("Accepting line: " + input);
}
// else
final String stripped = input.replaceFirst("\\s+#.*$", "");
final Integer cval = Integer.parseInt(stripped.substring(2), 16);
//System.out.println("cval = " + cval);
return (char) cval.intValue();
}
}
Original program that assumed a line consisted only of U+nnnn
is here.
You would run this as:
javac Reader.java
java Reader input.txt
import java.io.*;
public class Reader
{
public static void main(String... args)
{
final String filename = args[0];
try (BufferedReader br = new BufferedReader(
new FileReader(new File( filename ))
)
)
{
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() > 0) { // skip blank lines
//System.out.println( line );
// Write all chars on one line rather than one char per line
System.out.print ( convert(line) );
}
}
System.out.println(); // Print a newline after all chars are printed
}
catch(Exception e) { // don't catch plain `Exception` IRL
e.printStackTrace(); // don't just print a stack trace IRL
}
}
private static char convert(final String input)
{
// Reject any line that doesn't match U+nnnn
if (! input.matches("(?i)U\\+[0-9A-F]{4}")) {
System.err.println("Rejecting line: " + input);
return ' ';
}
// else convert the line to the character
final Integer cval = Integer.parseInt(input.substring(2), 16);
//System.out.println("cval = " + cval);
return (char) cval.intValue();
}
}
Try it using this as your input file:
U+0041
bad line
U+2718
U+00E9
u+0073
Redirect standard error when you run it java Reader input.txt 2> /dev/null
or comment out the line System.err.println...
You should get this output: A ✘és