Copy jpg from folder to folder on Linux - java

I'm using Files.copy(sourceFile,destFile) from apache's commonsIO lib, in order to copy jpg from one folder to another on Linux machine.
Actually I'm doing it for all pic's in the folder :
File folder = new File(sourcePath);
File[] folderContent = folder.listFiles();
File tmp = null;
File sourceFile = null;
File destFile = null;
//copy all pics to other folder :
for(int i=0;i<folderContent.length;i++){
if(folderContent[i].getName().endsWith("jpg")){
sourceFile = new File(sourcePath);
destFile = new File(destPath);
//copy to main dir:
Files.copy(sourceFile,destFile);
}
}
But all I get in the new folder is empty files (with the correct name).
When I tested it with a simple test with one file ,Like that :
Files.copy(sourceFile,destFile);
then the file copy successfully.
Does anyone have a clue ?? (Is it a java-Linux known issue ?)
Thanks!

This is no Linux issue.
First, you use the source folder as the source file, not the file itself.
Also, possibly, you use use the destination folder as the copy target.
Assuming destPath is the destination folder:
for(File file : folderContent){
if(file.getName().endsWith("jpg")){
Files.copy(file, new File(destPath, file.getName()));
}
}

Related

How to write relative file path for a file in java project folder

The project name is 'producer'. I have a file located in project folder C:/Users/Documents/producer/krb5.conf.
If I want to write its relative path, should I write
File file = new File("krb5.conf");
or
File file = new File("producer/krb5.conf");
or
File file = new File("./krb5.conf");
?
You can use both your 1. and 3. option.
The 2. option would refer to C:/Users/Documents/producer/producer/krb5.conf.
For the purpose of testing you could try to get the absolute path from each file and print it.
// 1.
File file1 = new File("krb5.conf");
File file2 = new File("producer/krb5.conf");
File file3 = new File("./krb5.conf");
System.out.println(file1.getAbsolutePath());
// Output: C:\Users\Documents\producer\krb5.conf
System.out.println(file2.getAbsolutePath());
// Output: C:\Users\Documents\producer\producer\krb5.conf
System.out.println(file3.getAbsolutePath());
// Output: C:\Users\Documents\producer\.\krb5.conf
The 3. path may look a bit weird at first, but it also works.
C:\Users\Documents\producer\. points to the current directory, so it is essentially the same as C:\Users\Documents\producer.

Java make directory but not file

So with the code below it creates the file in the folder
File f = new File(path);
if(!f.exists())
f.mkdirs();
, but i only want to create the directory, because after this i use this code
file.transferTo(new File(path));
which saves a Multipart file to the same location, but it throws and error because there is already a file. Is there a way only to create the folder without the file ? One solution is to delete the first file, but looking for better solution
EDIT:
File f = new File(path);
this line creates the folders and the file, it shouldn't. I use java 8 and IntelliJ 14
SOLUTION:
The problem was Intellij or Intellij debug watches. After restarting it and clearing watches which were like:
new File(path)
file.transferTo(new File(path))
f.exists()
The code started working.
It should be
f.getParentFile().mkdirs();
You don't need to check for existence beforehand: mkdirs() already does that.
File dir = new File("<Your_Path>/TestDirectory");
// attempt to create the directory here
boolean successful = dir.mkdir();
if (successful)
{
// creating the directory succeeded
System.out.println("directory was created successfully");
}
else
{
// creating the directory failed
System.out.println("failed trying to create the directory");
}
You can create your files inside your directory path from then on....

Does Files.copy(Path,Path) create directories?

I have a bunch of text files(say ss1.txt,ss2.txt,ss3.txt etc.) under a directory with my Java program (C:/Users/java/dir1)?
I want to move my txt files to a new directory that hasn't been created yet. I have a String address for all of my files and I think I can turn them into Paths using
Path path = Paths.get(textPath);
Would creating a String (C:/Users/java/dir2), turning that into a path using the above method and then using
Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)
result in ss1.text being copied to a new directory?
This is very easy with Files.createDirectories()
Path source = Path.of("c:/dir/dir-x/file.ext");
Path target = Path.of("c:/target-dir/dir-y/target-file.ext");
Files.createDirectories(target.getParent());
Files.copy(path, target, StandardCopyOption.REPLACE_EXISTING);
And do not worry if the directories already exist, in that case it will do nothing and keep going...
Method Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2) will not create directory, it will create file dir2 in directory java that will contain ss1.txt data.
You could try it with this code:
File sourceFile = new File( "C:/Users/java/dir1/ss1.txt" );
Path sourcePath = sourceFile.toPath();
File destFile = new File( "C:/Users/java/dir2" );
Path destPath = destFile.toPath();
Files.copy( sourcePath, destPath );
Remember use java.nio.file.Files and java.nio.file.Path.
If you want to use class form java.nio to copy files from one directory to other you should use Files.walkFileTree(...) method. You can see solution here Java: Using nio Files.copy to Move Directory.
Or you can simply use `FileUtils class from apache http://commons.apache.org/proper/commons-io/ library, available since version 1.2.
File source = new File("C:/Users/java/dir1");
File dest = new File("C:/Users/java/dir2");
try {
FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
e.printStackTrace();
}

Adding files to a file[] statement in java

Hello members of stackoverflow
I need some help with adding files from a directory to a bunch of files (File[])
So basically i want to add all the files in a directory to the following group of files:
File[] contents = {};
The user of my application will select a directory and i want that directories contents to be added to the above group of files ('contents I'm not sure how this is done because it doesn't have a simple 'add' method like an ArrayList/List does.
Any help with this would be greatly appreciated.
Thanks.
Use File.listFiles():
File dir = new File("/somedir");
File[] files = dir.listFiles();
try
{
File folder = new File(FOLDER_NAME);
File[] contents = folder.listFiles();
}
catch(Exception e)
{
System.out.println("[Error] Folder not Found");
}

Problem with moving a file to another folder

I am using:
// File (or directory) to be moved
File file = new File(output.toString());
// Destination directory
File dir = new File(directory_name);
// Move file to new directory
boolean success = file.renameTo(new File(dir, new_file.getName()));
if (!success) {
// File was not successfully moved
}
In this case file is main.vm, and folder is seven
the program shows that it works(the file exists and all) but the file is not moving to the seven directory.
Any ideas why?
Is it ok that the file name is main.vm or do i need to enter full path? the same for the folder.
Thanks
Maybe you wanna take a look at the Apache Commons FileUtils
Works for me. (run with java -ea opt.)
File f = new File("foo.mv");
if(!f.exists())
assert f.createNewFile() : "failed to create foo.mv";
File folder = new File("7");
if(!folder.exists())
assert folder.mkdir() : "failed to create new directory";
File fnew = new File(folder, f.getName());
assert !fnew.exists() : "fnew already exists";
f.renameTo(fnew);
assert fnew.exists() : "fnew does not exist -- move failed";
System.out.format("moved %s to %s\n",f, fnew);
Try to do following steps:
check if you move the file inside same filesystem - otherwise it will fail;
create destination directory;
define "new_file" variable.
You need to enter full path of the file, not only the filename. And would be nice if you'll show up your full source code in the future, for better understanding/answers.

Categories

Resources