title: How to convert string back to bytes in order to write to file in Java?
body: <p>I have a text file that contains the word "cool" in it. I read all the bytes in this file and turn it into a string. However, In another function where I am trying to turn the same string back to bytes to write into the file I don't get what I expected.</p>
  
  <pre><code>Path path = Paths.get(fileName);
  byte[] data = Files.readAllBytes(path);
  
  String x = new String();
  for(byte b: data){
      x += Byte.toString(b);
  }  
  System.out.println(x);
  </code></pre>
  
  <p><strong>Output:</strong> "cool" turned into bytes</p>
  
  <pre><code>99111111108
  </code></pre>
  
  <p>Unfortunately the code below does not write "cool" back to the file, instead it writes 99111111108. </p>
  
  <pre><code>str = "99111111108";
  FileOutputStream C = new FileOutputStream("new.txt");
  C.write(str.getBytes());
  C.close();   
  </code></pre>