How can I print symbols from a String to a txt file?

血红的双手。 提交于 2020-03-05 03:59:11

问题


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.



来源:https://stackoverflow.com/questions/49886402/how-can-i-print-symbols-from-a-string-to-a-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!