1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.Base64;
  5. public class FileEncoder {
  6. public static void main(String[] args) {
  7. if (args.length != 1) {
  8. System.out.println("Usage: java FileEncoder <input_file>");
  9. return;
  10. }
  11. String inputFile = args[0];
  12. try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
  13. String fileContents = reader.readLine(); // Read the first line. Adjust as needed for larger files.
  14. if(fileContents == null){
  15. System.out.println("File is empty.");
  16. return;
  17. }
  18. byte[] fileBytes = fileContents.getBytes(); // Convert to bytes
  19. String encodedString = Base64.getEncoder().encodeToString(fileBytes); // Encode to Base64
  20. System.out.println(encodedString); // Print the encoded output
  21. } catch (IOException e) {
  22. System.err.println("Error reading file: " + e.getMessage());
  23. }
  24. }
  25. }

Add your comment