OOP_L10.ppt
- Количество слайдов: 14
Введення-виведення в мові Java Класифікація потоків: q Байтові потоки q Символьні потоки
Класи байтових потоків Bufferedlnput. Stream Input. Stream Objectlnput. Stream Buffered. Output. Stream Object. Output. Stream Byte. Arraylnput. Stream Output. Stream Byte. Array. Output. Stream Pipedlnput. Stream Datalnput. Stream Piped. Output. Stream Data. Output. Stream Print. Stream Pushbacklnput. Stream Filelnput. Stream Random. Access. File. Output. Stream Sequencelnput. Stream Filter. Output. Stream
Класи символьних потоків Потоковый класс Line. Nиmber. Reader Oиtpиt. Stream. Writer Bиffered. Reader Piped. Reader Bиffered. Writer Piped. Writer Char. Array. Reader Print. Writer Char. Array. Writer Pиshback. Reader File. Reader String. Reader File. Writer String. Writer Filter. Reader Writer Filter. Writer Inpиt. Stream. Reader
Наперед визначені потоки v v v in out err System. in System. out System. err
Читання консольного введення Buffered. Reader(Reader input. Reader) Input. Stream. Reader(Input. Stream input. Stream) Buffered. Reader br = new Buffered. Reader(new Input. Stream. Reader(System. in));
Читання символів import java. io. *; class BRRead { public static void main(String args[]) throws IOException { char c; Buffered. Reader br = new Buffered. Reader(new Input. Stream. Reader(System. in)); System. out. println("Enter characters, 'q' to quit. "); // read characters do { c = (char) br. read(); System. out. println(c); } while(c != 'q'); } } int read() throws IOException Enter characters, 'q' to quit. 123 abcq 1 2 3 A B C q
Читання стрічок (1) import java. io. *; class BRRead. Lines { String read. Line() throws IOException public static void main(String args[]) throws IOException { // create a Buffered. Reader using System. in Buffered. Reader br = new Buffered. Reader(new Input. Stream. Reader(System. in)); String str; System. out. println("Enter lines of text. "); System. out. println("Enter 'stop' to quit. "); do { str = br. read. Line(); System. out. println(str); } while(!str. equals("stop")); } }
Читання стрічок (2) import java. io. *; System. out. println("n. Here is your file: "); class Tiny. Edit { // display the lines public static void main(String args[]) for(int i=0; i<100; i++) { throws IOException if(str[i]. equals("stop")) break; { System. out. println(str[i]); // create a Buffered. Reader using } System. in } } Buffered. Reader br = new Buffered. Reader(new Input. Stream. Reader(System. in)); String str[] = new String[100]; System. out. println("Enter lines of text. "); System. out. println("Enter 'stop' to quit. "); for(int i=0; i<100; i++) { str[i] = br. read. Line(); if(str[i]. equals("stop")) break; }
Запис консольного виведення class Write. Demo { public static void main(String args[]) { int b; b = 'A'; System. out. write(b); System. out. write('n'); } } void write (int byteval)
Клас Print. Writer import java. io. *; public class Print. Writer. Demo { public static void main(String args[]) { Print. Writer pw = new Print. Writer(System. out, true); pw. println("This is a string"); int i = -7; pw. println(i); double d = 4. 5 e-7; pw. println(d); } } Print. Writer pw = new Print. Writer(System. out, true) This is a string -7 4. 5 e-7
Читання і запис файлів (1) File. Input. Stream(String file. Name) throws File. Not. Found. Exception File. Output. Stream(String file. Name) throws File. Not. Found. Exception void close() throws IOException int read() throws IOException Void write(int byteval) throws IOException
Читання і запис файлів (2) import java. io. *; class Show. File { public static void main(String args[]) throws IOException { int i; File. Input. Stream fin; try { fin = new File. Input. Stream(args[0]); } catch(File. Not. Found. Exception e) { System. out. println("File Not Found"); return; } catch(Array. Index. Out. Of. Bounds. Exception e) { System. out. println("Usage: Show. File"); return; } // read characters until EOF is encountered do { i = fin. read(); if(i != -1) System. out. print((char) i); } while(i != -1); fin. close(); } }
Читання і запис файлів (3) import java. io. *; class Copy. File { public static void main(String args[]) throws IOException { int i; File. Input. Stream fin; File. Output. Stream fout; try { // open input file try { fin = new File. Input. Stream(args[0]); } catch(File. Not. Found. Exception e) { System. out. println("Input File Not Found"); return; } // open output file try { fout = new File. Output. Stream(args[1]); } catch(File. Not. Found. Exception e) { System. out. println("Error Opening Output File"); return; } } catch(Array. Index. Out. Of. Bounds. Exception e) { System. out. println("Usage: Copy. File From To"); return; }
Читання і запис файлів (4) // Copy File try { do { i = fin. read(); if(i != -1) fout. write(i); } while(i != -1); } catch(IOException e) { System. out. println("File Error"); } fin. close(); fout. close(); } }
OOP_L10.ppt