提问人:Simeon Leyzerzon 提问时间:10/10/2023 最后编辑:Simeon Leyzerzon 更新时间:10/10/2023 访问量:73
java.io.UncheckedIOException / java.nio.file.AccessDeniedException 在读取目录中的文件时提及不同的字符集
java.io.UncheckedIOException / java.nio.file.AccessDeniedException mention different charsets when reading files in a directory
问:
在IDE或终端中运行测试时,我得到了相同的不同变体:java.io.UncheckedIOException
在 IDE 中:
java.io.UncheckedIOException: Failed to read C:\code\STS\daily-cash-report\target\test-classes\zipTmp\October 3 Files content with UTF-8 charset
at org.assertj.core.api.AbstractFileAssert.readFile(AbstractFileAssert.java:1436)
at org.assertj.core.api.AbstractFileAssert.internalContent(AbstractFileAssert.java:1428)
at org.assertj.core.api.AbstractFileAssert.content(AbstractFileAssert.java:1350)
at com.pru.globalpayments.feeds.downstream.dailycashreport.AchFileUtilsTest.lambda$0(AchFileUtilsTest.java:313)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at com.abc.globalpayments.feeds.downstream.dailycashreport.AchFileUtilsTest.processZippedFiles(AchFileUtilsTest.java:311)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.nio.file.AccessDeniedException: C:\code\STS\daily-cash-report\target\test-classes\zipTmp\October 3 Files
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
在 Maven 中:
[ERROR] Errors:
[ERROR] AchFileUtilsTest.processZippedFiles:311->lambda$processZippedFiles$0:313 � UncheckedIO Failed to read C:\code\STS\daily-ca
sh-report\target\test-classes\zipTmp\October 3 Files content with windows-1252 charset
[INFO]
从上面可以看出,同样的异常指的是 IDE 中的 UTF-8 字符集和 Maven 中的 windows-1252 字符集。
这是罪魁祸首测试:
@Test
@SneakyThrows
public void processZippedFiles() {
String zipFileLocation = "/october3/October 3 Files.zip";
File zipFileToUnmarshall = new ClassPathResource(zipFileLocation).getFile();
File tmpContainer = new File(new ClassPathResource(".").getFile().getPath() + "/zipTmp");
if (FileUtils.unzip(zipFileToUnmarshall, tmpContainer)) {
Files.list(tmpContainer.toPath()).forEach(p -> {
File originalFile = p.toFile();
assertThat(originalFile).content().contains(ASCII_SUB_CHAR);
String normalizedOriginalFileName = com.pru.globalpayments.feeds.downstream.dailycashreport.FileUtils
.normalize(originalFile);
String fixedFileName = normalizedOriginalFileName + FXD;
NormalizedResult normalized = null;
try {
// both content- and name-wise
normalized = cut.normalize(originalFile, fixedFileName);
} catch (IOException e) {
e.printStackTrace();
}
File fullyNormalizedOriginalFileToUnmarshal = normalized.getFile();
int validLines = normalized.getNumberOfValidLines();
assertThat(fullyNormalizedOriginalFileToUnmarshal).content().doesNotContain(ASCII_SUB_CHAR);
String maskedFileName = fullyNormalizedOriginalFileToUnmarshal.getAbsolutePath() + MSK;
File maskedFile = null;
try {
maskedFile = cut.maskSensitiveData(fullyNormalizedOriginalFileToUnmarshal, maskedFileName);
} catch (IOException e) {
e.printStackTrace();
}
assertThat(maskedFile).isNotEmpty();
long maskedNumberOfLines = 0L;
try {
maskedNumberOfLines = countLines(maskedFile);
} catch (IOException e) {
e.printStackTrace();
}
assertThat(maskedNumberOfLines).isEqualTo(validLines);
});
// FileSystemUtils.deleteRecursively(tmpContainer);
}
}
以下是使用上述测试处理的文件填充目录的方式:
/**
* Utility to unzip contents of a supplied zip file into the supplied destination folder
*
* @param zipToUnzip
* @param destinationFolder
* @throws Exception
*/
public static boolean unzip(File zipToUnzip, File destinationFolder) throws Exception {
boolean result;
FileSystemUtils.deleteRecursively(destinationFolder);
try (ZipFile zipFile = new ZipFile(zipToUnzip);) {
Enumeration<? extends ZipEntry> enumEntries = zipFile.entries();
while (enumEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enumEntries.nextElement();
File destFile = new File(destinationFolder, entry.getName());
if (!destFile.exists()) {
destFile.getParentFile().mkdirs();
}
if (entry.isDirectory()) {
continue;
}
try (InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(destFile)) {
while (is.available() > 0) {
fos.write(is.read());
}
}
}
}
result = true;
return result;
}
这里可能缺少什么?
为什么字符集的使用因执行环境而异?
第一个堆栈跟踪中的节也会导致以下情况:
Caused by: java.nio.file.AccessDeniedException: C:\code\STS\daily-cash-report\target\test-classes\zipTmp\October 3 Files
这会是罪魁祸首吗?
答: 暂无答案
评论
FileUtils.unzip(zipFileToUnmarshall, tmpContainer)
tmpContainer
Files.list(tmpContainer.toPath()).forEash(p->{System.out.println(p);});