Created
August 28, 2019 13:04
-
-
Save thekalinga/dcb7c62ca09b49fb0d9e759d96009b6c to your computer and use it in GitHub Desktop.
Base64 encode file content (eager fetch) in java8
This file contains hidden or 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
public static String base64Encode(Path filePath) { | |
String base64EncodeFileConetnt = ""; | |
try (InputStream fileStream = newInputStream(filePath)) { | |
// Reading a Image file from file system | |
byte[] fileData = new byte[(int) Files.size(filePath)]; | |
//noinspection ResultOfMethodCallIgnored | |
fileStream.read(fileData); | |
base64EncodeFileConetnt = Base64.getEncoder().encodeToString(fileData); | |
} catch (FileNotFoundException e) { | |
System.out.println("File not found" + e); | |
} catch (IOException ioe) { | |
System.out.println("Exception while reading the file " + ioe); | |
} | |
return base64EncodeFileConetnt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment