Čtení a zápis souborů

Input/Output and Streams in Java

Alex Liu

Software Development Engineer

Přetypování

  • Převod mezi primitivními datovými typy

  • Rozšiřující přetypování – automatické, menší -> větší typ

    • byte -> short -> char -> int -> long -> float -> double
// Widening casting - happens automatically
byte myByte = 8;
int myInt = myByte;
  • Zužující přetypování – ruční, větší -> menší typ
// Narrowing casting - is manual
double myDouble = 3.14;
int myInt = (int) myDouble; // Note the (int)
Input/Output and Streams in Java

Specifické přetypování při čtení souborů

  • Přetypování z int na 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

Čtení dat ze souboru pomocí FileReader

  • Importujte třídu FileReader pro umožnění čtení souborů
import java.io.FileReader;
  • Čtěte soubor, tiskněte znak po znaku a poté soubor zavřete
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

Efektivní čtení s BufferedReader

  • BufferedReader - import java.io.BufferedReader;
    • Čte textové soubory řádek po řádku
    • Efektivnější pro větší soubory
// 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

Zápis dat pomocí FileWriter

  • FileWriter:
    • Ve výchozím nastavení přepíše existující obsah – buďte opatrní!
  • 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

Připojování dat pomocí FileWriter

  • FileWriter
    • Umožňuje připojování dat k souboru v režimu append bez přepsání existujícího obsahu

$$

// 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

Efektivní zápis s BufferedWriter

  • BufferedWriter - import java.io.BufferedWriter;
    • Zapisuje text efektivněji ve větších blocích
    • Minimalizuje počet operací zápisu
// 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

Pojďme si procvičit!

Input/Output and Streams in Java

Preparing Video For Download...