FileOutputStream Class: Write data into File

In this blog, we will see FileOutputStream class. This class uses to write data into any .txt, .bat, .java etc file in folder.

In this program, we will see how to create file automatically and how to write data in created file. This is file save near to your java file in same folder.

Better understanding, we will see video first then move to our program.

 

Video on Write data into File using FileOutputStream Class

 

 

Program on FileOutputStream Class

import java.io.*;

class io6

{

                public static void main(String args[]) throws IOException

                {

                                BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

 

                                FileOutputStream fout = null;

                                try

                                {

                                                fout = new FileOutputStream(“newFile.txt”);

                                                // newFile.txt is not in a folder

                                                // using of this FileOutputStream -> file automatically create in folder

 

                                                // How to write data in file

                                                // write() -> write ASCII and char letter

                                                char ch;

                                                do

                                                {

                                                                ch = (char)br.read();       // read from o/p screen

                                                                fout.write(ch);  // write into the file

                                                }

                                                while(ch!=’Q’);

                                }             

                                catch(FileNotFoundException ex)

                                {

                                                System.out.println(ex);

                                }                             

                }

}

Output:

Enter some text here

For Stop: Press Q


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

204total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

EmailEmail
PrintPrint