如何避免java中的FileSystemNotFoundException

时间:2018-05-04 06:22:41

标签: java filesystems

我有以下代码从一个文件复制数据并粘贴到另一个文件中,有人可以帮我避免异常** FileSystemNotFoundException ** 谢谢你期待.............................................. ......................我添加了例外截图 enter image description here

 package com.mod2.zipIn;

    import java.io.*;
    import java.net.*;
    import java.nio.file.*;
    import java.nio.file.FileSystem;
    import java.util.*;

    public class ZipIn {

        public static void main(String[] args)
        {
            String[] data = {
                    "Line 1 ",
                    "Line 2 2 ",
                    "Line 3 3 3",
                    "Line 4 4 4 4",
                    "Line 5 5 5 5 5"
            };


            try (FileSystem zipFs  = openZip(Paths.get("mydata.zip"))){
                copyToZip(zipFs);
            } catch (Exception e) {
                System.out.println(e.getClass().getSimpleName() + " " + e.getMessage());
            }
        }
            private static FileSystem openZip(Path zipPath) throws IOException, URISyntaxException {

                Map<String, String> providerProps = new HashMap<>();
                providerProps.put("Create", "True");

                URI zipURI = new URI("jar:file", zipPath.toUri().getPath(), null);

                FileSystem zipFs = FileSystems.newFileSystem(zipURI,providerProps);

                return zipFs;
            }

            private static void copyToZip(FileSystem zipFs)throws IOException {
                Path sourceFile  = Paths.get("file1.txt");
                Path destFile = zipFs.getPath("\file1Copied.txt");

                Files.copy(sourceFile, destFile, StandardCopyOption.REPLACE_EXISTING);          
            }
        }

1 个答案:

答案 0 :(得分:0)

你确定,Paths.get(&#34; mydata.zip&#34;)会返回文件的有效路径吗? (找不到文件?!)

抱歉,本来会把它写成评论,但目前我不允许