public static void writeImageToPpm(int[][] image,String ppmFileName) throws IOException {
  try (BufferedWriter ppmOut=new BufferedWriter(new FileWriter(ppmFileName))){
    int rows=image.length;
    int cols=image[0].length;
    ppmOut.write("P3\n");
    ppmOut.write("" + rows + " "+ cols+ " 255\n");
    for (    int[] anImage : image) {
      StringBuilder s=new StringBuilder();
      for (int j=0; j < cols; j++) {
        s.append(anImage[j] + " " + anImage[j]+ " "+ anImage[j]+ "  ");
      }
      ppmOut.write(s.toString());
    }
  }
 }

public static void writeImageToPpm ( int [ ] [ ] image , String ppmFileName ) throws IOException { try ( BufferedWriter ppmOut = new BufferedWriter ( new FileWriter ( ppmFileName ) ) ) { int rows = image . length ; int cols = image [ 0 ] . length ; ppmOut . write ( "P3\n" ) ; ppmOut . write ( "" + rows + " " + cols + " 255\n" ) ; for ( int [ ] anImage : image ) { StringBuilder s = new StringBuilder ( ) ; for ( int j = 0 ; j < cols ; j ++ ) { s . append ( anImage [ j ] + " " + anImage [ j ] + " " + anImage [ j ] + "  " ) ; } ppmOut . write ( s . toString ( ) ) ; } } }
writes the given image in the given file using the ppm data format .
