Program to Write String to file using BufferedOutputStream (and FileOutputStream) in java file IO




Read : Write String to file using FileOutputStream in java file IO

Difference between FileInputStream and BufferedInputStream in java file IO

Read text from file using BufferedInputStream (and FileInputStream) in java file IO



Program to Write String to file using BufferedOutputStream (and FileOutputStream) in java file IO >





import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class WriteToFileUsingBufferedOutputStream {
   public static void main(String... args) {
          FileOutputStream fos = null;
          BufferedOutputStream bos = null;
         
          try {
                 fos = new FileOutputStream("c:/myFile.txt");
                 bos = new BufferedOutputStream(fos);
                 String str="You are learning File IO from javaMadeSoEasy.com";
                 bos.write(str.getBytes()); //convert String into byte array to write in file
                
                 bos.flush(); //bytes in buffer are written in file
                
                 System.out.println("String str has been written successfully in file "
                              + "using BufferedOutputStream");
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (bos != null){
                              bos.close(); //close BufferedOutputStream
                                          //it will close FileOutputStream
                       }
                    } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
String str has been written successfully in file using BufferedOutputStream
*/



RELATED LINKS>

Write String to file using FileOutputStream in java file IO


Write String to file using BufferedWriter and FileWriter in java file IO

Program to Write in file using PrintWriter to in java


Important Differences in File handling  >

Difference between FileInputStream and BufferedInputStream in java file IO


Difference between Stream (FileInputStream) and Reader (FileReader) in java file handling

No comments:

Post a Comment