Java Files
Move data across the program boundary.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Files.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
Files are external resources and their operations can fail. The java.nio.file API works with Path values and provides convenient text methods. This example writes a temporary file in the execution environment, reads it, and removes it; it does not write a file on the reader computer.
விரிவான விளக்கங்கள்
Java Files என்னன்னு தெரியுமா?
Java-ல files-ஐ create, read, write, delete பண்றதுக்கு java.io package use பண்றோம்.
File Create பண்றது
File class-ஐ use பண்ணி file create பண்றோம்:
File Write பண்றது
FileWriter use பண்ணி file-ல write பண்றோம்:
File Read பண்றது
Scanner use பண்ணி file-ஐ read பண்றோம்:
File Delete பண்றது
delete() method use பண்ணி file delete பண்றோம்:
File operations-ல always IOException handle பண்றது important — try-catch பயன்படுத்தலாம்.
Modern File I/O with java.nio.file.Files
import java.nio.file.*;
Path path = Path.of("demo.txt");
// Write
Files.writeString(path, "Hello, NIO!");
// Read all content
String content = Files.readString(path);
System.out.println(content);
// Read all lines
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
// Check existence
boolean exists = Files.exists(path);
// Delete
Files.deleteIfExists(path);| Approach | API | Best for |
|---|---|---|
| Classic streams | FileReader, BufferedReader | Java 7 and earlier, character streams |
| NIO.2 (recommended) | Files, Path | Modern code, cleaner API, better error handling |
| Scanner | Scanner | Simple line-by-line reading, user input |
Java-வில் file operations-க்கு modern best practice என்ன?
- FileInputStream directly use செய்வது
- try-with-resources பயன்படுத்துவது — automatic resource closing
- finally block-ல் close செய்வது
- Static methods பயன்படுத்துவது
Reveal answer / விடை
try-with-resources பயன்படுத்துவது — automatic resource closing
try-with-resources (`try (BufferedReader br = new BufferedReader(…)) {}`) automatically close() call செய்யும் — exception வந்தாலும். Resource leak avoid செய்வதற்கான modern Java best practice.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
Path file = Files.createTempFile("java-practice-", ".txt");
try {
Files.writeString(file, "Learning Java");
System.out.println(Files.readString(file));
} finally {
Files.deleteIfExists(file);
}
}
}Predict first. Then reveal the expected output / வெளியீடு
Learning Java
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Change the text being written and confirm the same text is read back.
Reveal the answer and reasoning / விடை
writeString stores the supplied text and readString retrieves it. The finally block attempts cleanup even if reading fails.
தவிர்க்க வேண்டிய தவறு: Do not assume a relative path points where you expect. Its meaning depends on the process working directory.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.