![]() For example our main method should look like this : public static void main(String args) throws IOException In next chapter you will learn all about exceptions, but for now, we will simply allow our methods to throw any exceptions that might occur. If an exception occurs in a method, then the method should either handle the exception or throw it for the calling environment to handle. If such things happen, we say that an exception has occurred. out.close() Adding a throws Clause to the Method HeaderÄuring program execution, various things can happen - For example, suppose you create a PrintWriter object and pass the name of a file to its constructor The PrintWriter object attempts to create the file, but unexpectedly the disk is full and the file cannot be created. In step 4, once the output is completed, you close the output files by using the method close. In step 3, you store the output in the file oceans.txt. PrintWriter out = new PrintWriter("oceans.txt") // Step 2 In step 2, you create the PrintWriter object out and associates it with the file hello.txt. In step 1, you import the package to use PrintWriter class. Now discuss the above prgram step by step: ![]() Write the name of four oceans to the file Public static void main(String args) throws IOException The following program writes the name of four oceans to the text file. The class PrintWriter has the familiar print() and println() methods we have been using for writing to the console. The simplest way to write text to a file requires us to use PrintWriter class from the standard package java.io.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |