Skip to main content

Java File Handling basics Code Implementation


Java Contains large file handling concepts to work on the files available and creating , modification , writing , reading etc operations . we are posting here some very conceptual file handling programs using java .

I was working on a project and needed to modify a file through java , then i collected these codes .
It can help to java developers and Computer Students in their college projects .

-----------------------------------
// Java Code to create New Directory

import java.io.File ;
import java.io.IOException ;
public class CreateDir
{
public static void main(String []args)
{

File f=new File("C:/Users/UserName/Desktop/Create");
if(f.mkdir())
System.out.println("Success");
else
System.out.println("Error , File already exists");

}
}
----------------------------------
// Java Code to create a new File

import java.io.File ;
import java.io.IOException ;
public class CreateFile
{
public static void main(String []args)
{
String s="C:/Users/UserName/Desktop/CreateNewFile.txt" ;
try
{
File f=new File(s);
if(f.createNewFile())
System.out.println("Success");
else
System.out.println("Error , File already exists");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
----------------------------------
// Java Code for deleting a File

import java.io.File ;
import java.io.IOException ;
public class Delete
{
public static void main(String []args)
{

File f=new File("C:/Users/UserName/Desktop/CreateNewFile.txt");
if(f.delete())
System.out.println("Success");
else
System.out.println("Error , File does not exist");

}
}
----------------------------------
// Java Code to check last modified file

import java.io.File ;
import java.util.Date;
public class LastModified
{
public static void main(String []args)
{

File f=new File("C:/Users/UserName/Desktop/CreateNewFile.txt");
long modified=f.lastModified();
System.out.println("The File "+f.getPath()+" was last modified at : "+ new Date(modified));

}
}
----------------------------------
// Java Code to List the content of any Directory

import java.io.File ;
import java.io.IOException ;
public class List
{
public static void main(String []args)
{

File f=new File("D:/software/studySoftware");
String[] files=f.list() ;
System.out.println("Listing contents of "+f.getPath());
for(int i=0;i<files.length;i++)
{
System.out.println(files[i]);
}
}
}
----------------------------------
// Java Code to open a File at Runtime

import java.io.* ;
import java.util.*;
public class Note
{
public static void main(String []args)
{

Runtime rs=Runtime.getRuntime() ;
try
{
rs.exec("notepad");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
----------------------------------
// Java Code to Rename a File

import java.io.File ;
import java.io.IOException ;
public class Rename
{
public static void main(String []args)
{

File oldfile=new File("C:/Users/UserName/Desktop/CreateNewFIle.txt");
File newfile=new File("C:/Users/UserName/Desktop/CreateUserFile.txt");
if(oldfile.renameTo(newfile))
System.out.println("Success");
else
System.out.println("Error , File already exists or Not Found ..");

}
}
----------------------------------

Comments