Tuesday, August 4, 2015

[Java] Writing Byte to a File

Sample class that write byte to a file.



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteByteToAFile {
 private static FileOutputStream fileOutputStream;

 public static void main(String[] args) {
  String string = "Hello world";
  try {
    fileOutputStream = new FileOutputStream(new           File("temp-byte.txt"));
    fileOutputStream.write(string.getBytes(), 0,             string.length());
    fileOutputStream.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }  

 }//End main method

}//End class

No comments:

Post a Comment