import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class ImagesEncodingDecoding {
/**
* @param args
*/
public static void main(String[] args) {
// file names from folder
File folder = new File("<path of folder1>");
File[] listOfFiles = folder.listFiles();
String name = null;
for (int i = 0; i < listOfFiles.length; i++) {
name=listOfFiles[i].getName();
String path="<path of folder1>"+name;
File file = new File(path);
FileInputStream fin =null;
String encodedStr=null;
try {
fin = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte fileContent[] = new byte[(int)file.length()];
try {
fin.read(fileContent);
encodedStr=Base64.encode(fileContent);
String strFileContent = new String(fileContent);
System.out.println("File content : ");
System.out.println(strFileContent);
System.out.println("After encoding content : ");
String nametext=name.substring(0,name.indexOf('.'));
FileWriter f2 = new FileWriter("<path of folder2>"+nametext+".txt");
try {
BufferedWriter out = new BufferedWriter(f2);
try {
out.write(encodedStr);
out.close();
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(encodedStr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f3=new File("<path of folder3>"+name);
try {
FileOutputStream out=new FileOutputStream(f3);
try {
out.write(Base64.decode(encodedStr));
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}