in Education by
I have to copy classpath resource from one package to another. My program is: public static void main(String[] args) throws IOException, URISyntaxException { ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader(); InputStream in = classLoader.getResourceAsStream("com/stackoverflow/main/Movie.class"); URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI(); Path path = Paths.get(uri.getPath(),"Movie.class"); System.out.println(path); long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); System.out.println(copy); } At Files.copy method I get exception: Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/Programs/workspaceEE/HibernateDemo/target/classes/com/stackoverflow/json at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94) at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255) at java.nio.file.Paths.get(Paths.java:84) at com.stackoverflow.main.CopyFileToDirectoryTest.main(CopyFileToDirectoryTest.java:34) How to solve it? Solution public static void main(String[] args) throws IOException, URISyntaxException { ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader(); InputStream in = classLoader.getResourceAsStream("com//stackoverflow//main//Movie.class"); URI uri = ClassLoader.getSystemResource("com//stackoverflow//json").toURI(); String mainPath = Paths.get(uri).toString(); Path path = Paths.get(mainPath, "Movie.class"); System.out.println(path); long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); System.out.println(copy); } This code correctly copies Movie.class from package com/stackoverflow/main into com/stackoverflow/json. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
problem is that Paths.get() doesnt expect that kind of value which is generated from uri.getPath(). Solution: URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI(); String mainPath = Paths.get(uri).toString(); Path path = Paths.get(mainPath ,"Movie.class");

Related questions

0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    How to read entire file in one line using java 8? (a) Files.readAllLines() (b) Files.read() (c) ... Autoboxing & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I use Selenium RC, for which the scroll command is selenium.getEval("scrollBy(0, 250)"); Now, I have started ... the command for it? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Which of these method is used to make a bit zero specified by the index? (a) put() (b) set() (c ... - The Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these is a method of ListIterator used to obtain index of previous element? (a) previous() (b ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which feature of java 8 enables us to create a work stealing thread pool using all available processors at ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Rewrite the following JAVA program segment using switch case.. char code; if(code=='B'||code=='b') System.out. ... now .plz help me . Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    Which of these exceptions handles the situations when an illegal argument is used to invoke a method? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    The __________ was a huge marketplace of Dark Web specifically famous for selling of illegal drugs & narcotics as well as you can ... Road 2) Cotton Road 3) Dark Road 4) Drug Road...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a text file, by which I am trying to write to in my program. Whenever user wants to add ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Rather than rewriting the entire contents of an xml file when a single element is updated, is there a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
...