0

I have below code to copy the data from one file and paste in another, can someone please help me to avoid the exception **FileSystemNotFoundException ** Thank you in anticipation....................................................................I have added the exception screenshot 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);          
            }
        }
gs650x
  • 307
  • 1
  • 10
  • 1
    Please paste the error stacktrace also for ease of undertanding. – Manmohan_singh May 04 '18 at 06:30
  • Check this. https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Naresh Bharadwaj May 04 '18 at 06:31

1 Answers1

0

are you sure, that Paths.get("mydata.zip") returns a valid path to the file? (File not found?!)

sorry, would have written this as a comment, but currently I'm not allowed to

Markus
  • 23
  • 5