Chapter 6: Controlling Program Flow with Loops 135 (Web space)
Chapter 6: Controlling Program Flow with Loops 135 File handling in Java In Listing 6-4, the actual file-handling statements deserve some attention. These statements involve the use of classes, objects, and methods. Many of the meaty details about these things are in other chapters, like Chapters 7 and 9. Even so, I can t do any harm by touching on some highlights right here. So, you can find a class in the Java language API named java.io.File. The statement File evidence = new File( c:\cookedBooks.txt ); creates a new object in the computer s memory. This object, formed from the java.io.Fileclass, describes everything that the program needs to know about the disk file c:cookedBooks.txt. (In Java, when you want to indicate a backslash inside a double-quoted Stringliteral, you use a double backslash instead.) From this point on in Listing 6-4, the variable evidence refers to the disk file c:cookedBooks.txt. After you ve got all this java.io.Filestuff in your head, the only thing left to know is that the evidenceobject, being an instance of the java.io.File class, has a deletemethod. (What can I say? It s in the API documentation.) When you call evidence.delete, the computer gets rid of the file for you. Variable declarations and blocks A bunch of statements surrounded by curly braces form a block. If you declare a variable inside a block, you generally can t use that variable outside the block. For instance, in Listing 6-4, you get an error message if you make the following change: do { out.print( Delete evidence? (y/n) ); char reply = myScanner.findWithinHorizon( . , 0).charAt(0); } while (reply != y && reply != n ); if (reply == y ) With the declaration char replyinside the loop s curly braces, no use of the name replymakes sense anywhere outside the braces. When you try to compile this code, you get three error messages two for the replywords in while (reply != y && reply != n ), and a third for the if statement s reply.
Are you a Mac user in a search of Mac web hosting provider? We are. once upon a time, we were just like you, trying to find somebody who knows. Unfortunately, we had no luck, so we decided to help many others like you, and us. our team developed Mac friendly web hosting plans, feature packed, constantly monitored, and more than affordable. With us, you are home. Sign up.