title: Java : Writing a String to a JPG File
body: <p>Alright, so I am writing a small program that should have taken 10 minutes to complete however I am running into unforeseen problems. </p>
  
  <p>The program should take in some old files I had in a vault program on my old phone, they are basically Jpg files but with an added "obscured" text to the front of the file.  </p>
  
  <p>So below is my code logic</p>
  
  <ol>
  <li>get a folder input for the files, </li>
  <li>create an arraylist containing each actual file. </li>
  <li>call ConvertFiles to convert the file to a string, </li>
  <li>delete the first 8 characters using substring and save that temp file to another arraylist containing the strings. </li>
  <li>decode that string as base64 and input that into a bytearrayinputstream and save that to a bufferedimage. </li>
  </ol>
  
  <p>This is where the problem occurs. I have content all the way up to the ImageIO.read(bis), so when it tries to write to a new file it throws the image == null 
  from the ImageTypeSpecifier. I have tried multiple ways of decoding and encoding the string, but any help is wanted and if any more information is needed I will provide it!</p>
  
  <pre><code>public class ImageConvert {
  
      private File folder;
      private ArrayList&lt;File&gt; files;
      private ArrayList&lt;String&gt; stringFiles = new ArrayList&lt;&gt;();
      private ArrayList&lt;BufferedImage&gt; bfImages = new ArrayList&lt;&gt;();
      boolean isRunning = true;
      Scanner scanner = new Scanner(System.in);
      String folderPath;
  
      public static void main(String[] args) {
          ImageConvert mc = new ImageConvert();
          mc.mainCode();
      }
  
      public void mainCode(){
          System.out.println("Please enter the folder path: ");
          folderPath = scanner.nextLine();
          folder = new File(folderPath);
          //System.out.println("folderpath: " + folder);
          files = new ArrayList&lt;File&gt;(Arrays.asList(folder.listFiles()));
          convertFiles();
      }
  
      public void convertFiles(){
          for(int i = 0; i &lt; files.size(); i++){
              try {
                  String temp = FileUtils.readFileToString(files.get(i));
                  //System.out.println("String " + i + " : " + temp);
                  stringFiles.add(temp.substring(8));
              } catch (IOException ex) {
                  Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE,
                                                                      null, ex);
              }
          }
  
          //System.out.println("Converted string 1: " + stringFiles.get(0));
          for(int j = 0; j &lt; stringFiles.size(); j++){
  
              BufferedImage image = null;
              byte[] imageByte;
  
              try {
                 BASE64Decoder decoder = new BASE64Decoder();
                 imageByte = decoder.decodeBuffer(stringFiles.get(j));
                 System.out.println(imageByte.toString());
                 ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
                 image = ImageIO.read(bis);
                 bis.close();
                 bfImages.add(image);
            } catch (IOException ex) {
                 Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE,
                                                                     null, ex);
            }
  
          }
  
  
          System.out.println("Image 1: " + bfImages.get(0));
  
          for(int k = 0; k &lt; bfImages.size(); k++){
              try {
                  ImageIO.write(bfImages.get(k), "jpg",
                                new File(folderPath + "/" + k + ".jpg"));
              } catch (IOException ex) {
                  Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE,
                                                                      null, ex);
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p><a href="http://www.mediafire.com/download/8098hlprbeqt2tg/fdb42bd7-47277a60ra" rel="nofollow">This  is an example of my files:</a></p>