gpt4 book ai didi

Java readLine() 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 08:26:13 25 4
gpt4 key购买 nike

我正在编写一个从文本文件中读取翻译的翻译程序。此方法从文本文件读取并将其添加到我用于字典的 Map 对象。

public Map<String, String> loadTranslations() throws IOException {
Map<String, String> translations = new HashMap<String, String>();
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(System.getProperty("user.dir") + "\\resource\\translations.txt"));
englWord = in.readLine();
frenWord = in.readLine();
translations.put(englWord, frenWord);
System.out.println(englWord + "\n" + frenWord + "\n" + translations);
} catch(Exception e) {
JOptionPane.showMessageDialog(this, "An Error Occured!", "Error!", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} finally {
in.close();
}
JOptionPane.showMessageDialog(this, "Updated!", "", JOptionPane.INFORMATION_MESSAGE);
return translations;
}

但是每当我运行它时,它不会打印文本文件中的单词,而是打印这个;

潮攍ੵ渍ੴ睯ഊ摥畸ഊ瑨牥攍ੴ牯楳ഊ景畲ഊ煵慴牥ഊ晩癥ഊ捩湱ഊ獩砍ੳ楸ഊ獥癥渍ੳ数琍੥楧桴ഊ桵楴ഊ湩湥ഊ湥畦ഊ瑥渍੤楸
null
{潮攍ੵ渍ੴ睯ഊ摥畸ഊ瑨牥攍ੴ牯楳ഊ景畲ഊ煵慴牥ഊ晩癥ഊ捩湱ഊ獩砍ੳ楸ഊ獥癥渍ੳ数琍੥楧桴ഊ桵楴ഊ湩湥ഊ湥畦ഊ瑥渍੤楸=null}

文本文件只列出了英语和法语的前十个字母,所以输出应该是这样的;

one
un
{one=un}

我该如何解决这个问题?

最佳答案

如果您不指定编码,您将获得一个默认值 depends on how the JVM gets initialized .您可以像这样指定显式编码:

    FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader in = new BufferedReader(isr);

这段代码读取一个文件,假设内容是用 UTF8 编码的。

参见 Oracle tutorial on Character and Byte Streams .

关于Java readLine() 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21735328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com