当从导出的 .jar 中 运行 时扫描仪停止读取 运行domly 但当从 Eclipse 中 运行 时则不会
Scanner stops reading randomly when ran from exported .jar but not when ran from Eclipse
所以今天我 运行 在使用 java 扫描仪时遇到了一些麻烦。我在我的项目中多次使用扫描仪 class,但我从未 运行 遇到任何问题。
基本上,我总是这样做:
try (Scanner scanner = new Scanner(file)) {
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
...
}
} catch ...
} finally ...
并且扫描器工作正常,因为它只是一些简单的代码。然而,今天,我使用上面的代码读取了大约 17000 行的文本文件。
起初代码工作正常(当 运行 它通过 Eclipse 时)正如我预期的那样,但是在导出项目后,扫描仪将在大约 400 行后停止读取。
我用谷歌搜索了一下,最后我解决了这个问题,多亏了这些答案:
- Answer #1
- Answer #2
我所要做的就是将构造函数从
Scanner scanner = new Scanner(file)
到
Scanner scanner = new Scanner(new FileInputStream(sql)))
这是一些奇怪的编码问题,我明白了。但是,为什么当我 运行 来自 Eclipse 的代码时,它运行得非常完美,而当我 运行 来自我导出的 jar 时,扫描仪在读取大约 400 行后停止了?
代码在这两种情况下做的事情完全相同,因为我设置了 Eclipse,以便它使用与导出的 .jar 存档相同的工作目录(因为它有一些数据子目录):
- 采用相同的 .gz 存档
- 从 .gz 中提取文件
- 像上面显示的那样阅读文件
不确定是否有帮助,但 Eclipse 已设置为以 UTF-8 格式保存源文件。
提前致谢
But why when I ran the code from Eclipse it worked flawlessy and when I ran it from my exported jar the Scanner stopped after reading about 400 lines?
Scanner
有两个不同的构造函数接受 File
作为参数。来自 docs:
Scanner(File source)
Bytes from the file are converted into characters using the underlying platform's default charset.
和
Scanner(File source, String charsetName)
Bytes from the file are converted into characters using the specified charset.
因此,如果您不指定 charsetName
,它将使用环境的默认字符集。
当您 运行 您的项目在 Eclipse 之外时的环境编码可能不是 UTF-8。要检查是否是这种情况,您可以编写一个像这样的简单程序:
class CheckDefaultCharset {
public static void main(String... args) {
System.out.println(Charset.defaultCharset());
}
}
并且 运行 它在两种环境中都适用。
例如,当 运行从 Eclipse 中调用上述代码时,我得到:
UTF-8
当在 PowerShell (Windows 7) 中 运行ning 时,我得到:
windows-1252
为避免此类问题,最好在使用 Scanner
.
时始终指定要使用的文件的编码
所以今天我 运行 在使用 java 扫描仪时遇到了一些麻烦。我在我的项目中多次使用扫描仪 class,但我从未 运行 遇到任何问题。
基本上,我总是这样做:
try (Scanner scanner = new Scanner(file)) {
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
...
}
} catch ...
} finally ...
并且扫描器工作正常,因为它只是一些简单的代码。然而,今天,我使用上面的代码读取了大约 17000 行的文本文件。
起初代码工作正常(当 运行 它通过 Eclipse 时)正如我预期的那样,但是在导出项目后,扫描仪将在大约 400 行后停止读取。
我用谷歌搜索了一下,最后我解决了这个问题,多亏了这些答案:
- Answer #1
- Answer #2
我所要做的就是将构造函数从
Scanner scanner = new Scanner(file)
到
Scanner scanner = new Scanner(new FileInputStream(sql)))
这是一些奇怪的编码问题,我明白了。但是,为什么当我 运行 来自 Eclipse 的代码时,它运行得非常完美,而当我 运行 来自我导出的 jar 时,扫描仪在读取大约 400 行后停止了?
代码在这两种情况下做的事情完全相同,因为我设置了 Eclipse,以便它使用与导出的 .jar 存档相同的工作目录(因为它有一些数据子目录):
- 采用相同的 .gz 存档
- 从 .gz 中提取文件
- 像上面显示的那样阅读文件
不确定是否有帮助,但 Eclipse 已设置为以 UTF-8 格式保存源文件。
提前致谢
But why when I ran the code from Eclipse it worked flawlessy and when I ran it from my exported jar the Scanner stopped after reading about 400 lines?
Scanner
有两个不同的构造函数接受 File
作为参数。来自 docs:
Scanner(File source)
Bytes from the file are converted into characters using the underlying platform's default charset.
和
Scanner(File source, String charsetName)
Bytes from the file are converted into characters using the specified charset.
因此,如果您不指定 charsetName
,它将使用环境的默认字符集。
当您 运行 您的项目在 Eclipse 之外时的环境编码可能不是 UTF-8。要检查是否是这种情况,您可以编写一个像这样的简单程序:
class CheckDefaultCharset {
public static void main(String... args) {
System.out.println(Charset.defaultCharset());
}
}
并且 运行 它在两种环境中都适用。
例如,当 运行从 Eclipse 中调用上述代码时,我得到:
UTF-8
当在 PowerShell (Windows 7) 中 运行ning 时,我得到:
windows-1252
为避免此类问题,最好在使用 Scanner
.