how convert here URL to String in java

310
December 30, 2017, at 1:17 PM
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;`enter code here`

public class Mover {
    public static void main(String[] args) throws IOException, InterruptedException {

        URL source = Mover.class.getResource("host"); 
        source.toString();
        String destino = "C:\\users\\jerso\\desktop\\";

Path sourceFile = Paths.get(source,"hosts");//here an error occurs.
Path targetFile = Paths.get(destino,"hosts");
Files.copy(sourceFile, targetFile,StandardCopyOption.REPLACE_EXISTING);
    enter code here
}
}

I Don't know what to do here->>Path sourceFile = Paths.get(source,"hosts"); The method get(String, String...) in the type Paths is not applicable for the arguments (URL, String.

Answer 1

Calling toString() on source does not change the memory reference to now point to a string; toString() returns a string. What you're looking for is this:

Path sourceFile = Paths.get(source.toString(),"hosts");

Good luck!

Answer 2

The target could be composed as:

Path targetFile = Paths.get("C:\\users\\jerso\\desktop", "hosts");

Solution:

URL source = Mover.class.getResource("host/hosts"); 
Path sourceFile = Paths.get(source.toURI());
Files.copy(sourceFile, targetFile,StandardCopyOption.REPLACE_EXISTING);

Better (more immediate):

InputStream sourceIn = Mover.class.getResourceAsStream("host/hosts"); 
Files.copy(sourceIn, targetFile,StandardCopyOption.REPLACE_EXISTING);

Mind that getResource and getResourceAsStream use relative paths from the package directory of class Mover. For absolute paths: "/host/hosts".

READ ALSO
Not able to click an element and redirect to new tab in selenium

Not able to click an element and redirect to new tab in selenium

I am trying to learn selenium by testing them on different websitesIn this process, I am trying to work with Flipkart website

177
Passing Java ArrayList to C++ instance method throws java.lang.UnsatisfiedLinkError: ClassName.MethodName(Ljava/util/ArrayList;)V

Passing Java ArrayList to C++ instance method throws java.lang.UnsatisfiedLinkError: ClassName.MethodName(Ljava/util/ArrayList;)V

I am trying to pass an ArrayList to an instance method in C++The java function def looks like this:

160
Getting issues with ADB for react-native run-android

Getting issues with ADB for react-native run-android

I've been trying to follow the react-native tutorial on udemy, but I've been having trouble getting started: When I try to run the react-native run-android, I've been getting this error:

242