【问题标题】:Converting Unicode to text in Java将 Unicode 转换为 Java 中的文本
【发布时间】:2018-04-11 01:21:41
【问题描述】:

我这里有一个包含数字的 unicode.txt 文件(.txt 文件的第一行“82 117 109 112 101 108 115 116 105 108 116 115 107 105 110”)。我想将此 Unicode 转换为普通文本。问题是,不是这样做,而是将 unicode.txt 文件复制到 text.txt 文件中。我是java新手,所以希望有人可以帮助我,如果我的代码的其他部分不正确或者我的解释无法理解,我提前道歉。

public static void translator(String a) throws IOException{
    a = "unicode.txt";
    Scanner inputFile = new Scanner(new File(a));
    PrintWriter outputFile = new PrintWriter("text.txt");
    while(inputFile.hasNext()){
        String fileL = inputFile.nextLine();
        for(int h = 0; h < fileL.length(); h++){
            char y = fileL.charAt(h);
            outputFile.print(y);
        }
        outputFile.println();
    }
    outputFile.close();
}

【问题讨论】:

  • 你的意思是 int char 到 ascii 吗? char ch = (char) yourIntString str = String.valueOf(yourInt);
  • 互联网上有很多文章可以为您提供有关该主题的一些深入信息。试试tutorials.jenkov.com/java-internationalization/unicode.html
  • 我认为这与 Unicode 没有太大关系
  • 另外,charAt 不是您想要的,如果您想使用每个单独的号码。比如82 而不是8 然后是2

标签: java unicode


【解决方案1】:

您当前的方法只是一位一位地读取输入文件的字符,然后将其写入输出文件而不进行任何修改。

如果要读取整数而不是数字,则需要使用nextInt()

while (inputFile.hasNextInt()) {
    int asciiCode = inputFile.nextInt();
    ...
}

要将 asciiCode 转换为 char,您可以使用

char c = (char) asciiCode;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多