Skip to content

Instantly share code, notes, and snippets.

@ezhov-da
Last active March 10, 2019 12:25
Show Gist options
  • Save ezhov-da/d1121becb3246493867a38ed00eba36d to your computer and use it in GitHub Desktop.
Save ezhov-da/d1121becb3246493867a38ed00eba36d to your computer and use it in GitHub Desktop.
java-code-base64-encoder
package ru.ezhov;
import sun.misc.BASE64Encoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
public class Base64File {
private static String fileSource = "E:\\_git_local_data_\\trunk\\groovy\\from-db-to-db-base64.zip";
private static String fileTarger = "E:\\_git_local_data_\\trunk\\groovy\\from-db-to-db-base64.text";
public static void main(String[] args) {
BASE64Encoder base64Encoder = new BASE64Encoder();
try {
BufferedInputStream fileInputStream =
new BufferedInputStream(new FileInputStream(fileSource));
List<Byte> bytes = new ArrayList<Byte>();
byte[] nowByte = new byte[fileInputStream.available()];
int read = 0;
while ((read = fileInputStream.read(nowByte)) != -1) {
for (int i = 0; i < nowByte.length; i++) {
bytes.add(nowByte[i]);
}
}
System.out.println(bytes.size());
byte[] finalByte = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); i++) {
finalByte[i] = bytes.get(i);
}
fileInputStream.close();
String data = base64Encoder.encode(finalByte);
FileWriter fileWriter = new FileWriter(fileTarger);
fileWriter.write(data);
fileWriter.close();
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment