Reading and writing Files 

Java provides a number of classes and methods that allow you to read and write files, In Java, all files are byte-oriented, and Java provides methods to read and write bytes from and to a file. However, Java allows you to wrap a byte-oriented file stream within. a character-based object. This technique is described in Part II. This chapter examines' the basics of file I/O.

Two.of the most often-used stream classes are File input Stream and File Output Stream, which create byte streams linked to files. To open a file, you simply create an object of one of these classes, specifying the name of the file as an argument to the constructor. While both classes support additional, overridden constructors, the following are the forms that we will be using: . File input Stream (String file Name) throws Troubleshooting

exception File Output Stream(String file Name) throws File Not Found Exception Here,file Name specifies the name of the file that you want to open. When you create an ~ input stream, if the file does not exist, then File Not Found Exception is thrown. For . output streams, if the file cannot be created, then File Not Found Exception is thrown. 'When an output file is opened, any preexisting file by the same name is destroyed. When you are done with a file, you should close it by calling close(). It Is defined by both EU Input Stream and File Output Stream, as shown here: void dose( ) throws IO Exception, To read from a file, you can use a version of read( ) that is defined within File Input Stream. The one that we will use is shown here: int read( ) throws IO Exception Each time that it is called, it reads a single byte from the file and returns the byte as an. integer value: read() returns -1 when the end of the file is encountered. It can throw an IO Exception.

The following program uses ready ) to input and display the contents of a text file, , the name of which is specified as a command-line argument. Note the try/catch blocks .that handle the two er~rs that might occur when this program is used-the specified file not being found or user forgetting to include the name of the file. You can use .this same approach whenever you use command-line arguments. To write to a file, you will use the writer ) method defined by Outputting: am. Its simplest form is shown here: . ' , void write (int civility) throws IO Exception 'This method writes the byte specified by balaclava to the file. Although by html is declared as an integer, only the low-order eight bits are written to 1.'10 file. If an error occurs .during writing, an IO Exception is thrown. The next example uses write! ) to copy a file: Notice the way that potential I/O errors are handled in this program and in the preceding Show File program. Unlike most other computer languages, including C and C++, which use error codes to report file errors, java uses its exception handling mechanism. Not only does this make file handling cleaner, but it also enables Java to easily differentiate the end-of-file condition from file errors when input is being performed. In C/C++, many input functions return the same value when an error occurs and when the end of the file is reached. (That is, in C/C++, an EOF condition often is mapped to the same value as an input error.) This usually means that the programmer must include extra program statements-to determine which event actually occurred. In Java, errors are passed to YOU program via exceptions, not by values returned by read  ). Thus, when read ) returns -1, it means only one thing: the end of the file has been encountered.

Share This