Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

File Handling in Java

File is a collection of bytes stored in secondary storage device i.e. disk. Thus, File handling is used to read, write, append or update a file without directly opening it.
Types of File:

  • Text File
  • Binary File


Text File contains only textual data. Text files may be saved in either a plain text (.TXT) format and rich text (.RTF) format like files in our Notepad.
Binary Files contains both textual data and custom binary data like font size, text color and text style etc.

Java File Handling program to input data to an existing file, else it will create a new one and adds contents in it. [following program demonstrate the same]
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
 
public class FileApp 
{
public static void main( String args[] )
 {  

try
{
String data = " 1234567890 hello to java file";
 File file =new File("temp.txt");

 if(!file.exists())
{
file.createNewFile();
}
 
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.close();
System.out.println("Done");
}
catch(IOException e){ }
}
}

Java File Handling program to delete an existing file stored on the disk.
import java.io.File;
public class FileDel
{
public static void main(String[] args)
{   
try
{
File file = new File("c:\\temp.txt");
if(file.delete())
{
System.out.println(file.getName() + " is deleted!");
}
else
{
System.out.println("Delete operation is failed.");
}
}
catch(Exception e){ e.printStackTrace(); 
}
}
}

Java File Handling program to test whether the file or folder exists or not in the specified location.
import java.io.*;
public class FileExt
{
public static void main(String args[])
{
File file=new File("C:/temp.txt");
boolean exists = file.exists();
if(exists) 
{
System.out.println("File or Directory exist.");
}
else
{
System.out.println("File or Directory does not exists.");
}
}
}

Java File Handling program to read contents from the existing file.
import java.io.*;
class FileRead 
{
public static void main(String args[])
{
try
{
FileInputStream fstream = new FileInputStream("temp5.txt");

DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

  String strLine;
  while ((strLine = br.readLine()) != null) 
{
System.out.println (strLine);
}

 in.close();
  }catch (Exception e){
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Java File Handling program to check Existing FILE SIZE
import java.io.*;
public class FileSize 
{
public static void main(String args[])
{
File file = new File("guru_cv1.doc");
long filesize = file.length();
long filesizeInKB = filesize / 1024;
System.out.println("Size of File is: "  + filesizeInKB + " KB");
}
}


Java File Handling program to input data into an existing file.
import java.io.*;
class FileTest
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("temp5.txt");
DataInputStream in=new DataInputStream(System.in);
String s;
System.out.print("Enter any string:");
s=in.readLine();
byte b[]=s.getBytes();
fout.write(b) ;
fout.close();
System.out.println("Data was successfully entered into the file!");
}
catch(Exception e) {}
}
}

<< Java basics 

No comments:

Post a Comment