This is handy, as the basic input and output streams deal only with the transfer of bytes - not the easiest way for us to write out data. In this case, we wish to write a string to the file, and so we create a new PrintStream object that takes as its constructor the existing FileOutputStream. Any data we send from PrintStream will now be passed to the FileOutputStream, and ultimately to disk. We then make a call to the println method, passing it a string, and then close the connection. After running the output example, and seeing that it wrote to our file, let's take a more detailed look at the code.
Exceptions are events that occur in exceptional or unusual circumstances, such as error conditions.
A method can throw an exception, to indicate to the calling object that a non-standard circumstance has occurred. File input File input under Java differs little from file output. In this example, the file we open is given to us as the first command line parameter. We obtain this by examining the args array of strings. To do anything useful with file input, we create a DataInputStream which can handle reading in data as well as lines of text.