Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

byte array to base64 string

package com.mkyong.string;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

public class ConvertBytesToStringBase64 {

    public static void main(String[] args) {

        String filepath = "/Users/mkyong/phone.png";
        Path path = Paths.get(filepath);

        if (Files.notExists(path)) {
            throw new IllegalArgumentException("File is not exists!");
        }

        try {

            // convert the file's content to byte[]
            byte[] bytes = Files.readAllBytes(path);

            // encode, byte[] to Base64 encoded string
            String s = Base64.getEncoder().encodeToString(bytes);
            System.out.println(s);

            // decode, Base64 encoded string to byte[]
            byte[] decode = Base64.getDecoder().decode(s);

            // save into another image file.
            Files.write(Paths.get("/Users/mkyong/phone2.png"), decode);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: java arraylist to array 
Java :: how to end a program in an if statement java 
Java :: java check if enum contains value 
Java :: java example 
Java :: share intent android 
Java :: n queens problem leetcode 
Java :: clear jtable rows java 
Java :: java get folder content 
Java :: initialize arraylist 
Java :: java load image 
Java :: AmazonS3ClientBuilder 
Java :: javafx resizable window 
Java :: android dialog 
Java :: view binding in recyclerview adapter android java 
Java :: how to print hello world java 
Java :: java create list integer range 
Java :: kjk;ok 
Java :: base64 in java 
Java :: thread 
Java :: lambda comparator java 
Java :: eclipse java content assist 
Java :: awk print second 
Java :: printing 2d array in java 
Java :: add opacity to activity android 
Java :: how to remove duplicate elements from char array in java 
Java :: Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/webview 
Java :: Processing Java examples 
Java :: android separator line in view 
Java :: android application manifest 
Java :: template competitive programming java 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =