You are here: Home / Topics / Program to write data into a file using BufferedOutputStream in Java

Program to write data into a file using BufferedOutputStream in Java

Filed under: Java on 2024-03-01 06:41:17

// Program to write data into a file using BufferedOutputStream.

import java.io.*;

class  BufferedOutputStreamDemo
{
public static void main( String args[ ] ) throws IOException
{
 // attach keyboard to DataInputStream
 DataInputStream dis = new DataInputStream( System.in );
 
 // attach file to FileOutputStream
 FileOutputStream fos = new FileOutputStream( "file1.txt" );
 BufferedOutputStream bos = new BufferedOutputStream( fos, 1024 );
 
 System.out.println("Enter string ( $ to end ) : ");
 char ch;
 
 // read character from dis into ch and write them into bos.
 while( (ch=(char)dis.read()) != '$' )
 {
  bos.write(ch);
 }
 
 // close the file.
 bos.close();
}
}


Output:

Enter string ( $ to end ) : 
Java Programming Example App

About Author:
G
Geetam     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.