Cannot copy a file using "File.copy" method in java - java

I am new to Java.
While I am trying to copy a file using "File.copy" method in java.
The process end without errors.
But the file I need to copy is created with only one weired character (I think it is an file end character). and it has no content other than it.
Here is my source code,
var sourceFile = new File("mySourceFile.txt")
var destinyFile = new File("myDestinyFile.txt");
Files.copy(sourceFile.toPath(), destinyFile.toPath());

it doesnt matter if the file you will copy has one or hundred character. You say the process ends without errors. Does it mean it works fine or it does someting wrong but you get no error. If this is the case you can try to take the full path like this:
var sourceFile = new File("C:\\Users\\Kevin\\test1.txt").toPath();
var destinyFile = new File("C:\\Users\\Kevin\\test2.txt").toPath();
Files.copy(sourceFile, destinyFile);

I solved the problem.
The problem is that there are outputstreams already open for the same destiny file I have to.
So it File.copy() method cannot write on the file.

Related

No matter what I do, I can't get Java to recognize the filepath for a text file

I've been trying to set up a Scanner to use a File as an input, but it doesn't seem to recognize the filepath. The file exists in the same folder as my .java files.
File errorList = new File("Errors.txt");
Scanner errorIn = new Scanner(errorList);
This results in a FileNotFoundException.
What am I doing wrong, and how can I fix this?
One other approach you could try is, execute the below code in your eclipse (from any of your class), and see where the hello.txt is created, so you get an idea of where Java is looking for the file.
new File("hello.txt").createNewFile();
Then you could either put your Errors.txt in that location or provide the corresponding relative location.

File class cannot access file using absolute path

I am trying to access a file using the file class but it works only with below code
File file = new File("s:\\testing\\selenium\\Version8\\locators\\OR.properties");
however I need to use this approach where I go directly to the server:
"\\GIGABYTE\\s-drive\\Testing\\selenium\\Version8\\locators\OR.properties"in which the above will not find the file - very strange
Any clue as to why this is?
To go directly to the server you will need to have 4 slashes as two are actual slashes and 2 are escape characters:
\\\\GIGABYTE\\s-drive\\Testing\\selenium\\Version8\\locators\OR.properties
You can try the below code:
File file = new File("\\\\GIGABYTE\\s-drive\\Testing\\selenium\\Version8\\locators\\OR.properties");
or this:
File file = new File("//GIGABYTE/s-drive/Testing/selenium/Version8/locators/OR.properties");

Robovm Read and write to a file

I try to write a text to a file and read this text later. When I use FileWriter I become a NullPointerException?
Is that a permission problem or ...? I also try the PrintWriter but I see the same Exception
.
This my code:
FileWriter fw = new FileWriter(new File("file.file"));
fw.write("XYZ");
best regards
londi
I guess your problem is that you use a relative file path, but that the origin of the relative path is not the one you think.
First of all, try to use an absolute path, that would be, on linux-like machines something like /home/me/myCode/myfile.txt or on windows something like c:/some/path/myfile.txt
Another thing you can do, in order to know what happens is print the origin.
File origin = new File(".");
System.out.println(origin.getAbsolutePath());
Once you know where the origin is, you can see what you need in order to get to your file.
Hope it will help.
Sounds like a permission issue. On iOS your application lives within a security sandbox, so you cannot just randomly read and write files anywhere you want. You could either use File.createTempFile to create a temp file somewhere hidden you your sandbox where nothing else can see it, or use the native api to determine where to dump your files. The following example will give you a file reference to the Documents Directory folder:
NSArray nsa = NSFileManager.defaultManager().URLsForDirectory$inDomains$(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask);
NSURL nsu = (NSURL)nsa.getFirst();
String snsu = nsu.getAbsoluteString() + "MyNewDocument.pdf";
File newFile = new File(new URI(snsu));

Files, URIs, and URLs conflicting in Java

I am getting some strange behavior when trying to convert between Files and URLs, particularly when a file/path has spaces in its name. Is there any safe way to convert between the two?
My program has a file saving functionality where the actual "Save" operation is delegated to an outside library that requires a URL as a parameter. However, I also want the user to be able to pick which file to save to. The issue is that when converting between File and URL (using URI), spaces show up as "%20" and mess up various operations. Consider the following code:
//...user has selected file
File userFile = myFileChooser.getSelectedFile();
URL userURL = userFile.toURI().toURL();
System.out.println(userFile.getPath());
System.out.println(userURL);
File myFile = new File(userURL.getFile());
System.out.println(myFile.equals(userFile);
This will return false (due to the "%20" symbols), and is causing significant issues in my program because Files and URLs are handed off and often operations have to be performed with them (like getting parent/subdirectories). Is there a way to make File/URL handling safe for paths with whitespace?
P.S. Everything works fine if my paths have no spaces in them (and the paths look equal), but that is a user restriction I cannot impose.
The problem is that you use URL to construct the second file:
File myFile = new File(userURL.getFile());
If you stick to the URI, you are better off:
URI userURI = userFile.toURI();
URL userURL = userURI.toURL();
...
File myFile = new File(userURI);
or
File myFile = new File( userURL.toURI() );
Both ways worked for me, when testing file names with blanks.
Use instead..
System.out.println(myFile.toURI().toURL().equals(userURL);
That should return true.

Generate URL for File

The default output of File.toURL() is
file:/c:/foo/bar
These don't appear to work on windows, and need to be changed to
file:///c:/foo/bar
Does the format
file:/foo/bar
work correctly on Unix (I don't have a Unix machine to test on)? Is there a library that can take care of generating a URL from a File that is in the correct format for the current environment?
I've considered using a regex to fix the problem, something like:
fileUrl.replaceFirst("^file:/", "file:///")
However, this isn't quite right, because it will convert a correct URL like:
file:///c:/foo/bar
to:
file://///c:/foo/bar
Update
I'm using Java 1.4 and in this version File.toURL() is not deprecated and both File.toURL().toString() and File.toURI().toString() generate the same (incorrect) URL on windows
The File(String) expects a pathname, not an URL. If you want to construct a File based on a String which actually represents an URL, then you'll need to convert this String back to URL first and make use of File(URI) to construct the File based on URL#toURI().
String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.toURI());
Update: since you're on Java 1.4 and URL#toURI() is actually a Java 1.5 method (sorry, overlooked that bit), better use URL#getPath() instead which returns the pathname, so that you can use File(String).
String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.getPath());
The File.toURL() method is deprecated - it is recommended that you use the toURI() method instead. If you use that instead, does your problem go away?
Edit:
I understand: you are using Java 4. However, your question did not explain what you were trying to do. If, as you state in the comments, you are attempting to simply read a file, use a FileReader to do so (or a FileInputStream if the file is a binary format).
What do you actually mean with "Does the format file:/c:/foo/bar work correctly on Unix"?
Some examples from Unix.
File file = new File("/tmp/foo.txt"); // this file exists
System.out.println(file.toURI()); // "file:/tmp/foo.txt"
However, you cannot e.g. do this:
File file = new File("file:/tmp/foo.txt");
System.out.println(file.exists()); // false
(If you need a URL instance, do file.toURI().toURL() as the Javadoc says.)
Edit: how about the following, does it help?
URL url = new URL("file:/tmp/foo.txt");
System.out.println(url.getFile()); // "/tmp/foo.txt"
File file = new File(url.getFile());
System.out.println(file.exists()); // true
(Basically very close to BalusC's example which used new File(url.toURI()).)

Categories