Skip to content

Instantly share code, notes, and snippets.

@AliZafar120
Created October 10, 2017 07:24
Show Gist options
  • Save AliZafar120/da63a3602c6bdd96bab3b2a0866794e7 to your computer and use it in GitHub Desktop.
Save AliZafar120/da63a3602c6bdd96bab3b2a0866794e7 to your computer and use it in GitHub Desktop.
Export Files to another folder/change extension using java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class TransformFiles {
public static void main(String[] args) throws IOException {
final File folder= new File("C:\\Users\\User\\Downloads\\Compressed\\cars\\cars1");
for (final File fileEntry : folder.listFiles()) {
File destination = new File("C:\\Users\\User\\Downloads\\Compressed\\cars\\cars2\\"+fileEntry.getName().replaceAll(".bmp",".jpg"));
copyFile(fileEntry,destination);
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new RandomAccessFile(sourceFile,"rw").getChannel();
destination = new RandomAccessFile(destFile,"rw").getChannel();
long position = 0;
long count = source.size();
source.transferTo(position, count, destination);
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment