Java Advanced Input Stream Reader Output Stream Georgiy
- Slides: 52
Java Advanced Ввод-вывод
Классы потоков Ввод Байтовый Символьный Input. Stream Reader Вывод Output. Stream Georgiy Korneev Java Advanced / Ввод-вывод Writer 6
Пример: Блочное копирование l Процедура копирования void copy(Input. Stream is, Output. Stream os) throws IOException { byte[] b = new byte[1024]; int c = 0; while ((c = is. read(b)) >= 0) { os. write(b, 0, c); } } Georgiy Korneev Java Advanced / Ввод-вывод 10
Классы файлового ввода-вывода l Классы File* File. Input. Stream l File. Output. Stream l File. Reader l File. Writer l Georgiy Korneev Java Advanced / Ввод-вывод 12
Пример: преобразования регистра l Файл input. txt копируется в output. txt с изменением регистра Reader reader = new File. Reader("input. txt"); Writer writer = new File. Writer("output. txt"); int c = 0; while ((c = reader. read()) >= 0) { writer. write(Character. to. Upper. Case((char) c)); } reader. close(); writer. close(); Georgiy Korneev Java Advanced / Ввод-вывод 14
Пример: перекодирование файла l Файл input. txt копируется в output. txt с изменением кодировки с Cp 1251 на Cp 866 Reader reader = new Input. Stream. Reader( new File. Input. Stream("input. txt"), "Cp 1251"); Writer writer = new Output. Stream. Writer( new File. Output. Stream("output. txt"), "Cp 866"); int c = 0; while ((c = reader. read()) >= 0) writer. write(c); reader. close(); writer. close(); Georgiy Korneev Java Advanced / Ввод-вывод 17
Пример: шифрующий поток public class Encoding. Output. Stream extends Filter. Output. Stream { private final int key; public Encoding. Output. Stream(Output. Stream os, int key) { super(os); this. key = key; } public void write(int b) throws IOException { super. write(b ^ key); } } Georgiy Korneev Java Advanced / Ввод-вывод 22
Класс System out – Print. Stream для stdout l err – Print. Stream для stderr l in – Input. Stream для stdin l set. Out(. ) / set. Err(. ) / set. In(. ) – установка консольных потоков l console() – символьный доступ к консоли l Georgiy Korneev Java Advanced / Ввод-вывод 34
Класс Console format/printf(format, args) – Форматированный вывод l reader() – Reader для stdin l writer() – Writer для stdout l read. Line() – чтение строки текста l read. Password(format? , args? ) – чтение пароля l Georgiy Korneev Java Advanced / Ввод-вывод 35
Обычная обработка исключений Reader reader = new File. Reader("input. txt"); try { // Операции с файлом } finally { reader. close(); } Georgiy Korneev Java Advanced / Ввод-вывод 45
Надежная обработка исключений Reader reader = new File. Reader("input. txt"); try { // Операции с файлом reader. close(); } catch (IOException e) { try { reader. close(); } catch (IOException e) { /* Ignoring */ } throw e; } Georgiy Korneev Java Advanced / Ввод-вывод 46
Альтернативный метод Reader reader = null; try { reader = new File. Reader("input. txt"); … } finally { if (reader != null) { reader. close(); } } Georgiy Korneev Java Advanced / Ввод-вывод 47
Случай нескольких потоков Reader reader = new File. Reader("input. txt"); try { Writer writer = new File. Writer("output. txt"); try { // Операции ввода-вывода. . . } finally { writer. close(); } } finally { reader. close(); } Georgiy Korneev Java Advanced / Ввод-вывод 48
Подавление исключений l Print. Writer l l Print. Stream l l check. Error() check. Errors() Scanner l io. Exception() Georgiy Korneev Java Advanced / Ввод-вывод 49
Ссылки I/O tutorial // http: //java. sun. com/docs/books/tutorial/essen tial/io/index. html l I/O in Java 2 Platform // http: //java. sun. com/j 2 se/1. 5. 0/docs/guide/io/ l Georgiy Korneev Java Advanced / Ввод-вывод 51
Вопросы Georgiy Korneev Java Advanced / Ввод-вывод 52
- Input and output devices chapter 2
- Magnetic mark reader
- Esnalt
- Output devices notes
- Streams in c++
- Meletakkan posisi yang telah ditandai dengan mark()
- Input stream in java
- Java input and output statements
- Chapter 15 input output java
- Java file input output
- Java file input and output
- Console input and output in java
- Differentiate byte stream and character stream
- File reader java
- Java oop exercises
- Java scanner stream
- Peek stream java
- Advanced data structures in java
- Advanced programming in java
- Dependent variable explained
- Output devices of virtual reality
- Vat input
- Input vs output vat
- Input vs output vat
- Input transformation output process examples
- Cpu vs io
- System and forms design
- Longitudinal fissure
- Scanner is an input device
- The ratio of input distance to output distance
- Limitations of the study examples
- Input productivity
- Perkakasan
- Peripheral output adalah
- Laser printer output device
- Conclusion for input and output devices of computer
- Input output proses
- Ipo research paradigm
- Input output organization
- How to copy text from image in pc
- Is a plotter an input or output device
- Keyboard input or output
- Disadvantages of input devices
- Input and output device
- Input/output statement in fortran
- Conclusion for input and output devices of computer
- Objectives of input output management
- Input and output unit
- Conclusion of input devices
- Human input output channels
- Cpu output device
- Manual input flowchart
- Membuat flowchart