英文:
Problem with Scanner and a letter "Ł" in text - JAVA
问题
我遇到了Scanner的问题。在doc.txt中有一个字母“Ł”(来自波兰字母表)。Scanner跳过了包含这个字母的部分,只扫描前面的部分。可能有解决办法,但我是编程新手...你能帮忙吗?以下是代码片段。
File file = new File("doc.txt");
String tempTest = "";
Scanner sc = new Scanner(file);
while(sc.hasNext()) {
tempTest = tempTest.concat(sc.nextLine() + "\n");
}
sc.close();
英文:
I have a problem with Scanner. In doc.txt there is a letter "Ł" (its from polish alphabet). Scanner skips the part with this letter and scans only the part before. There is probably a solution, but I'm a newby in coding… Can you help? Fragment of the code is below.
File file = new File("doc.txt");
String tempTest = "";
Scanner sc = new Scanner(file);
while(sc.hasNext()) {
tempTest = tempTest.concat(sc.nextLine() + "\n");
}
sc.close();
答案1
得分: 0
确保你的文件以 UTF-8
编码。如果你使用记事本创建文件,它的默认编码器是 ANSI,不支持波兰字符。
要更改在记事本中创建的文件的编码:
- 在记事本中打开你的文件
- 在标签
文件
下,点击另存为
- 点击左下角
保存
按钮左侧的下拉列表 - 在下拉列表中选择
UTF-8
作为编码器 - 保存你的文件
英文:
Make sure that your file is encoding in UTF-8
. If you use for example Notepad to create your file the default encoder is ANSI which do not support Polish letters.
To change the encoding of a file created in Notepad:
- Open your file in Notepad
- Under tab
File
presssave as
- Press the drop down list just left of the
Save
button in the lower left corner - Choose
UTF-8
as your encoder in the drop down list - Save your file
专注分享java语言的经验与见解,让所有开发者获益!
评论