Created
October 10, 2017 07:24
-
-
Save AliZafar120/da63a3602c6bdd96bab3b2a0866794e7 to your computer and use it in GitHub Desktop.
Export Files to another folder/change extension using java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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