In programming you can move file from one location to another, if you move file from one location to another then that is not available on original file it move to another location .
this code help when you have multiple file in one location with different extension and you want to move file of specific extension for example you want all .pdf file then below code will guide you how to move file
Below is the code of move file:
package sumadding;
import java.io.File;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FileMove {
String destinationFilePath = "C:\\Users\\xyz\\workspace\\adding\\Destination\\";
String sourceFilePath = "C:\\Users\\xyz\\workspace\\adding\\";
File sourceFile = new File(sourceFilePath);
public void testing() throws IOException {
File[] files = sourceFile.listFiles();
CopyOption[] options = new CopyOption[]{
};
if(sourceFile != null)
{
for(File file:files)
{
if(file.isFile())
{
String fileName = file.getName();
String path = file.getPath();
String extension = fileName.substring(fileName.lastIndexOf("."));
if(extension.equalsIgnoreCase(".pdf"))
{
File srcPath = new File(path);
destinationFilePath = destinationFilePath.concat(fileName);
File destinationFile = new File(destinationFilePath);
// This line move your file from one location to another
Files.move(srcPath.toPath(), destinationFile.toPath(), options);
System.out.print("File Copied Successfull");
}
}
else
{
System.out.println("Is a Directory");
}
}
}
else
{
System.out.print("File does not Copied ");
}
}
public static void main(String[] args) throws IOException {
FileMove f = new FileMove();
f.testing();
}
}
this code help when you have multiple file in one location with different extension and you want to move file of specific extension for example you want all .pdf file then below code will guide you how to move file
Below is the code of move file:
package sumadding;
import java.io.File;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FileMove {
String destinationFilePath = "C:\\Users\\xyz\\workspace\\adding\\Destination\\";
String sourceFilePath = "C:\\Users\\xyz\\workspace\\adding\\";
File sourceFile = new File(sourceFilePath);
public void testing() throws IOException {
File[] files = sourceFile.listFiles();
CopyOption[] options = new CopyOption[]{
};
if(sourceFile != null)
{
for(File file:files)
{
if(file.isFile())
{
String fileName = file.getName();
String path = file.getPath();
String extension = fileName.substring(fileName.lastIndexOf("."));
if(extension.equalsIgnoreCase(".pdf"))
{
File srcPath = new File(path);
destinationFilePath = destinationFilePath.concat(fileName);
File destinationFile = new File(destinationFilePath);
// This line move your file from one location to another
Files.move(srcPath.toPath(), destinationFile.toPath(), options);
System.out.print("File Copied Successfull");
}
}
else
{
System.out.println("Is a Directory");
}
}
}
else
{
System.out.print("File does not Copied ");
}
}
public static void main(String[] args) throws IOException {
FileMove f = new FileMove();
f.testing();
}
}
No comments
Post a Comment