powerpoint 4.txt

The flashcards below were created by user dimeng on FreezingBlue Flashcards.

  1. What are exceptions?
    Exceptions are objects that are thrown and caught (handled).
  2. What is the syntax for a try / catch statement?
    • try {
    • statements }
    • catch (ExceptionName e) {
    • statements when ExceptionName happens }
    • more statements that happen whether or not an exception occurs (unless break or return in Exception statements.)
  3. What is a stream?
    • A stream is an Object in Java.
    • -It can deliver data from the program to a destination (the screen or a file) (output)
    • -It can take data from a source (keyboard or file) and deliver it to the program (input)
  4. Objects of type Scanner are input or output streams?
  5. Objects of type PrintWriter are input or output streams?
  6. What are some differences betwee a file and a stream?
    A file is permanent storage for an application. Data in a file is available externally to the program. A stream is a representation of the file in a program.
  7. In java, what is the process of writing to a file using a stream?
    You must first specify a filename, then attempt to open the file. Next, you associate the file with a stream. Next, you write to the stream (file). Finally, you close the file.
  8. What import statement do you need in order to write output to a file?
    import java.io.PrintWriter;
  9. What happens when you associate a PrintWriter stream with a file that already exists?
    The existing file is overwritten.
  10. What methods are used to output data to the file or stream?
    • print()
    • println()
    • printf()
  11. Give a short example of writing data to an output file.
    • import java.io.PrintWriter;
    • String fname = "readme.txt";
    • PrintWriter outputString = new PrintWriter( fname );
    • outputString.println( "hello" );
    • outputString.printf( "this is %s", fname );
    • outputString.close();
  12. What Class must be imported to throw the file not found exception?
    import java.io.FileNotFoundException
  13. What is the syntax for declaring a main method that throws a FileNotFoundException?
    public static void main (String[] args ) throws FileNotFoundException {
  14. When you associate a stream with a file, what is the syntax for appending your output to the file if the file already exists?
    outputStream = new PrintWriter ( new FileOutputStream (fname, true ); //the , true makes it append
  15. What happens if you try to append to a file that doesn't exist?
    • The file will be created.
    • *Ask about this*
    • Give a short example of reading data from an input file.
    • import java.io.file;
    • import java.util.Scanner;
    • String fName = "readme.txt";
    • Scanner inStream = new Scanner ( new File (fName) );
    • int i = inStream.nextInt();
    • inStream.close;
  16. What happens if the filename specified as the input file does not exist?
    file not found error
  17. What happens if the file exists but is empty?
    NoSuchElementException when trying to read the first line.
  18. If you want to specify a path for an input or output file, what must you remember when you type the path in?
    The \ requires an escape character - so replace each \ with \\. Sometimes you can use / rather than \, but not always.
Card Set:
powerpoint 4.txt
2015-03-02 03:45:34
Diane cs140
Diane cs140 powerpoint4
Show Answers: