|
| 1 | +IOException : |
| 2 | +An IOException is any unexpected problem the JVM encounters while attempting to run a program. |
| 3 | +When an IOException is thrown, it means that whatever is throwing the exception (perhaps a try{}-catch block that reads |
| 4 | +data from a file) can throw an IOException, for example if the file is not found, corrupted, etc, or when the file is |
| 5 | +otherwise unable to be read, or any other of a list of issues that can occur with the IO package and it's extensions. |
| 6 | + |
| 7 | +Streams : |
| 8 | +In its most basic form a stream simply represents a sequence of data (bytes or unicode characters) in some sort of a sequential queue. |
| 9 | +Java programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. |
| 10 | +A stream is linked to a physical device by the Java I/O system. |
| 11 | +Java implements streams within class hierarchies defined in the java.io package. |
| 12 | +The same I/O classes and methods can be applied to different types of devices. |
| 13 | + |
| 14 | +Java defines two types of streams: |
| 15 | +-Byte streams provide a convenient means for handling input and output of bytes. |
| 16 | +Byte streams contain binary data this useful for things such as reading and writing to files - just imagine opening up an image file in a text editor, that is a good representation of byte data. |
| 17 | +EXAMPLE : Use a byte stream ex : InputStream etc and type 'ᛞ'. This exceeds the 256 limit of 8-bit characters hence it will show some other |
| 18 | +value. If we type 'ᛞ' in char-stream such as InputStreamReader it will show 'ᛞ' only. |
| 19 | + |
| 20 | + OutputStream os = System.out; |
| 21 | + os.write('ᛞ'); // will not show anything (or garbage) cuz range exceeded. ᛞ is Unicode. |
| 22 | + |
| 23 | + |
| 24 | +-Character streams provide a convenient means for handling input and output of characters. They use Unicode and, |
| 25 | +therefore, can be internationalized, good candidate for things like keyboard input and console output. |
| 26 | +Also, in some cases, character streams are more efficient than byte streams. |
| 27 | + |
| 28 | +Byte streams are defined by using two class hierarchies. |
| 29 | +At the top are two abstract classes: InputStream and OutputStream. |
| 30 | +Each of these abstract classes has several concrete subclasses that handle the differences among various devices, |
| 31 | +such as disk files, network connections, and even memory buffers. |
| 32 | +The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement. |
| 33 | +Two of the most important methods are read( ) and write( ) |
| 34 | + |
| 35 | +Character streams are defined by using two class hierarchies. |
| 36 | +At the top are two abstract classes: Reader and Writer. |
| 37 | +Two of the most important methods are read( ) and write( ). |
| 38 | + |
| 39 | +The Predefined Streams : |
| 40 | +System.out refers to the standard output stream. By default, this is the console. |
| 41 | +System.in refers to standard input, which is the keyboard by default. |
| 42 | +System.err refers to the standard error stream, which also is the console by default. |
| 43 | +System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream. |
| 44 | +These are byte streams. |
| 45 | + |
| 46 | +NOTE : Names of character streams typically end with Reader/Writer & names of byte streams end with InputStream/OutputStream. |
| 47 | + |
| 48 | +InputStreamReader Class : |
| 49 | +EXTENDS READER : Abstract class for reading character streams. |
| 50 | +int read() : The character read, as an integer in the range 0 to 65535 (0x00-0xffff), |
| 51 | + or -1 if the end of the stream has been reached |
| 52 | +Primarily it is used to convert byte streams to character streams. |
| 53 | +java.io.InputStreamReader has several overloaded constructors with each taking an InputStream (like System.in, or FileInputStream) as the first parameter. |
| 54 | +The InputStreamReader class has a method called close() that will close the stream and releases any system resources associated with it. |
| 55 | +The close() method should always be called once you are done with the input stream. |
| 56 | +In Java 7 a new feature called try-with-resources was introduced. |
| 57 | +Specifically a resource is an object that must be closed after the program is finished with it. |
| 58 | +The try-with-resources statement ensures that each resource is closed at the end of the statement. |
| 59 | +How do we know if a class is a resource? Simple, if it implements java.lang.AutoCloseable, |
| 60 | +the class can be considered a resource. |
| 61 | + |
| 62 | +try-with-resources : |
| 63 | +The try-with-resources statement is a try statement that declares one or more resources. |
| 64 | +A resource is an object that must be closed after the program is finished with it. |
| 65 | +The try-with-resources statement ensures that each resource is closed at the end of the statement. |
| 66 | +Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, |
| 67 | +can be used as a resource. |
| 68 | + |
| 69 | +FileReader Class : |
| 70 | +The purpose of the FileReader class is to simply read character based files. |
| 71 | +The FileReader class implements AutoClosable so we can use the try-with-resources type exception handling. |
| 72 | +It also interesting to note that the FileReader class does not define or override any public methods, |
| 73 | +so it inherits all of its methods from its superclass InputStreamReader; InputStreamReader is a subclass of Reader |
| 74 | +which is a subclass of Object and that describes the class hierarchy. |
| 75 | + |
| 76 | +Object -> Reader -> InputStreamReader -> FileReader |
| 77 | + |
| 78 | +BufferedReader Class : BufferedReader(Reader inputReader) |
| 79 | +IT EXTENDS READER. HENCE HAS IT'S OWN read() which is same as InputStreamReader. |
| 80 | +The BufferedReader class is used to read text from a character stream. |
| 81 | +The BufferedReader class has the same read() method that both InputStreamReader and FileReader use to read a single byte at a time. |
| 82 | +The BufferedReader class introduces a method named readLine() which will read an entire line of text which is a huge improvement. |
| 83 | +The BufferedReader class implements AutoClosable so we can use the try-with-resources type exception handling. |
| 84 | + |
| 85 | +To obtain a character-based stream that is attached to the console, wrap System.in in a BufferedReader object. |
| 86 | +InputStreamReader(InputStream inputStream) |
| 87 | +BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 88 | +NOTE : new InputStreamReader(System.in) converts byte stream into char stream. |
| 89 | +AND then br is reading that char stream. Which is what we want! |
| 90 | +After this statement executes, br is a character-based stream that is linked to the console through System.in. |
| 91 | + |
| 92 | +Writer Classes : |
| 93 | + |
| 94 | +OutputStreamWriter Class : |
| 95 | +EXTENDS WRITER |
| 96 | +Used to write to character streams. |
| 97 | +The OutputStreamWriter class implements AutoClosable so we can use the try-with-resources type exception handling. |
| 98 | +There are only four public methods in the OutputStreamWriter class: close(), flush(), getEncoding(), and write(). |
| 99 | +The write() method has three overloaded versions : |
| 100 | +-write(int a) : writes a single character to character stream. |
| 101 | + Characters being written is contained in 16 lower bits of the ‘char’ integer value, |
| 102 | + rest of the 16 higher bits are ignored by the method. |
| 103 | +-write(String str) |
| 104 | +-write(char cbuf[]) |
| 105 | + |
| 106 | +FileWriter Class : |
| 107 | +EXTENDS OutputStreamWriter |
| 108 | +The purpose of the FileWriter class is to simply write character based files. |
| 109 | +The FileWriter class implements AutoClosable so we can use the try-with-resources type exception handling. |
| 110 | +FileWriter class does not define or override any public methods, so it inherits all of its methods from its superclass OutputStreamWriter. |
| 111 | + |
| 112 | +Object -> Writer -> OutputStreamWriter -> FileWriter |
| 113 | + |
| 114 | +BufferedWriter Class : |
| 115 | +The BufferedWriter class is used to write text to a character stream. |
| 116 | +The BufferedWriter class has three overloaded versions of the write() method. |
| 117 | +The BufferedWriter class introduces a method named newLine() which means that you will not have to hardcode in the "\r\n" into your output stream anymore. |
| 118 | +The BufferedWriter class implements AutoClosable so we can use the try-with-resources type exception handling. |
| 119 | + |
| 120 | + |
| 121 | +File Class : |
| 122 | + |
| 123 | +Consider this Windows hard coded path represented as a string: "c:\\Java\\BW\\Sample.txt". If a user attempted to run |
| 124 | +your program on a Linux or UNIX OS, your program will fail miserably. That is because the directory separator is '/' in |
| 125 | +UNIX as opposed to '\' in Windows. The file class provides us with several tools to dynamically create our directory |
| 126 | +and file structure. |
| 127 | + |
| 128 | +Constructors: |
| 129 | +File(File parent, String child) |
| 130 | +File(String pathname) // not very useful for multi-platform |
| 131 | +File(String parent, String child) |
| 132 | +File(URI uri) |
| 133 | + |
0 commit comments