title: convert pdf to byteArray - byteArray to String - String to byteArray - byteArray to pdf
body: <p>I have the following code which works for text files but doesn't work for <code>pdf</code> files. My files contain english and greek characters. I try to convert a <code>pdf</code> file to <code>byteStream</code> and the <code>byteStream</code> to <code>String</code> format in order to save it in <code>database</code>. After this I try to create the <code>pdf</code> from the saved String.</p>
  
  <p>Any help?</p>
  
  <pre><code>public class PdfToByteStream {
  
      public static byte[] convertDocToByteArray(String path)throws FileNotFoundException, IOException{
          File file = new File(path);
  
          FileInputStream fis = new FileInputStream(file);
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          byte[] buf = new byte[1024];
          try {
              for (int readNum; (readNum = fis.read(buf)) != -1;) {
                  bos.write(buf, 0, readNum);
              }
          } catch (IOException ex) {
              Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex);
          }
          byte[] bytes = bos.toByteArray();
          return bytes;
      }
  
      public static void convertByteArrayToDoc(String path, byte[] bytes)throws FileNotFoundException, IOException {
          File someFile = new File(path);
          FileOutputStream fos = new FileOutputStream(someFile);
          fos.write(bytes);
          fos.flush();
          fos.close();
      }
  
      public static void main(String[] args) throws FileNotFoundException, IOException {
          byte[] bytes = convertDocToByteArray("path/test.pdf");
  
          String stream = new String(bytes, "UTF-8");//ok for txt
          byte[] newBytes = stream.getBytes(Charset.forName("UTF-8")); // ok for txt
  
          convertByteArrayToDoc("path/newTest.pdf", newBytes);
      }
  }
  </code></pre>