Error while copying files from source to destination java - java

//original file
Path original = Paths.get("C:\\Users\\Laksahan\Desktop\\bg.jpg");
File f = new File("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
// new file
Path destination = Paths.get("C:\\Program Files\\Tour v0.1\\image\\"+f.getName());
try {
Files.copy(original, destination, LinkOption.NOFOLLOW_LINKS);
} catch (IOException x) {
x.printStackTrace();
}
i tried above method to copy files, it wont work and it prints this error
java.nio.file.NoSuchFileException: C:\Users\Laksahan\Desktop\bg.jpg -> C:\Program Files\Tour v0.1\image\bg.jpg
please help

Java 7's NIO will not create a folder if it doesn't exist when using Files.copy().
The best you can do is check for the folder and create it if it doesn't exist before you call copy.

Try
Path original = Paths.get("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
instead of
Path original = Paths.get("C:\\Users\\Laksahan\Desktop\\bg.jpg");
Create a folder programmatically for example -
Path from = Paths.get("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
Path to = Paths.get("C:\\Program Files\\Tour v0.1\\image\\");
Path destination;
File f = new File("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
if (!Files.exists(to)) {
try {
Files.createDirectories(to);
} catch (IOException ioe) {
ioe.printStackTrace();
}
destination = Paths.get(to.toString() + "\\" + f.getName());
try {
Files.copy(from, destination, LinkOption.NOFOLLOW_LINKS);
} catch (FileAlreadyExistsException faee) {
faee.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

Related

How to read all files in directory iteratively in Java?

I have a directory on network drive with million of files. If I try to read it with
folder.listFiles()
it will take a lot of time until resulting array will be filled with files.
I would like to receive files by one and printout a progress.
How can I do this in Java?
You might try with DirectoryStream:
Path dir = Paths.get("C:\\"); // directory to list
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry: stream) {
System.out.println(entry);
}
} catch (DirectoryIteratorException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
You can also make the DirectoryStream filter files for you if you need to: all you need to do is add a parameter to the Files.newDirectoryStream call:
DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{txt,png,jpg}");
You can use the NIO.2 API:
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("/path/to/directory"))) {
for (Path path : directoryStream) {
System.out.println(path.toString());
}
} catch (IOException ex) {}

how to create dynamic paths in java and write the file data in java

I will get dynamic paths from database. Example: 1.xyz/abc/file1.txt
2.pqr/file2.txt
Now I need to append these paths to existing file (eg:/users/rama/) and save that file
my final directory should like /users/rama/xyz/abc/file1.txt
I am able to create directories such as xyz/abc if they don't exist, but the problem is file1.txt is also created as directory instead of file.
I am able to create directories such as xyz/abc if they don't exist,
but the problem is file1.txt is also created as directory instead of
file.
Because you are creating the directories until *.txt. Below an example of code to acheive what you want:
String prefix = "/users/rama/";
String filePath = "xyz/abc/file1.txt";
// concatenation => /users/rama/xyz/abc/file1.txt
String fullPath = prefix.concat(filePath);
PrintWriter writer;
try {
// Getting the directory path : /users/rama/xyz/abc/
int lastIndexOfSlash = fullPath.lastIndexOf("/");
String path = filePath.substring(0, lastIndexOfSlash);
File file = new File(path);
// If /users/rama/xyz/abc/ don't exist then creating it.
if(!file.exists()) {
file.mkdirs();
}
// Creating the file.
writer = new PrintWriter(fullPath, "UTF-8");
writer.println("content");
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Unable to located generated file

I am generating a file using the following syntax
File file = new File("input.txt");
The problem is that it is saying that it is writing to the file but I am not able to locate where the file is created, I searched my entire workspace. The expectation was that it would be created in the same folder as my code which is executing.
Any ideas?
Rest of the code :
File file = new File("input.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
You could do a sop on the absolute path and you would get the path:
File file = new File("input.txt");
System.out.println("" + file.getAbsolutePath());
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
When you create file through relative paths, Java uses System.getProperty("user.dir"). So, in your case the full path to file will be System.out.println(System.getProperty("user.dir") + "/input.txt");.

How to copy a zip file in Java

I have a zip file that gets created at runtime that I need to copy to another directory however whenever I run my code I get a DirectoryNotEmptyException. Is there some extra parameter I need to specify to copy into a non-empty directory?
Here's the layout
Path sourceZip = new File(path).toPath();
String destinDir = new File(System.getProperty("user.dir")).getParent();
Path target = Paths.get(destinDir);
try {
Files.copy(sourceZip, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) //DirectoryNotEmptyException occurs here
{}
the destination needs to contain the full path of the file that will eventually be there.
so you say if you needed to copy /home/dauser/faq.txt to /home/faq.txt
File file = new File(path);
Path sourceZip = file.toPath();
StringBuilder sb = new StringBuilder();
sb.append(new File(System.getProperty("user.dir")).getParent());
sb.append("/");
sb.append(file.getName());
Path target = Paths.get(sb.toString());
try {
Files.copy(sourceZip, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) //DirectoryNotEmptyException occurs here
{}

i want to overwrite the existing zipfile

i want to overwrite the existing zip file(which mean i add new file in existing zip file) but here show this error (java.util.zip.ZipError: zip END header not found)
private void updateZip(String fileName, String scenarioDirectory){
System.out.println("File Name : " +fileName);
System.out.println("Scenario Directory : " +scenarioDirectory);
String scenarioName ="12345";
Path myFilePath = Paths.get(fileName);
Path zipFilePath = Paths.get(scenarioDirectory);
FileSystem fs;
try {
fs = FileSystems.newFileSystem(zipFilePath,null);
Path fileInsideZipPath = fs.getPath(scenarioName);
Files.copy(myFilePath, fileInsideZipPath);
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try this :
if your file exist then delete it ant after save the new.
Or Added a file directly in the zip code since this example https://stackoverflow.com/a/17500924/4017037

Categories

Resources