简体   繁体   English

Java没有正确显示重音词

[英]Java doesn't display accent words correctly

I have a java program that saves a text file and then outputs it contents in a dialog. 我有一个java程序,它保存一个文本文件,然后在对话框中输出它的内容。 When I run the program inside my IDE (BlueJ) the display is as follows: 当我 IDE(BlueJ)中运行程序时,显示如下: BlueJ内部的程序在此输入图像描述

As you can see in the dialog the line "1º) Mónica" appears correctly. 正如您在对话框中看到的那样,“1º)Mónica”行正确显示。
But when I run the same program outside the IDE the "Mónica" doesn't appear right, as you can see in the picture: 但是,当我在IDE 外部运行相同的程序时,“Mónica”似乎不正确,如图所示:
BlueJ以外的计划

How can I fix this to always display the right output? 如何解决此问题始终显示正确的输出?

this is the code that reads the text file to a string 这是将文本文件读取为字符串的代码

public String recordesString()
    {
        Premios premios = new Premios();
        File recordes = new File("recordes.txt");
        if(!recordes.exists()) {
            if(!client.isConnected())
                openFTPSession();
            downloadRecordes(); // this downloads the recordes.txt file
        }
        Scanner s = null;
        try {
            s = new Scanner(recordes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String string = "";
        String linha = null;
        for(int i = 1; s.hasNext(); i++) {
            linha = s.nextLine();
            String palavra[] = linha.split(" ",2);
            string += i+"º) "+palavra[1] +"  "+ premios.getPremio(Integer.parseInt(palavra[0]))+"\n";
        }
        s.close();
        try {
            Files.deleteIfExists(Paths.get(ficRecordes));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return string;
    }

Scanner reads the file using the underlying platform's default charset. 扫描程序使用底层平台的默认字符集读取文件。

See http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File) 请参阅http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)

I'd expect the file is encoded in UTF-8 and when you run outside the IDE, the default charset is ISO-latin-1 or so. 我希望该文件以UTF-8编码,当您在IDE外部运行时,默认字符集是ISO-latin-1左右。 If you specify the file's encoding when creating the scanner, the results will be predictable. 如果在创建扫描程序时指定文件的编码,则结果将是可预测的。

s = new Scanner(recordes, "UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM