INFO 6066 - I0 Reading Files and Writing To Files
INFO 6066 - I0 Reading Files and Writing To Files
INFO 6066
1 INFO 6066
Software and Information Systems Testing
2 INFO 6066
Software and Information Systems Testing
3 INFO 6066
Software and Information Systems Testing
4 INFO 6066
Software and Information Systems Testing
5 INFO 6066
Software and Information Systems Testing
Using getParentFile()
• There is a method of the File object called getParentFile() which will
read the parent element or elements of the path so that we can
create them first using the mkdirs() method.
• Once this is done then we can call createNewFile() to create the
empty file
6 INFO 6066
Software and Information Systems Testing
7 INFO 6066
Software and Information Systems Testing
Handling Exceptions:
Approach #1: “CONFESS” using throws
• If your code could possibly throw a CHECKED EXCEPTION such as
FileNotFoundException, then you must “confess” to the compiler that you
know that your code could possibly do this.
• The “confession” is done using a throws clause on the method that contains
the offending code.
• Example: in your main method you are trying to create a Scanner object
using a File object as the argument,. You need to add the throws clause to
your main method declaration like this:
8 INFO 6066
Software and Information Systems Testing
Confessing, cont’d
• By adding the throws clause to the method, you are
letting the compiler know that “yes, this could cause a
checked exception if the file is not found, and I am
willing to let my program crash if this occurs”.
Handling Exceptions:
Approach #2: Using try-catch blocks
• A second way of handling exceptions (and the more effective way) is to
put the code that could cause the exception inside a “try” block.
• Immediately following the try block, you write a “catch” block and specify
what type of exception the catch block will “handle”, and provide a formal
parameter variable to hold the exception object when it is thrown.
• Example:
try
{
//some code that could throw a FileNotFoundException
}
catch(FileNotFoundException ex) // ‘ex’ is the formal parameter
{
//usually here we print the error message held by the object
// and the stack trace, which shows the line where the exception was
// thrown
}
10 INFO 6066
Software and Information Systems Testing
11 INFO 6066
Software and Information Systems Testing
12 INFO 6066
Software and Information Systems Testing
13 INFO 6066
Software and Information Systems Testing
14 INFO 6066
Software and Information Systems Testing
15 INFO 6066
Software and Information Systems Testing
16 INFO 6066
Software and Information Systems Testing
17 INFO 6066
Software and Information Systems Testing
IO Summary
• We’ve just scratched the surface of doing file input/output
here.
• We’ve dealt just with text files, in which the characters are
encoded using the Unicode coding scheme, so the bytes are
stored as characters.
• You can also read and write raw bytes to binary files, such
as .gif or .jpeg files using other classes from the Java API such
as FileInputStream and FileOutputStream, or
BufferedInputStream and BufferedOutputStream.
• There is also an updated Java library called the java.nio
package ( the ‘n’ stands for ‘new’) which provides more classes
with more capabilities for moving data between devices.
18 INFO 6066
Software and Information Systems Testing
Homework
9.1 Output and input streams
9.2 Output formatting
9.3 Streams using Strings
9.4 File input
9.5 File output
19 INFO 6066