IO Flashcards
nacteni file.txt do input streamu
InputStream in = new FileInputStream(“file.txt”);
output stream to file.txt
OutputStream out = new FileOutputStream(“file.txt”);
FileInputStream - co umi
Umi cist z file do inputStream (v bytech)
InputStreamReader - co umi
prevest stream bytu na stream charu
BufferedReader - co umi
precist radek z charoveho streamu: br.readLine()
FileOutputStream - co umi
umi ze streamu bytu zapisovat do souboru
OutputStreamWriter
prevadi stream charu na stream bytu
BufferedWriter - co umi
umi zapisovat do charoveho streamu: bw.println(“x”)
Zapis radku “x” do souboru file.txt
try (BW out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(“file.txt”)))) {
out.println(“x”);
}
Vypis radku ze souboru file.txt na stdout
try (BR in = new BufferedReader(new InputStreamReader(new FileInputStream(“file.txt”)))) {
String line;
while (line = in.readLine() != null) {
sout(line);
}
}
Alternativa k new InputStreamReader(new FileInputStream(file))
new FileReader(file, charset)
Alternativa k new OutputStreamWriter(new FileOutputStream(file))
new FileWriter(file, charset)
Jake mame tridy pro zapis/cteni mezi streamy
PipedInput/OutputStream
PipedReader/Writer
Jake mame tridy pro zapis/cteni objektu do streamu
ObjectInput/OutputStream
vytvor charset pro UTF-8
Charset.forName(“UTF-8”)
Nacteni vsech radku ze souboru file.txt do seznamu
Files.readAllLines(Path.of(“file.txt”));
Jak se nactou byty z readeru do bufferu? (2)
read(buffer)
read(buffer, offset, length) - kolik se preskoci a kolik vezme
vraci pocet nactenych bytu (nebo -1)
Jak se oznaci pozice ve streamu?
is.mark(5)
Jak se input stream prevede na output stream
is.transferTo(os)
Output writer - jak se zapise z bufferu (byte[]) do streamu
os.write(buffer, offset, length) - offset je u bufferu
Co dela flush na streamu?
Zapise na vystup a smaze frontu
Metody zmeny pozice v binarnim streamu (2)
is.skip(5)
is.reset()
Jak zjistim zbyvajici pocet bytu ve streamu?
is.available()
Checky: input stream - lze oznacovat pozici?
textovy input stream - lze cist?
is.markSupported()
is.ready()
Soubor file.txt - zapis do souboru file2.txt (binarne)
try (InputStream is = new FileInputStream(“file.txt”);
OutputStream os = new FileOutputStream(“file2.txt”)) {
byte[] bytes = new byte[1024];
while ((length = is.read(bytes)) != -1) {
os.write(bytes, 0, length);
}
}
Trida pro cteni bytu ze souboru do streamu
FileInputStream
Trida pro prevod input streamu s byty na input stream se znaky
InputStreamReader
Trida pro cteni ze znakoveho input streamu po radcich
BufferedReader
Trida pro zapis streamu bytu do souboru
FileOutputStream
Trida pro prevod output streamu charu na output stream bytu
OutputStreamWriter
Trida pro zapis do znakoveho output streamu po radcich
PrintWriter
InputStream pro stdin, PrintWritery pro stdout, a stderr
System.out, System.in, System.err
Jak se cte ze stdin? dokud tam neni hodnota exit
Scanner s = new Scanner(System.in);
String txt;
while ((txt = s.nextLine()) != “exit”) {
sout(txt);
}
Jak zapisu do konzole “str”?
System.console().writer().println(“str”);
Jak prectu z konzole radek?
Co vraci, kdyz dosly radky?
String line = System.console().reader().readLine()
null