Reading and writing files

Input/Output and Streams in Java

Alex Liu

Software Development Engineer

Casting

  • Converting between different primitive data types

  • Widening casting - automatically for smaller -> larger size

    • byte -> short -> char -> int -> long -> float -> double
// Widening casting - happens automatically
byte myByte = 8;
int myInt = myByte;
  • Narrowing casting - manual, larger -> smaller
// Narrowing casting - is manual
double myDouble = 3.14;
int myInt = (int) myDouble; // Note the (int)
Input/Output and Streams in Java

Understanding specific casting for reading file

  • Casting from int to char: char data = (char) 123;
// Unicode value for character 'a'
int data = 97;
// Directly print the data in `int` format
System.out.print(data);
// Cast data from `int` to `char` and print
System.out.print((char)data);
97
a
1 https://www.ascii-code.com/
Input/Output and Streams in Java

Reading data from a file using FileReader

  • Import the FileReader class to enable file reading functionality
import java.io.FileReader;
  • Read the file, print character by character and then close the file
FileReader fr = new FileReader("example.txt");
int data = fr.read();
// Read and print each character
while (data != -1) {
    System.out.print((char) data);
    data = fr.read();
}
fr.close(); // Close the file to release resources
Input/Output and Streams in Java

Efficient reading with BufferedReader

  • BufferedReader - import java.io.BufferedReader;
    • Reads text files line by line
    • More efficient for larger files
// Create a BufferedReader object
BufferedReader br = new BufferedReader(new FileReader("example.txt"));
String line;
// User .readLine() to read the file line by line
while ((line = br.readLine())!= null){
  System.out.println(line);
}
br.close();
Input/Output and Streams in Java

Writing data using FileWriter

  • FileWriter:
    • Overwrites existing content by default - use caution to prevent data loss!
  • Import FileWriter
import java.io.FileWriter;


// Create a new `FileWriter` Object FileWriter fw = new FileWriter("example.txt"); // Write the data to file using `.write() fw.write("Overwriting the file."); fw.close()
Input/Output and Streams in Java

Appending data using FileWriter

  • FileWriter
    • Allows appending data to files by setting to append mode without overwriting existing content

$$

// Initialize `FileWriter` with append mode
FileWriter fw = new FileWriter("example.txt", true);
// Append data to the file
fw.write("Appending data.");
fw.close()
Input/Output and Streams in Java

Efficient writing with BufferedWriter

  • BufferedWriter - import java.io.BufferedWriter;
    • Writes text more efficiently using larger chunks of text
    • Minimizes write operations
// Create a BufferedWriter object
BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"));
bw.write("Writing data");

// .newLine() to add line breaks
bw.newLine(); 
bw.close();
Input/Output and Streams in Java

Let's practice!

Input/Output and Streams in Java

Preparing Video For Download...