public static void saveX509Cert ( Certificate [ ] certs , File certFile ) throws GeneralSecurityException , IOException { try ( BufferedWriter writer = new BufferedWriter ( new FileWriter ( certFile ) ) ) { for ( Certificate cert : certs ) { String certStr = Base64 . getEncoder ( ) . encodeToString ( cert . getEncoded ( ) ) . replaceAll ( "(.{64})" , "$1\n" ) ; writer . write ( BEGIN_CERT ) ; writer . newLine ( ) ; writer . write ( certStr ) ; writer . newLine ( ) ; writer . write ( END_CERT ) ; writer . newLine ( ) ; } } }
Save a list of certificates into a file