The document provides a comprehensive guide on handling streams and files in Java, including fundamental concepts such as character sets, encoding, and serialization. It includes practical examples of reading and writing both binary and text files using various Java classes like InputStream, OutputStream, FileReader, and FileWriter. Additionally, it covers handling exceptions and the importance of using try-with-resources for better resource management.
marcello.thiry@gmail.com if you donot know much about charsets, code pages, encoding, ASCII, UNICODE, etc. Before we start… Take a look in this article http://www.joelonsoftware.com/articles/Unicode.html The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky It’s a bit old, but a good start
5.
marcello.thiry@gmail.com Set of charactersyou can use Charset Repertoire ISO-8859-1 - Western Alphabet ISO-8859-5 - Cyrillic Alphabet JIS X 0208 - Japanese Alphabet ISO-8859-7 - Greek Alphabet
marcello.thiry@gmail.com Region of aphysical memory storage used to temporarily store data while it is being moved from one place to another Data Buffer
15.
marcello.thiry@gmail.com Conversion of anobject to a series of bytes, which lets a program write whole objects out to streams and read them back again Object Serialization
16.
Set of routines,protocols, and tools to access a software component/module without the need to know details of its implementation Application Programming* Interface API *Also used: program
marcello.thiry@gmail.com read() reads asingle byte read(byte[] b) reads b.length bytes into an array read(byte[] b, int off, int len) reads len bytes into an array, starting from the position off skip(long n) skips discards n bytes close() closes the stream https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html
23.
marcello.thiry@gmail.com mark(int readlimit) marksthe current position in this input stream reset() repositions this stream to the position at the time the mark method was last called https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html And, if markSupported()...
24.
marcello.thiry@gmail.com write(int b) writesa single byte write(byte[] b) writes b.length bytes from the array write(byte[] b, int off, int len) writes len bytes from the array starting at offset off flush() forces any buffered output bytes to be written out
marcello.thiry@gmail.com class Reader Abstract classfor reading character streams read(): reads a single character read(char[]): reads characters into an array skip(long): skips N characters close(): closes the stream https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html
marcello.thiry@gmail.com But what aboutthe class InputStreamReader? https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html Reads bytes and decodes them into characters using a specified charset Bridge from byte streams to character streams
marcello.thiry@gmail.com class Writer Abstract classfor writing to character streams write(int): writes a single character write(char[]): writes an array of characters write(String): writes a string close(): closes the stream https://docs.oracle.com/javase/8/docs/api/java/io/Writer.html
marcello.thiry@gmail.com String fileName ="c:/temp/MyFile.txt"; try { FileWriter writer = new FileWriter(fileName, true); try (BufferedWriter buffWriter = new BufferedWriter(writer)) { buffWriter.write("My first line"); buffWriter.newLine(); buffWriter.write("My second line!"); } } catch (IOException e) {...}
66.
marcello.thiry@gmail.com But what aboutthe class OutputStreamWriter? https://docs.oracle.com/javase/8/docs/api/java/io/OutputStreamWriter.html Characters written to it are encoded into bytes using a specified charset bridge from character to byte streams
marcello.thiry@gmail.com try { OutputStreamWriter outWriter= new OutputStreamWriter(System.out); try (BufferedWriter buffWriter = new BufferedWriter(outWriter)) { buffWriter.write("Printing a line on the console"); buffWriter.newLine(); buffWriter.write("Printing a second line...rn"); } } catch (IOException e) {}