Site icon DataFlair

Java Program on How to Write Data in File

Free Java courses with 37 real-time projects - Learn Java

Program 1

package dataflair;
import java.io.*;
public class TestWrite1 
{
     public static void main(String[] args) throws IOException 
     {
          FileOutputStream fos=null;
           byte b[];
          try
          {
               File F=new File("d://filedata");
               F.mkdir();
              fos=new FileOutputStream("d://filedata/student.txt");
              char ch;
              String S="Data Flair free course with free certificate ,Indore MP India";
              b=S.getBytes();
              fos.write(b);
//              for(int i=0;i<b.length;i++)
//                  System.out.print(" "+b[i]);
              
              fos.flush();
//              for(int i=0;i<S.length();i++)
//              {
//                   ch=S.charAt(i);
//                   fos.write(ch);
//              }
              
              System.out.println("File Created.......");
          }
          finally
          {
              fos.close();
          }
         
     }
    
}

Program 2

package dataflair;
import java.io.*;
public class TestFileWrite2 
{
    public static void main(String[] args) throws IOException
    {
       FileOutputStream fos=null;
           byte b[];
          try
          {
               File F=new File("d://filedata");
               F.mkdir();
              fos=new FileOutputStream("d://filedata/student.txt");
              char ch;
              String S="Data Flair Free Course With Free Certificate ,Indore MP India";
               for (int i = 0; i < S.length(); i++) 
               {
                  ch=S.charAt(i);
                  if(Character.isUpperCase(ch))
                      fos.write(ch);
               }
              
              System.out.println("File Created.......");
          }
          finally
          {
              fos.close();
          }
             
    }
    
}

Program 3

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dataflair;
import java.io.*;
/**
 *
 * @author admin
 */
public class TestFileWrite3 
{
    public static void main(String[] args) throws IOException
    {
      FileOutputStream fos=null;
           byte b[];
          try
          {
               File F=new File("d://filedata");
               F.mkdir();
               File F1=new File("d://filedata/student123.txt");
               fos=new FileOutputStream(F1);
              char ch;
              String S="Data Flair Free Course With Free Certificate ,Indore MP India";
               for (int i = 0; i < S.length(); i++) 
               {
                  ch=S.charAt(i);
                  if(Character.isUpperCase(ch))
                      fos.write(ch);
               }
              
              System.out.println("File Created.......");
          }
          finally
          {
              fos.close();
          }    
    }
    
}
Exit mobile version