How to write to file in Java – FileOutputStream example
Published: May 30, 2010 , Updated: May 28, 2010 , Author: mkyong
In Java, FileOutputStream is an bytes stream class to handle the raw binary data. To write it, you have to convert the data into bytes, see below example
FileOutputStream example
package com.mkyong.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample { public static void main( String[] args ) { try{ String content = "This is the text content"; File file =new File("newfile.txt"); //if file doesnt exists, then create it if(!file.exists()){ file.createNewFile(); } FileOutputStream fop=new FileOutputStream(file); //get the content in bytes fop.write(content.getBytes()); fop.flush(); fop.close(); System.out.println("Done"); }catch(IOException e){ e.printStackTrace(); } } }
Reference
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~