It's all about Java: file
Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Saturday, 20 September 2025

File IO operations with java programming language

File Management in OS

File management includes files and folders creation, copy and paste operations, reading the file and writing into files and deleting the files and folders 

And also reading meta data about files and folders. It may include file free space, occupied space and read/write permissions etc.

These above operations are considered as file management and managed by respective operating system.

GUI that represents Windows File Manager



File Management with Java 


Though Java is platform independent, programming language depends on native file IO resources of operating system.

This is possible with the Java API support. These API are categorized into 2 types.

  1. Readers and Writers: Readers and writers does the IO operations, character by character
  2. InputStream and OutputStream: Where as InputStream and OutputStream does IO operations byte by byte

Below are simple Java programs that demonstrates different File IO operations.

Program for creating directory

package com.allabtjava.fileio;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Path;


public class FileManager {


public FileManager() {


}


public void makeDirectory(String path, String dirName) {

Path dirPath = Path.of(path, dirName);

File dir = new File(dirPath.toString());

boolean isCreated = dir.mkdir();

if (isCreated)

System.out.println("A new directory with the name " + dir.getPath() + " created!");

}

        public static void main(String args[]) throws IOException {

FileManager manager = new FileManager();

manager.makeDirectory("e:\\", "students");

}

}



Program for creating file:

package com.allabtjava.fileio;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Path;


public class FileManager {


public FileManager() {


}


public void createFile(String dirPath, String dirName, String fileName) throws IOException {

Path filePath = Path.of(dirPath, dirName, fileName);

File file = new File(filePath.toString());

boolean isFileCreated = file.createNewFile();

if (isFileCreated)

System.out.println("A new file with the name " + file.getPath() + " created!");

}

        public static void main(String args[]) throws IOException {

FileManager manager = new FileManager();

manager.createFile("e:\\", "students", "student_1");

}

}


Listing all files available in given folder

package com.allabtjava.fileio;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Path;


public class FileManager {


public FileManager() {


}

     public void listFiles(String dirPath, String dirName) {
Path dir = Path.of(dirPath, dirName);
File file = new File(dir.toString());
String[] listOfAllFiles = file.list();
System.out.println("List of all Files in the directory/folder - " + dirName + ": ");
for (String fileName : listOfAllFiles) {
System.out.println(fileName);
}
}


        public static void main(String args[]) throws IOException {

FileManager manager = new FileManager();

manager.listFiles("e:\\", "pinterest");

}


}



Delete a directory

package com.allabtjava.fileio;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Path;


public class FileManager {


public FileManager() {


}

     public void deleteDirectory(String dirPath, String dirName) {

Path dir = Path.of(dirPath, dirName);

File dirToBeDeleted = new File(dir.toString());

if (dirToBeDeleted.exists()) {

boolean isDeleted = dirToBeDeleted.delete();

if(isDeleted)

System.out.println("File deleted!");

else

System.out.println("Could not delete file. File is being used by other program or application.");

}

}


        public static void main(String args[]) throws IOException {

FileManager manager = new FileManager();

manager.deleteDirectory("e:\\", "pinterest");

}


}



Read a file content


package com.allabtjava.fileio;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Path;


public class FileManager {


public FileManager() {


}

     public void readFile(String dirPath, String dirName, String fileName) throws IOException {

Path filePath = Path.of(dirPath, dirName, fileName);

File file = new File(filePath.toString());

FileReader reader = new FileReader(file.getAbsolutePath());

BufferedReader bufferedRead = new BufferedReader(reader);

String line = null;

while((line = bufferedRead.readLine()) != null) {

System.out.println(line);

}

bufferedRead.close();

}


        public static void main(String args[]) throws IOException {

FileManager manager = new FileManager();

manager.readFile("e:\\", "pinterest", "demo.txt");

}


}


Write content into file



package com.allabtjava.fileio;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.file.Path;


public class FileManager {


public FileManager() {


}

     public void writeToFile(String dirPath, String dirName, String fileName) throws IOException {

Path filePath = Path.of(dirPath, dirName, fileName);

File file = new File(filePath.toString());

FileWriter writer = new FileWriter(file);

BufferedWriter bufferedWriter = new BufferedWriter(writer);

bufferedWriter.write("allabtjava.com is a web site, has information about java technologies \n");

bufferedWriter.write("This article explains about, files and IO management with java programming language.");

bufferedWriter.close();

}


        public static void main(String args[]) throws IOException {

FileManager manager = new FileManager();

manager.writeToFile("e:\\", "pinterest", "demo.txt");

}


}

Popular posts

Atomicity with Java Programming Language

 Atomicity with Java What is Atomicity Atomicity, in computer science, is considered to be a property [ALL-OR-NOTHING], that the state of a ...