在文本中使用Scanner和字母”Ł”的问题 – JAVA

huangapple 未分类评论58阅读模式
英文:

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,不支持波兰字符。

要更改在记事本中创建的文件的编码:

  1. 在记事本中打开你的文件
  2. 在标签 文件 下,点击 另存为
  3. 点击左下角 保存 按钮左侧的下拉列表
  4. 在下拉列表中选择 UTF-8 作为编码器
  5. 保存你的文件
英文:

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:

  1. Open your file in Notepad
  2. Under tab File press save as
  3. Press the drop down list just left of the Save button in the lower left corner
  4. Choose UTF-8 as your encoder in the drop down list
  5. Save your file

huangapple
  • 本文由 发表于 2020年4月8日 19:30:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/61099631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定