add chapter 11

This commit is contained in:
Rémi Heredero 2022-03-16 08:42:47 +01:00
parent bd2e651b5e
commit c99d2bd3f0
10 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package C11_Flux_de_donnees.C11b_Lire_et_ecrire_des_fichiers;
import java.io.BufferedReader;
import java.io.FileReader;
public class StreamReadDemo {
public static void main(String args[]) {
try {
// The first argument is the path where the file will be read.
// This is a absolute path for Windows. In Linux, something
// like "~/out.txt" would read the file directly in you home directory
FileReader f = new FileReader("c://tmp//stuff.txt");
BufferedReader bf = new BufferedReader(f);
String line = "";
// Each readLine() call CONSUMES the complete line
line = bf.readLine();
System.out.println("The first line of file is :" + line);
line = bf.readLine();
System.out.println("The second line of file is :" + line);
bf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,26 @@
package C11_Flux_de_donnees.C11b_Lire_et_ecrire_des_fichiers;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class StreamWriteDemo {
public static void main(String[] args) {
try {
// The first argument is the path where the written file will be
// This is a absolute path for Windows. In Linux, something
// like "~/out.txt" would write the file directly in you home directory
FileOutputStream fs = new FileOutputStream("c://tmp//out.txt", true);
PrintWriter pw = new PrintWriter(fs);
// This is the content which will be written INTO the file
pw.print("This is the text which will be written");
// We have to close the file when we are done working with it
pw.close();
} catch (Exception e) {
System.out.println("File can't be written");
e.printStackTrace();
}
System.out.println("Writing done");
}
}