Java中如何避免FileSystemNotFoundException异常?附代码求助
Hey there! Let's break down why you're hitting that FileSystemNotFoundException when running your file copy code. Given your class name ZipIn and the NIO FileSystem imports, it's almost certainly tied to how you're handling ZIP/JAR files or cross-filesystem copies—here are the most common fixes to get you back on track:
Common Causes & Practical Solutions
1. You're accessing a ZIP/JAR file without loading its dedicated FileSystem
ZIP/JAR archives are treated as separate file systems in Java NIO. You can't directly use Paths.get() to pull files out of them—you need to explicitly create a FileSystem instance for the ZIP first.
Fixed Code Example:
package com.mod2.zipIn; import java.io.IOException; import java.nio.file.*; import java.util.HashMap; import java.util.Map; public class ZipIn { public static void main(String[] args) { // Define your paths Path zipFilePath = Paths.get("path/to/your/archive.zip"); String internalZipFilePath = "data/your-source-file.txt"; Path localTargetPath = Paths.get("local/output/your-target-file.txt"); // Configure ZIP filesystem environment Map<String, String> env = new HashMap<>(); env.put("create", "false"); // Set to "true" if you need to create a new ZIP // Use try-with-resources to auto-close the filesystem try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFilePath, env)) { Path sourceFileInZip = zipFileSystem.getPath(internalZipFilePath); // Make sure target directory exists Files.createDirectories(localTargetPath.getParent()); // Perform the copy Files.copy(sourceFileInZip, localTargetPath, StandardCopyOption.REPLACE_EXISTING); System.out.println("File copied successfully!"); } catch (IOException e) { e.printStackTrace(); } } }
2. Your source or target path doesn't exist
Even outside of ZIPs, this exception can pop up if the source file is missing or the target directory hasn't been created. Always validate paths before attempting to copy:
Add These Checks Before Copying:
Path source = ...; // Your source path Path target = ...; // Your target path // Verify source exists if (!Files.exists(source)) { System.err.println("Error: Source file does not exist!"); return; } // Create target directory if it's missing if (!Files.exists(target.getParent())) { try { Files.createDirectories(target.getParent()); } catch (IOException e) { System.err.println("Failed to create target directory: " + e.getMessage()); return; } }
3. Invalid path/URI format
If you're using URIs to reference files (like jar:file:/path/to/zip!internal/path), make sure the format is correct. For ZIP files, the URI must follow the jar:<file-uri>!<internal-path> pattern.
Correct URI Usage Example:
URI zipUri = URI.create("jar:file:/C:/my-archive.zip!/internal/file.txt"); try (FileSystem zipFs = FileSystems.newFileSystem(zipUri, new HashMap<>())) { Path source = zipFs.getPath("/internal/file.txt"); // Add your copy logic here }
Quick Pre-Flight Checks
- Double-check all file paths for typos (relative paths can behave unexpectedly if your working directory isn't what you assume).
- Confirm your program has read permissions for the source location and write permissions for the target.
- If working with ZIPs, make sure the archive isn't corrupted or locked by another process.
内容的提问来源于stack exchange,提问作者gs650x

