AWT (Abstract Window Toolkit) ব্যবহার করে ফাইল অপারেশন এবং ত্রুটি (error) হ্যান্ডলিং করার জন্য Java-এ কিছু সাধারণ ফাইল অপারেশন যেমন ফাইল পড়া, লেখা, এবং ফাইল পরিচালনা করা হয়। তবে AWT নিজে ফাইল পরিচালনার জন্য সরাসরি কোন API প্রদান করে না; এই কাজের জন্য Java-র java.io প্যাকেজ ব্যবহৃত হয়। নিচে ফাইল অপারেশন এবং ত্রুটি হ্যান্ডলিং নিয়ে কিছু উদাহরণ দেওয়া হল:
ফাইল অপারেশন: ফাইল লেখা এবং পড়া
এখানে আমরা একটি সাধারণ ফাইল লেখা এবং পড়ার উদাহরণ দেখব, যেখানে AWT ব্যবহার করে একটি GUI তৈরি করা হয়েছে এবং ফাইল অপারেশন করা হয়েছে।
ফাইল লেখা (Write to a File)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileWriterExample extends Frame {
private TextArea textArea;
public FileWriterExample() {
setTitle("File Writer Example");
setSize(400, 300);
setLayout(new BorderLayout());
// TextArea for user input
textArea = new TextArea();
add(textArea, BorderLayout.CENTER);
// Button to save content to a file
Button saveButton = new Button("Save to File");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Open file in write mode
FileWriter writer = new FileWriter("output.txt");
writer.write(textArea.getText()); // Write text to file
writer.close(); // Close the file
System.out.println("File saved successfully.");
} catch (IOException ioException) {
System.out.println("Error writing to file: " + ioException.getMessage());
}
}
});
add(saveButton, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new FileWriterExample();
}
}
এখানে কী হচ্ছে:
TextAreaদিয়ে ব্যবহারকারী ইনপুট দিতে পারেন।Buttonক্লিক করলেFileWriterব্যবহার করে ফাইলের মধ্যে লেখা হয়।- যদি কোন ত্রুটি হয়, তবে
IOExceptionহ্যান্ডেল করা হয়।
ফাইল পড়া (Read from a File)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileReaderExample extends Frame {
private TextArea textArea;
public FileReaderExample() {
setTitle("File Reader Example");
setSize(400, 300);
setLayout(new BorderLayout());
// TextArea to display file content
textArea = new TextArea();
add(textArea, BorderLayout.CENTER);
// Button to open and read the file
Button openButton = new Button("Open File");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Open file in read mode
FileReader reader = new FileReader("input.txt");
BufferedReader br = new BufferedReader(reader);
String line;
StringBuilder content = new StringBuilder();
while ((line = br.readLine()) != null) {
content.append(line).append("\n"); // Read line by line
}
textArea.setText(content.toString()); // Display content
br.close(); // Close the reader
} catch (IOException ioException) {
System.out.println("Error reading file: " + ioException.getMessage());
}
}
});
add(openButton, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new FileReaderExample();
}
}
এখানে কী হচ্ছে:
TextAreaএর মধ্যে ফাইলের কন্টেন্ট দেখানো হবে।Buttonক্লিক করলেFileReaderএবংBufferedReaderব্যবহার করে ফাইল পড়া হয়।- যদি কোন ত্রুটি হয়, তবে
IOExceptionহ্যান্ডেল করা হয়।
Error Handling in File Operations
ফাইল অপারেশন চলাকালীন কিছু সাধারণ ত্রুটি হতে পারে, যেমন:
- FileNotFoundException: যখন নির্দিষ্ট ফাইল পাওয়া যায় না।
- IOException: সাধারণ Input/Output ত্রুটি, যেমন ডিস্ক ফিল হয়ে যাওয়া, ফাইল লক হয়ে যাওয়া, ইত্যাদি।
এrror Handling Example
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileErrorHandlingExample extends Frame {
private TextArea textArea;
public FileErrorHandlingExample() {
setTitle("File Error Handling Example");
setSize(400, 300);
setLayout(new BorderLayout());
// TextArea to display file content
textArea = new TextArea();
add(textArea, BorderLayout.CENTER);
// Button to handle file operations
Button processButton = new Button("Process File");
processButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Simulate file reading operation
File file = new File("non_existent_file.txt");
if (!file.exists()) {
throw new FileNotFoundException("The specified file does not exist.");
}
// Simulate file processing logic here
System.out.println("File processing...");
} catch (FileNotFoundException fnfException) {
// Specific exception for file not found
textArea.setText("Error: " + fnfException.getMessage());
System.out.println("File not found: " + fnfException.getMessage());
} catch (IOException ioException) {
// General IO exception handling
textArea.setText("IO Error: " + ioException.getMessage());
System.out.println("IOException: " + ioException.getMessage());
} catch (Exception ex) {
// Catch any other unexpected exceptions
textArea.setText("Unexpected Error: " + ex.getMessage());
System.out.println("Unexpected Error: " + ex.getMessage());
}
}
});
add(processButton, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new FileErrorHandlingExample();
}
}
এখানে কী হচ্ছে:
- FileNotFoundException কাস্টম ত্রুটি দেখানোর জন্য ব্যবহার করা হয়েছে।
- IOException এবং Exception হ্যান্ডলিং করা হয়েছে যাতে অন্যান্য ত্রুটিও সঠিকভাবে ম্যানেজ করা যায়।
ফাইল অপারেশন সংক্রান্ত কিছু সাধারণ ত্রুটি:
- FileNotFoundException: নির্দিষ্ট ফাইল না থাকলে।
- IOException: ইনপুট/আউটপুট ত্রুটি (ফাইল খুলতে বা লেখার সময়)।
- SecurityException: ফাইল অ্যাক্সেসের জন্য নিরাপত্তা ত্রুটি।
এভাবে AWT এবং java.io ক্লাসের সাহায্যে আপনি ফাইল অপারেশন করতে পারেন এবং ত্রুটি হ্যান্ডলিং সম্পন্ন করতে পারেন।
Read more