dav*_*278 5 java character-encoding
我正在使用Java Scanner.
我有一个.txt文件,其中保存了此文本.
PriceDB = {
["profileKeys"] = {
["Name - ???"] = "Name - ???",
},
["char"] = {
["Name - ???"] = {
["CurrentValue"] = "????|cffffffff70,197|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t|r",
},
},
}
Run Code Online (Sandbox Code Playgroud)
所有我想要做的就是打开这个文件,扫描仪和提取"CurrentValue"的70,197从该文件并将其保存为一个int.然而,每次打开文件时也不会读取一行,并抛出一个NoSuchElementException与"No line found"作为消息.在摆弄文件并逐一删除一些汉字后,我把它缩小到这个小家伙口.出于某种原因,扫描仪不喜欢这个角色.我只是想知道是否有一些我需要更改的编码设置,或者我是否会使用BufferedReader或者什么......我真的不确定发生了什么,除了我猜有编码错误.那么这里发生了什么?
编辑:这是我的扫描仪的初始化.
Scanner scanner;
if (region.equals("US")) {
scanner = new Scanner(new File("C:\\Program Files\\World of Warcraft\\WTF\\Account\\313023286#1\\SavedVariables\\WoWTokenPrice.lua"));
} else if (region.equals("EU")) {
scanner = new Scanner(new File("C:\\Program Files\\World of Warcraft\\WTF\\Account\\313495228#1\\SavedVariables\\WoWTokenPrice.lua"));
} else if (region.equals("China")) {
File file = new File("C:\\Program Files\\World of Warcraft\\WTF\\Account\\232241227#1\\SavedVariables\\WoWTokenPrice.lua");
System.out.println(file.exists());
scanner = new Scanner(file);
} else {
System.exit(1);
break;
}
Run Code Online (Sandbox Code Playgroud)
我只是原样复制它.region =="中国"
您必须在创建扫描仪时指定正确的编码.构造函数:
public Scanner(InputStream source, String charsetName)
Run Code Online (Sandbox Code Playgroud)
构造一个新的Scanner,它生成从指定输入流扫描的值.使用指定的字符集将流中的字节转换为字符.
在这里找到你的字符集,我想UTF-16但不是外国人的专家:).
Scanner scanner = new Scanner(is, StandardCharsets.UTF-16.toString());
Run Code Online (Sandbox Code Playgroud)