NAME : JASON MACHARIA KARIUKI
REG NO : CT100/G/14130/21
1.DISCUSS ABOUT FILE AND ITS IMPLEMENTATIONS AS USED IN JAVA
PROGRAMMING .
In Java programming, file handling is a crucial aspect that involves reading from
and writing to files. Java provides a comprehensive set of classes and APIs in the
java.io and java.nio packages for file input and output operations. Let's discuss
the concept of files in Java and various implementations:
1. File Concept in Java:
• File Class: The java.io.File class represents a file or directory path in a
platform-independent manner. It doesn't provide methods for file
manipulation but serves as an abstraction for file system paths.
• File System Operations: Java provides various classes and methods for
performing file system operations, including creating, deleting, renaming,
and checking the existence of files and directories.
2. File Operations in Java:
a. Reading from Files:
• FileInputStream and BufferedReader:
try (BufferedReader reader = new BufferedReader(new
FileReader("example.txt"))) { String line; while ((line = reader.readLine()) != null) {
System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }
• Scanner for Parsing:
try (Scanner scanner = new Scanner(new File("example.txt"))) { while
(scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } } catch
(FileNotFoundException e) { e.printStackTrace(); }
1
b. Writing to Files:
• FileOutputStream and BufferedWriter:
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, World!"); } catch (IOException e) { e.printStackTrace(); }
• PrintWriter for Formatted Output:
try (PrintWriter writer = new PrintWriter("output.txt")) { writer.println("Line 1");
writer.println("Line 2"); } catch (FileNotFoundException e) { e.printStackTrace(); }
3. Java NIO (New I/O) for Advanced File Operations:
Java NIO introduces a more flexible and scalable approach to file I/O with the
java.nio.file package.
• Path and Paths:
Path filePath = Paths.get("example.txt");
• Files Class:
try { List<String> lines = Files.readAllLines(filePath);
lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); }
4. Common File Operations:
• Checking File Existence:
File file = new File("example.txt"); boolean exists = file.exists();
• Deleting a File:
File fileToDelete = new File("fileToDelete.txt"); boolean deleted =
fileToDelete.delete();
• Creating Directories:
File newDirectory = new File("newDirectory"); boolean created =
newDirectory.mkdir();
2
5. Exception Handling:
• File I/O operations can throw IOException, so it's essential to handle or
propagate these exceptions appropriately.
try { // File operations } catch (IOException e) { e.printStackTrace(); }
6. Closing Resources:
• When working with file streams, it's crucial to close resources to release
system resources. The try-with-resources statement ensures that resources
are closed properly.
try (BufferedReader reader = new BufferedReader(new
FileReader("example.txt"))) { // File reading operations } catch (IOException e) {
e.printStackTrace(); }