How can I print symbols from a String to a txt fil

2019-08-23 04:07发布

问题:

This is what I have tryed to do:

 String raw = dstream.readLine();
                  raw = raw.replaceAll("Up"," ↑ ");
                  raw = raw.replaceAll("Right"," → ");
                System.out.println(raw);

This is part of an easy program that receives Strings and prints them to a text file: "java -jar Server.jar > a.txt".

The problem is this:
Insted of ↑ or → , I get those in the txt file: "↕ ↕ ↕ ↑"

回答1:

cat Upright.java 
public class Upright
{
    public static void main (String args[])
    {
        String raw ="Left - Down - Up - Right - East - West";
        raw = raw.replaceAll("Up"," ↑ ");
        raw = raw.replaceAll("Right"," → ");
        System.out.println(raw);
    }
}
java Upright > upright.txt && cat upright.txt 
Left - Down -  ↑  -  →  - East - West

Works like a charm. The character encoding of the tool, you use to produce the source, has to match the tool for looking at the output file.