mathajax

Java File Write Operation

The Java program explains how to write text content into a new file on the disk using the FileOutputStream object which has different write functions operation.

The following basic file operation used in this program.
File Create

File write operation in Java is done while creating an instance/object to the FileOutputStream by invoking constructor function with filepath a as argument. If the file is already found on specified file path, The FileOutputStream object truncates content of the file.
File Write and close

After creating an instance of the FileOutputStream class, it supports file write and close operation and also supports three ways of writing bytes into the file. They are, write single byte, write the array of bytes with its index and length and write all bytes in an array into the file

The Java program is an example of write a byte into the file. The byte is read from user through keyboard.


import java.io.FileOutputStream;
import java.io.IOException;

public class FileWrite {

 public static void main(String[] args) throws IOException {
  
  String fpath = "c://test2.txt";
  FileOutputStream  fos=new FileOutputStream(fpath);  

  System.out.println("File Write Operation \n");
  System.out.println("User keyboard typing char written 
                     into the file ");
  System.out.println("Stop write operation enter 0 ");
 
  int b=0;
  
  while ( (b=System.in.read() ) !='0' ) 
  {            
      fos.write(b);      
  }   
  fos.close();
  System.out.println("File Write finished");

 }
}


Output
File Write Operation 

 User keyboard typing char written into the file 
 Stop write operation enter 0 
i am a java programmer 
0
File Write finished




The Java program is an example of write a line of bytes into the file. These line of bytes are read from user through keyboard.

The System.in read function read all typed line of characters ,stored into byte array variable bs and return how many bytes read from keyboard while enterkey pressed. Then, the stored array of bytes written into the file using write function.


import java.io.FileOutputStream;
import java.io.IOException;

public class FileWrite2 {

 public static void main(String[] args) throws IOException {
  
  String fpath = "c://test.txt";
  FileOutputStream  fos=new FileOutputStream(fpath);
  
  System.out.println(" File Write Operation \n");
  System.out.println(" User keyboard typing line of chars 
                      written into file ");
  System.out.println(" Stop write operation enter ^z \n");

  byte bs[]=new byte[64];
  
  while ( true ) 
  {
   int s=System.in.read(bs);      
   if (  bs[0]=='^' && bs[1]=='z' )
      break;      
   fos.write(bs,0,s);  
  }     
  fos.close();
  System.out.println("\n File Write Finished ");
 }
}


Output
 File Write Operation 

 User keyboard typing line of chars written into file 
 Stop write operation enter ^z 

i am a java programmer 
and writing a simple java code into my blogger
^z

 File Write Finished 


The Java program is an example copying a file by writing array of bytes into the file by single write operation. These bytes are read from FileInputStream object. The content of source file test.txt is copied into test2.txt.


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileWrite3 {

 public static void main(String[] args) throws IOException {
  
  String fpath = "c://test.txt";
  String fpath2 = "c://test2.txt";
  
  FileOutputStream  fos=new FileOutputStream(fpath2);
    
  FileInputStream  fis=new FileInputStream(fpath);          
  int filesize=fis.available();
  byte barray[]=new byte[filesize];  
  fis.read(barray);
  
  fos.write(barray);
  
  fis.close();  
  fos.close();

 }
}


Output
File Coping Finished



Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement