Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.2 kB
2
Indexable
Never
import java.io.FileWriter;
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 WriteToPemFile {

    public static void main(String[] args) {
        String dataToWrite = "This is your data to be written to the PEM file.";

        try {
            writeToPemFile(dataToWrite, "path/to/your/file.pem");
            System.out.println("Data written to PEM file successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void writeToPemFile(String data, String filePath) throws IOException {
        String pemContent = convertToPemFormat(data);

        try (FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(pemContent);
        }
    }

    private static String convertToPemFormat(String data) {
        // Wrap the data with PEM headers and footers
        StringBuilder pemContent = new StringBuilder();
        pemContent.append("-----BEGIN DATA-----\n");
        pemContent.append(Base64.getEncoder().encodeToString(data.getBytes())).append("\n");
        pemContent.append("-----END DATA-----\n");

        return pemContent.toString();
    }
}
Leave a Comment