Java I/O (Input/Output) অপারেশন ফাইল, ডিরেক্টরি এবং অন্যান্য ডেটা সোর্সের সাথে যোগাযোগ করতে ব্যবহৃত হয়। Java তে I/O সম্পর্কিত কাজের জন্য java.io প্যাকেজে বিভিন্ন ক্লাস এবং ইন্টারফেস রয়েছে, যেমন File
, FileReader
, BufferedReader
, FileWriter
, PrintWriter
, FileInputStream
, FileOutputStream
, BufferedInputStream
, BufferedOutputStream
ইত্যাদি।
নিচে Java I/O (Input/Output) এর কয়েকটি প্রাথমিক উদাহরণ দেয়া হলো:
এখানে File
ক্লাস ব্যবহার করে একটি ফাইল তৈরি, তথ্য লেখার এবং তথ্য পড়ার উদাহরণ দেখানো হয়েছে।
import java.io.*;
public class FileExample {
public static void main(String[] args) {
File file = new File("example.txt");
// Create a new file if it doesn't exist
try {
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
// Write to the file
FileWriter writer = new FileWriter(file);
writer.write("Hello, Java I/O!");
writer.close();
System.out.println("Data written to the file.");
// Read from the file
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
File created: example.txt
Data written to the file.
Hello, Java I/O!
Scanner
ক্লাস ব্যবহার করে কনসোল থেকে ইনপুট পড়া হয়।
import java.util.Scanner;
public class ConsoleInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + ". You are " + age + " years old.");
}
}
Output:
Enter your name: John
Enter your age: 25
Hello, John. You are 25 years old.
এই উদাহরণে FileInputStream এবং FileOutputStream ব্যবহার করে একটি ফাইল থেকে ডেটা পড়া এবং অন্য একটি ফাইলে লেখা হচ্ছে।
import java.io.*;
public class FileStreamExample {
public static void main(String[] args) {
String data = "This is an example of FileInputStream and FileOutputStream.";
// Write data to file using FileOutputStream
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
byte[] byteData = data.getBytes();
fos.write(byteData);
System.out.println("Data written to output.txt");
} catch (IOException e) {
e.printStackTrace();
}
// Read data from file using FileInputStream
try (FileInputStream fis = new FileInputStream("output.txt")) {
int i;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Data written to output.txt
This is an example of FileInputStream and FileOutputStream.
BufferedReader
এবং BufferedWriter
ব্যবহার করে ফাইল থেকে দ্রুত রিড এবং ফাইলের মধ্যে দ্রুত লেখার উদাহরণ:
import java.io.*;
public class BufferedReaderWriterExample {
public static void main(String[] args) {
// Write to a file using BufferedWriter
try (BufferedWriter writer = new BufferedWriter(new FileWriter("bufferedOutput.txt"))) {
writer.write("This is a test for BufferedWriter.");
writer.newLine();
writer.write("Buffered I/O operations are faster.");
System.out.println("Data written using BufferedWriter.");
} catch (IOException e) {
e.printStackTrace();
}
// Read from the file using BufferedReader
try (BufferedReader reader = new BufferedReader(new FileReader("bufferedOutput.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Data written using BufferedWriter.
This is a test for BufferedWriter.
Buffered I/O operations are faster.
PrintWriter
ক্লাস ব্যবহার করে ফাইল বা কনসোলে লেখার উদাহরণ:
import java.io.*;
public class PrintWriterExample {
public static void main(String[] args) {
// Print to file using PrintWriter
try (PrintWriter writer = new PrintWriter(new FileWriter("printWriterOutput.txt"))) {
writer.println("This is an example using PrintWriter.");
writer.println("It supports easy output formatting.");
System.out.println("Data written using PrintWriter.");
} catch (IOException e) {
e.printStackTrace();
}
// Print to console using PrintWriter
try (PrintWriter writer = new PrintWriter(System.out)) {
writer.println("This is printed to the console using PrintWriter.");
}
}
}
Output:
Data written using PrintWriter.
This is printed to the console using PrintWriter.
Contents of "printWriterOutput.txt":
This is an example using PrintWriter.
It supports easy output formatting.
Java তে অবজেক্ট serialization এবং deserialization করার জন্য ObjectOutputStream
এবং ObjectInputStream
ব্যবহার করা হয়।
import java.io.*;
class Person implements Serializable {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class SerializationExample {
public static void main(String[] args) {
Person person = new Person("John", 30);
// Serialize object to file
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
oos.writeObject(person);
System.out.println("Object serialized to person.ser");
} catch (IOException e) {
e.printStackTrace();
}
// Deserialize object from file
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {
Person deserializedPerson = (Person) ois.readObject();
System.out.println("Deserialized Object: " + deserializedPerson.name + ", " + deserializedPerson.age);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
Object serialized to person.ser
Deserialized Object: John, 30
Java I/O (Input/Output) অপারেশন খুবই গুরুত্বপূর্ণ, কারণ এটি বিভিন্ন ডেটা সোর্স (ফাইল, কনসোল, নেটওয়ার্ক, ডেটাবেস ইত্যাদি) থেকে ডেটা ইনপুট এবং আউটপুট করতে সহায়ক। উপরের উদাহরণগুলি Java I/O এর প্রধান অংশগুলির মধ্যে কিছু সাধারণ এবং গুরুত্বপূর্ণ অপারেশন দেখিয়েছে, যেমন ফাইল পড়া, লেখা, স্ট্রিমিং, এবং অবজেক্ট সিরিয়ালাইজেশন। Java তে I/O অপারেশন সঠিকভাবে করতে হলে, আপনি সঠিক ক্লাস এবং অপারেশনগুলির সঠিক ব্যবহার জানলে পারফরম্যান্স এবং নিরাপত্তা উভয়ই উন্নত হবে।
Java তে File Handling খুবই গুরুত্বপূর্ণ এবং এটি ব্যবহৃত হয় ফাইল তৈরি, ফাইল পড়া এবং ফাইল লেখার জন্য। Java তে java.io প্যাকেজের মাধ্যমে এই কাজগুলো করা হয়। নিচে File Handling সম্পর্কিত কিছু সাধারণ উদাহরণ দেওয়া হলো:
Java তে File
ক্লাস ব্যবহার করে একটি ফাইল তৈরি করা যায়। নিচের উদাহরণে একটি ফাইল তৈরি করা হয়েছে এবং সেই ফাইলটি সেভ করা হয়েছে।
import java.io.*;
public class CreateFileExample {
public static void main(String[] args) {
try {
// Create a file object
File file = new File("example.txt");
// Check if file exists, if not, create a new one
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output:
File created: example.txt
Java তে FileReader এবং BufferedReader ব্যবহার করে একটি ফাইল থেকে ডেটা পড়া হয়। নিচে একটি ফাইল থেকে টেক্সট পড়ার উদাহরণ দেওয়া হলো।
import java.io.*;
public class ReadFileExample {
public static void main(String[] args) {
try {
// Create a FileReader object
FileReader fileReader = new FileReader("example.txt");
// Create a BufferedReader object to read the file
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
// Read the file line by line
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// Close the reader
bufferedReader.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output:
This is a sample file.
Hello, Java!
Java তে FileWriter এবং BufferedWriter ব্যবহার করে ফাইল লিখা হয়। নিচে একটি ফাইলের মধ্যে লেখা লেখার উদাহরণ দেয়া হলো।
import java.io.*;
public class WriteFileExample {
public static void main(String[] args) {
try {
// Create a FileWriter object
FileWriter fileWriter = new FileWriter("example.txt");
// Create a BufferedWriter object
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Write data to the file
bufferedWriter.write("This is a new content in the file.");
bufferedWriter.newLine();
bufferedWriter.write("Adding another line of text.");
// Close the writer
bufferedWriter.close();
System.out.println("Successfully written to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output:
Successfully written to the file.
ফাইলের শেষে নতুন ডেটা যোগ করার জন্য FileWriter এর append
প্যারামিটার ব্যবহার করা যায়।
import java.io.*;
public class AppendToFileExample {
public static void main(String[] args) {
try {
// Open the file in append mode
FileWriter fileWriter = new FileWriter("example.txt", true);
// Create a BufferedWriter object
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Append data to the file
bufferedWriter.write("This is appended text.");
bufferedWriter.newLine();
// Close the writer
bufferedWriter.close();
System.out.println("Successfully appended to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output:
Successfully appended to the file.
ফাইলটি উপস্থিত কিনা তা চেক করার জন্য exists() মেথড ব্যবহার করা হয়।
import java.io.*;
public class FileExistsExample {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.exists()) {
System.out.println("File exists.");
} else {
System.out.println("File does not exist.");
}
}
}
Output:
File exists.
ফাইল মুছে ফেলার জন্য delete() মেথড ব্যবহার করা হয়।
import java.io.*;
public class DeleteFileExample {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.delete()) {
System.out.println("File deleted successfully.");
} else {
System.out.println("Failed to delete the file.");
}
}
}
Output:
File deleted successfully.
ফাইলের প্রপার্টি যেমন সাইজ, পাথ ইত্যাদি বের করার জন্য File ক্লাসের বিভিন্ন মেথড ব্যবহার করা হয়।
import java.io.*;
public class FilePropertiesExample {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.exists()) {
System.out.println("File Path: " + file.getAbsolutePath());
System.out.println("File Size: " + file.length() + " bytes");
System.out.println("File Readable: " + file.canRead());
System.out.println("File Writable: " + file.canWrite());
} else {
System.out.println("File does not exist.");
}
}
}
Output:
File Path: C:\path\to\your\file\example.txt
File Size: 45 bytes
File Readable: true
File Writable: true
Java তে File Handling একটি গুরুত্বপূর্ণ ফিচার এবং java.io প্যাকেজের File, FileReader, BufferedReader, FileWriter, BufferedWriter ইত্যাদি ক্লাসগুলি ব্যবহার করে ফাইল তৈরি, পড়া, লেখা, মুছে ফেলা, এবং ফাইলের প্রপার্টি পাওয়া যায়। এগুলির সাহায্যে আপনি সহজে ফাইলের উপর অপারেশন করতে পারেন।
Java BufferedReader এবং BufferedWriter হল I/O ক্লাস যা ফাইলের সাথে কার্যকরীভাবে কাজ করতে ব্যবহৃত হয়, বিশেষত যখন বড় পরিমাণে ডেটা পড়া বা লেখা হয়। এই ক্লাসগুলি buffered streams এর অংশ, যার মাধ্যমে ডেটা দ্রুত এবং কম মেমরিতে সঞ্চালিত হয়। BufferedReader সাধারণত character-based input stream এর জন্য ব্যবহৃত হয় এবং BufferedWriter character-based output stream এর জন্য ব্যবহৃত হয়।
BufferedReader এবং BufferedWriter ব্যবহারের মাধ্যমে আপনি ফাইল থেকে দ্রুত পাঠ এবং দ্রুত লেখার কাজ করতে পারেন, কারণ এই ক্লাসগুলি buffering প্রযুক্তি ব্যবহার করে।
এখানে একটি উদাহরণ দেওয়া হলো যেখানে একটি ফাইল থেকে BufferedReader ব্যবহার করে লাইনে লাইনে ডেটা পড়া হয়েছে।
import java.io.*;
public class BufferedReaderExample {
public static void main(String[] args) {
// Input file path
String inputFile = "input.txt";
// Reading file using BufferedReader
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
// Read file line by line
while ((line = br.readLine()) != null) {
System.out.println(line); // Printing each line
}
} catch (IOException e) {
e.printStackTrace(); // Handling IOExceptions
}
}
}
BufferedReader
দিয়ে readLine() মেথড ব্যবহার করে লাইনে লাইনে ফাইলের ডেটা রিড করা হয়। এটি প্রতিটি লাইনে একটি স্ট্রিং রিটার্ন করে।FileReader
ব্যবহার করা হয়।try-with-resources
ব্যবহার করা হয় যাতে ফাইল বা স্ট্রিম ক্লোজ করা নিশ্চিত করা যায়।Hello, this is a test file.
Java BufferedReader example.
Efficient file reading with buffering.
এখানে একটি উদাহরণ দেওয়া হলো যেখানে BufferedWriter ব্যবহার করে একটি ফাইলে ডেটা লেখা হয়েছে।
import java.io.*;
public class BufferedWriterExample {
public static void main(String[] args) {
// Output file path
String outputFile = "output.txt";
// Writing to file using BufferedWriter
try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
String[] lines = {
"Hello, this is a test file.",
"Java BufferedWriter example.",
"Efficient file writing with buffering."
};
// Writing lines to the file
for (String line : lines) {
bw.write(line);
bw.newLine(); // Writing a new line after each string
}
System.out.println("File written successfully.");
} catch (IOException e) {
e.printStackTrace(); // Handling IOExceptions
}
}
}
BufferedWriter
ব্যবহার করে write() মেথড দিয়ে ফাইলে ডেটা লেখা হয়। newLine()
মেথড দিয়ে একটি নতুন লাইন অ্যাড করা হয়।FileWriter
ব্যবহার করা হয়েছে, যেটি ফাইলে ডেটা লেখার জন্য BufferedWriter
কে ইনিশিয়ালাইজ করে।try-with-resources
ব্যবহার করা হয়েছে যাতে ফাইল লেখার পরে স্ট্রিম বন্ধ হয়ে যায়।The contents of output.txt
would be:
Hello, this is a test file.
Java BufferedWriter example.
Efficient file writing with buffering.
এখানে একটি উদাহরণ দেয়া হলো যেখানে BufferedReader এবং BufferedWriter ব্যবহার করে একটি ফাইলের ডেটা আরেকটি ফাইলে কপি করা হচ্ছে।
import java.io.*;
public class FileCopyExample {
public static void main(String[] args) {
String inputFile = "input.txt"; // Input file
String outputFile = "output.txt"; // Output file
try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
String line;
// Read from input file and write to output file line by line
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine(); // Adding a new line after each line copied
}
System.out.println("File copied successfully.");
} catch (IOException e) {
e.printStackTrace(); // Handling IOExceptions
}
}
}
যদি input.txt
ফাইলের মধ্যে এই ডেটা থাকে:
This is the first line.
This is the second line.
This is the third line.
তাহলে output.txt
ফাইলের মধ্যে একই ডেটা কপি হবে:
This is the first line.
This is the second line.
This is the third line.
BufferedReader
এবং BufferedWriter
ব্যবহার করার মাধ্যমে I/O অপারেশনগুলি buffered করা হয়, যার ফলে I/O অপারেশন অনেক দ্রুত হয়।BufferedReader
এবং BufferedWriter
স্বয়ংক্রিয়ভাবে স্ট্রিম পরিচালনা করতে সহায়তা করে, যেমন ফাইল ক্লোজ করা, যা ম্যানুয়ালি করতে প্রয়োজন হয় না।Java তে FileInputStream এবং FileOutputStream ক্লাস দুটি binary data (যেমন: ছবি, অডিও ফাইল, ভিডিও ফাইল, ইত্যাদি) পড়তে এবং লেখার জন্য ব্যবহৃত হয়। এই ক্লাসগুলো byte-oriented streams হিসেবে কাজ করে, যা byte আকারে ডেটা পরিচালনা করে এবং character-based streams এর মতো text data না, বরং binary data এক্সেস করে।
এই উদাহরণে আমরা একটি ফাইল থেকে বাইনারি ডেটা রিড করব এবং সেই ডেটা অন্য একটি ফাইলে লিখে দেব।
import java.io.*;
public class FileCopyExample {
public static void main(String[] args) {
// Source and Destination file paths
String sourceFile = "sourceFile.jpg"; // Replace with the path of your source file
String destFile = "destinationFile.jpg"; // Replace with the path of your destination file
// Reading from the source file and writing to the destination file
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destFile)) {
byte[] buffer = new byte[1024]; // Buffer size to read/write 1KB at a time
int bytesRead;
// Read data from source file and write it to destination file
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead); // Write the read bytes to destination file
}
System.out.println("File copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream fis = new FileInputStream(sourceFile);
→ এটি sourceFile ফাইল থেকে বাইনারি ডেটা রিড করতে ব্যবহৃত হয়।FileOutputStream fos = new FileOutputStream(destFile);
→ এটি destinationFile ফাইলে বাইনারি ডেটা লেখার জন্য ব্যবহৃত হয়।byte[] buffer = new byte[1024];
→ এখানে একটি বাফার তৈরি করা হয়েছে যাতে প্রতি বার ১ কিলোবাইট ডেটা রিড বা রাইট করা যায়। এটি I/O অপারেশনগুলোকে দ্রুততর করে তোলে।fis.read(buffer)
→ ফাইল থেকে ডেটা রিড করতে বাফার ব্যবহার করা হচ্ছে।fos.write(buffer, 0, bytesRead)
→ রিড করা ডেটাকে ডেস্টিনেশন ফাইলে লেখার জন্য বাফার ব্যবহার করা হচ্ছে।IOException e
→ I/O অপারেশনগুলিতে যদি কোনো সমস্যা হয়, যেমন ফাইল না পাওয়া, পারমিশন সমস্যা ইত্যাদি, তবে তা catch ব্লকে ধরা হবে।try (FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(destFile))
→ এখানে try-with-resources ব্যবহৃত হয়েছে যাতে ফাইল স্ট্রিমগুলো স্বয়ংক্রিয়ভাবে বন্ধ হয়, কোনো সমস্যা ছাড়াই। এটি Java 7 থেকে প্রবর্তিত।File copied successfully!
এটি একটি সাধারণ binary file copy operation, যেখানে একটি বাইনারি ফাইল (যেমন একটি ছবি) রিড করা হয়েছে এবং তার কন্টেন্ট অন্য একটি ফাইলে লেখা হয়েছে।
FileReader
বা BufferedReader
) এর থেকে আলাদা।IOException
সাধারণত ফাইল পরিচালনার ক্ষেত্রে বিভিন্ন সমস্যা (যেমন ফাইল পাওয়া না যাওয়া, রাইট পারমিশন না থাকা) ঘটলে থ্রো করা হয়।Serialization এবং Deserialization হল দুটি গুরুত্বপূর্ণ প্রক্রিয়া যা Java তে অবজেক্ট ডেটাকে সংরক্ষণ এবং পুনরুদ্ধারের জন্য ব্যবহৃত হয়।
Java তে Serialization এর জন্য Serializable
ইন্টারফেস এবং Deserialization এর জন্য ObjectInputStream
ব্যবহার করা হয়।
প্রথমে একটি ক্লাস তৈরি করতে হবে যেটি Serializable হবে, অর্থাৎ সেই ক্লাসের অবজেক্টকে সিরিয়ালাইজ করা যাবে। ক্লাসে Serializable
ইন্টারফেস ইমপ্লিমেন্ট করতে হয়।
import java.io.*;
// Class must implement Serializable interface to be serialized
class Person implements Serializable {
private static final long serialVersionUID = 1L; // Serialization version ID
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
// ToString Method
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}
Explanation:
Person
ক্লাস Serializable
ইন্টারফেসটি ইমপ্লিমেন্ট করেছে, তাই এর অবজেক্টটি সিরিয়ালাইজ করা যাবে।serialVersionUID
একটি ইউনিক আইডি যা সিরিয়ালাইজড অবজেক্টের সংস্করণ চিহ্নিত করে। এটি সুনির্দিষ্টভাবে দেওয়া হলে, পরবর্তীতে যে কোনো সমস্যা ছাড়াই ডেসিরিয়ালাইজ করা যাবে।এখন আমরা Person
অবজেক্ট সিরিয়ালাইজ করব এবং একটি ফাইলে লিখব।
import java.io.*;
public class SerializationExample {
public static void main(String[] args) {
// Creating a Person object
Person person = new Person("Alice", 30);
// Serialize the person object to a file
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
out.writeObject(person);
System.out.println("Object has been serialized.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
person
অবজেক্টটি একটি ফাইলে (এখানে person.ser
) সিরিয়ালাইজ করতে।writeObject()
মেথড ব্যবহার করা হয়েছে।try-with-resources
ব্যবহার করে স্বয়ংক্রিয়ভাবে স্ট্রিম বন্ধ করা হচ্ছে।এখন আমরা সেই person.ser
ফাইল থেকে অবজেক্টটি ডেসিরিয়ালাইজ (পুনরুদ্ধার) করব এবং পুনরায় একটি Person
অবজেক্টে রূপান্তরিত করব।
import java.io.*;
public class DeserializationExample {
public static void main(String[] args) {
// Deserialize the person object from the file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) {
Person person = (Person) in.readObject();
System.out.println("Object has been deserialized.");
System.out.println(person); // Print the deserialized object
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Explanation:
person.ser
ফাইল থেকে অবজেক্টটি ডেসিরিয়ালাইজ করতে।System.out.println()
ব্যবহার করে সেটির মান প্রিন্ট করেছি।Person
অবজেক্টটি তৈরি করা হয় এবং ObjectOutputStream
এর মাধ্যমে person.ser
ফাইলে সিরিয়ালাইজ করা হয়।ObjectInputStream
ব্যবহার করে person.ser
ফাইল থেকে অবজেক্টটি ডেসিরিয়ালাইজ করা হয় এবং আবার Person
অবজেক্টে পুনরুদ্ধার করা হয়।Serialization Output:
Object has been serialized.
Deserialization Output:
Object has been deserialized.
Person{name='Alice', age=30}
Transient Keyword:
transient
কিওয়ার্ড ব্যবহার করতে পারেন। এই ফিল্ডটি সিরিয়ালাইজেশন প্রক্রিয়ায় উপেক্ষা করা হবে।Example:
private transient int password; // This will not be serialized
ObjectOutputStream
ও ObjectInputStream
ব্যবহার করে আপনি সহজেই অবজেক্টকে ফাইলে সেভ এবং পুনরুদ্ধার করতে পারবেন।Java Directory Traversing হল একটি প্রক্রিয়া যার মাধ্যমে একটি নির্দিষ্ট ডিরেক্টরির মধ্যে থাকা সব ফাইল এবং সাবডিরেক্টরি গুলি একে একে এক্সপ্লোর করা হয়। Directory Traversing সাধারণত একটি ডিরেক্টরি স্ট্রাকচার যাচাই করার জন্য ব্যবহৃত হয়, এবং Java NIO (New I/O) অথবা File API ব্যবহার করে এটি সহজে করা যায়।
নিচে একটি Java Directory Traversing Example দেয়া হলো, যা একটি নির্দিষ্ট ডিরেক্টরির মধ্যে থাকা সমস্ত ফাইল এবং সাবডিরেক্টরির নামগুলি প্রিন্ট করবে।
File
Class:import java.io.File;
public class DirectoryTraversingExample {
public static void main(String[] args) {
// ডিরেক্টরি পাথ
String directoryPath = "path_to_your_directory"; // এখানে আপনার ডিরেক্টরি পাথ দিবেন
// ডিরেক্টরি অবজেক্ট তৈরি
File directory = new File(directoryPath);
// যদি এটি একটি ডিরেক্টরি হয়
if (directory.isDirectory()) {
// ডিরেক্টরি থেকে ফাইল ও সাবডিরেক্টরি গুলোর লিস্ট পাওয়া
String[] fileList = directory.list();
// ফাইলের নাম প্রিন্ট করা
System.out.println("Files and directories in " + directoryPath + ":");
for (String fileName : fileList) {
System.out.println(fileName);
}
} else {
System.out.println("The provided path is not a directory.");
}
}
}
Explanation:
File
ক্লাসের list()
মেথড ব্যবহার করা হয়েছে যা ডিরেক্টরির মধ্যে থাকা সকল ফাইল এবং সাবডিরেক্টরি গুলির নাম ফেরত দেয়।isDirectory()
মেথডটি চেক করে যে পাথটি একটি ডিরেক্টরি কিনা।Output:
Files and directories in path_to_your_directory:
file1.txt
file2.pdf
subdir1
subdir2
Files
and Paths
(NIO):Java 7 এ NIO (New I/O) এ কিছু নতুন ফিচার যোগ করা হয়েছে, যা ডিরেক্টরি ট্রাভার্সিং আরও সহজ এবং কার্যকরী করে। Files
এবং Paths
ক্লাস ব্যবহার করে ডিরেক্টরি ট্রাভার্স করা যায়।
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
public class DirectoryTraversingExample {
public static void main(String[] args) {
// ডিরেক্টরি পাথ
Path path = Paths.get("path_to_your_directory"); // এখানে আপনার ডিরেক্টরি পাথ দিবেন
try {
// Files.walkFileTree ব্যবহার করে ডিরেক্টরি ট্রাভার্স করা
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// ফাইলের পাথ প্রিন্ট করা
System.out.println(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
// ত্রুটির জন্য মেসেজ প্রিন্ট করা
System.out.println("Failed to visit file: " + file);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
Files.walkFileTree()
: এটি একটি রিকার্সিভ পদ্ধতি যা ডিরেক্টরি এবং সাবডিরেক্টরিগুলির মধ্যে থাকা সমস্ত ফাইলের পাথ ট্রাভার্স করে।SimpleFileVisitor
: এটি একটি FileVisitor
ইন্টারফেসের এক বাস্তবায়ন, যা visitFile
মেথডের মাধ্যমে ফাইলের পাথকে প্রসেস করে।Output:
path_to_your_directory/file1.txt
path_to_your_directory/file2.pdf
path_to_your_directory/subdir1/file3.txt
path_to_your_directory/subdir2/file4.txt
File
class: এটি পুরনো Java I/O API, যেখানে কিছু ফিচার সীমাবদ্ধ থাকে এবং কিছু পদ্ধতি অনেকটা আউটডেটেড হতে পারে।Files
and Paths
(NIO): এটি Java 7 এর পরবর্তী নতুন I/O লাইব্রেরি, যা অনেক বেশি কার্যকরী, দ্রুত এবং উন্নত ফিচার প্রদান করে। এছাড়াও, এটি রিকার্সিভ ট্রাভার্সিং এবং বড় ফাইল সিস্টেমের জন্য আরও কার্যকরী।File
class) সাধারণত ছোট ফাইল সিস্টেমের জন্য উপযুক্ত, যেখানে আপনি এক্সপ্লোর করতে চান একটি ডিরেক্টরি এবং তার মধ্যে থাকা ফাইল এবং ডিরেক্টরির নাম।Files
, Paths
) অনেক বেশি দক্ষ এবং রিকার্সিভ ট্রাভার্সিং এবং বড় ফাইল সিস্টেমের জন্য উপযুক্ত।Read more