50 likes | 167 Vues
This guide explores the concept of readers in Java, focusing on how they serve as a means of reading text inputs. It discusses the functionality of BufferedReader, which allows reading input from streams line by line. The document also covers the importance of encoding, such as ASCII and Unicode, and demonstrates basic encryption techniques using FileReader. With practical examples, readers will learn how to handle non-ASCII input and implement encryption while managing exceptions effectively in their Java applications.
E N D
Readers a means of reading text
What are readers? • Inputs a stream • Outputs a String • Unicode is produced from ASCII input • Manage the translation from non ASCII input • ASCII – American Standard Code for Information Interchange • EBCDIC – Extended Binary Coded Decimal Interchange Code.
How do I build a reader? BufferReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f))); .... while ((String s = br.readLine()) != null) { processLine(s); }
Simple encryption try { FileReader fr = new FileReader("input.txt"); int c = reader.read(); while(c != -1) { char newC = encrypt((char) c); System.out.print(newC); c = reader.read(); } reader.close(); }......add a catch clause......
BufferedReader: one line at a time • Analogously, you may want to read in a line at a time • Use BufferedReader • public BufferedReader(Reader in) • public String readLine() • returns null on EOF