It's all about Java: September 2025

Sunday, 28 September 2025

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 resource in an operation is not controlled by a different flow of execution at the same time [Even in time slicing operations]

We know, Java is not pure object oriented programming language, due to primitive data types' design behavior.

Most of the Java experts, while designing and developing applications thrives hard, to create, most secured applications considering "Atomicity in multi threaded application".

Does Java language helps in developing Secured application..?

The answer is no, Java programming language does not give secured applications at all, especially in multi threaded environment.

As we know, Java supports 2 types of variables/data types. 
  1. Primitive data types
  2. User defined data types

Primitive data types:      

Java supports 8 different primitive data types. They are: 

  1.  boolean    1 byte
  2.  short        2 bytes
  3.  int            4 bytes
  4.  char         2 bytes
  5.  float         4 bytes
  6.  long         8 bytes
  7.  double     8 bytes
  8.  byte         1 byte

User Defined data types:


In Java, user defined data types are created using "new" operator. For example, to create Employee object, the following instruction is coded

Employee employee = new Employee();

Now, the employee in the above statement, is a reference to an Employee class and called as user defined data type.

Now, coming to the point [Why Java is not secured..?], the primitive data types, long and double are not atomic variables. That means they are not executed in a single operation. 

long variable's size is 64 bits. And it is divided into 32 bits + 32 bits while copying from one memory location to another memory location. and all user defined data types are based on primitive data types.

And these two primitive variables [long and double] breaks the atomicity law. 

And developers can not bypass long and double variables in their program to create application. It is almost impossible to create the application without these variables.

Especially, in multi threaded based application, these non atomic variables may give unpredictable results. 

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 ...