How to Create, Write/Append, Read, Delete and Rename a file in Java?

 



We can see in all applications and programs we may need to write or edit file. File operations like creating, deleting, append etc., So in this tutorial we will see about Create, Write, Read and Delete a file using Java code. Each file operations we will discuss with separate program and lets see the output. 
NOTE: File name, path and folder names can vary accordingly. 


1. How to create a file in Java?


public class CreateFile {
    public static void main(String[] args) {
        File myFile = new File("javadiscover.txt");
        try{
        if(!(myFile.exists())){ // checking file exist or not
            myFile.createNewFile(); // Creating new file
            System.out.println("New File created....");
        }else{
            System.out.println("File already exisit....");
        }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}



OUTPUT:


New File created....




2. How to Write/ Append a text into a file in Java ?

public class WriteFile {
    public static void main(String[] args) {
        
        FileWriter fWrite = null;
        BufferedWriter bWrite = null;
        
        String content = "Hi All, Welcome to Java Discover";
        File myFile = new File("javadiscover.txt");
        
        try{
            if(!(myFile.exists())){
                myFile.createNewFile();
        }
        fWrite = new FileWriter(myFile, true); // true for appending content to the existing file
            bWrite = new BufferedWriter(fWrite);
            bWrite.write(content);
            bWrite.close();
            
            System.out.println("File write complete.....");
            
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fWrite != null
                try { fWrite.close(); } catch (IOException e) { e.printStackTrace(); }
            if(bWrite != null
                try { bWrite.close(); } catch (IOException e) { e.printStackTrace(); }            
        }
    }
}



OUTPUT:

Console

File write complete.....


File


3. How to read a file in Java ?


public class ReadFile {
    public static void main(String[] args) {
        BufferedReader br = null;
        try{
            FileReader myFile  = new FileReader("javadiscover.txt");
            br = new BufferedReader(myFile);
            String line = null
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(br != null
                try{ br.close(); }catch(IOException e){e.printStackTrace();}
        }        
    }
}


OUTPUT:


Hi All, Welcome to Java Discover
Blog about Java questions and answers


4. How to delete a file in Java ?



public class DeleteFile {
    public static void main(String[] args) {
        try{
            File myFile = new File("javadiscover.txt");
            if(myFile.exists()){
                myFile.delete();
                System.out.println("File deleted successfully....");
            }else{
                System.out.println("File NOT Exisit....");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}



OUTPUT:


File deleted successfully....





5. How to rename a file in Java ?

public class RenameFile {
    public static void main(String[] args) {
        File oriFile = new File("java.txt");
        File newFile = new File("javadiscover.txt");
        if(oriFile.exists()){
            oriFile.renameTo(newFile);
            System.out.println("File rename completed....");
        }else{
            System.out.println("Original file not exist for renaming....");
        }
    }
}



OUTPUT:


File rename completed....





Apart from these examples various file operations which you can refer to Oracle docs.







 

No comments:
Write comments