Unit 12 Streams, files and persistence Flashcards
Streams, files and persistence:
What does it mean by “making information persistent” .
so that information can be stored at one time and retrieved unchanged at a later time. For example, if you are word processing a document, you expect to be able to save it one day then open it the next day and find it in the same state as you left it. Persistence involves saving data to files
Streams, files and persistence:
Files or Streams (fill in the blanks):
_____ are used to store data on an external storage device, such as a hard disk drive.
_____ are a way of transferring data from one place to another in a software system.
Files are used to store data on an external storage device, such as a hard disk drive.
Streams are a way of transferring data from one place to another in a software system.
Streams, files and persistence:
which is the source and which is the sink?
When we want to read data into a Java program, the source is the file and the sink is the Java program. The Java program reads data from the stream
Streams, files and persistence:
which is the source and which is the sink?
When we want to save data that is currently in a Java program to a file, the source is the Java program and the sink is the file. The Java program writes data to the stream
Streams, files and persistence:
Reading from (or writing to) a stream is done sequentially. What does sequentially means?
when you read data from a stream the first item is read first, then the second item is read, and so on until the last item is reached.
Streams, files and persistence:
Which of these is a Reader/Writer and which is the InputStream/OutputStream class
- Instances of subclasses of these classes handle 16-bit character streams. This means they correctly handle textual information based on characters and strings.
- Instances of subclasses of these classes handle (8-bit) byte streams. They are used when making objects persistent through a technique called serialisation*, for writing binary data such as sounds and images and internet-based communication.
* The process of converting an object to a sequence of bytes is known as serialisation
- Reader and Writer classes handle 16-bit character streams
- InputStream andOutputStream classes handle (8-bit) byte streams
The File class:
File pathnames can be absolute or relative. Which is which, in the examples below
- String pathname = “C:/BlueJ/README.TXT”;
- OUFileChooser.setPath(“C:/Users/myName/Documents/M250/Activities/Unit12/Unit12_Project_1”);
- File aFile = new File (“Documents/M250/Activities/Unit12/Unit12_Project_1”);
- Absolute - absolute pathname contains all the information you need to know about the location of a file and always starts from the root directory of the disk, usually C:\ in Windows
- Absolute
- Relative - relative pathname assumes the current working directory as the starting point
The File class:
You must remember that both the getFilename() methods
String pathname = “C:/BlueJ/README.TXT”; &
String pathname = OUFileChooser.getFilename();
only return strings, and a further step is needed before we have something that can actually refer to a file. The next step is to create an instance of the Fileclass, and we do this by using the string returned by a getFilename()method as the argument to the File class constructor:
String pathname = //enter code here
File aFile = //enter code here
String pathname = OUFileChooser.getFilename();
File aFile = new File(pathname);
The File class:
a File object does hold important information, for example:
- the pathname;
- whether the pathname identifies a file or a folder (directory);
- whether a physical file exists at that pathname;
- if a physical file exists, whether it can be written to.
Therefore, the protocol of File objects includes the following messages:
- _____( ) – returns true if the file or directory denoted by the pathname exists, false otherwise;
- _____( ) – returns true if the file denoted by the pathname exists and is a file, false otherwise;
- _____( ) – returns true if the file denoted by the pathname exists and is a directory, false otherwise;
- _____( ) – returns true if the file denoted by the pathname exists and the current program is allowed to write to that file, false otherwise.
- exists( )
- isFile( )
- isDirectory( )
- canWrite( )
The File class:
- Use the setPath() method to set the path to C:/Users/myName/Documents/M250/Activities/Unit12/Unit12_Project_1.
- Get the pathname of a file called README.TXT using thegetFileName() method.
- Create a File object using the pathname obtained in step 1.
- Using an if statement, test whether the new file object is associated with an actual physical file on disk by sending it an exists() message. If the file does exist, use an alert dialogue box to display the message, ‘A physical file exists!’ If no physical file exists, use an alert dialogue box to display the message, ‘No physical file exists!’
OUFileChooser.setPath(“C:/Users/myName/Documents/M250/Activities/Unit12/Unit12_Project_1”);
String pathname = OUFileChooser.getFilename();
File aFile = new File(pathname);
if (aFile.exists( ))
{
OUDialog.alert(“A physical file exists!”);
}
else
{
OUDialog.alert(“No physical file exists!”);
}
The File class:
Thinking back to what you learnt in Unit 8, when you execute the statement
File aFile = new File(pathname);
and pathname is null do you observe a compilation error or a run-time error?
The error is a run-time error, because whether or not an error is thrown depends on the value of pathname at run-time. It is also an example of an unchecked exception, specifically a NullPointerException. We would know that is an unchecked exception because, when the OUWorkspace runs the compiler, the compiler would not insist that we embed the call of the constructor within a try-catch statement.
Exceptions, files and streams:
Unit 8 introduced you to different kinds of error and emphasised the need to catch and recover from checked exceptions where possible.
Which statement do you use to handle an exception thrown by a method?
The try-catch statement is used.
Exceptions, files and streams:
A non-existence of a file is a potential problem when reading from files. Suggest some other potential problems of reading from and writing to files.
Possible problems include:
- Trying to overwrite a file that is read-only.
- Trying to write to a file when there is no space on the disk.
- Trying to read from a file that has become corrupted – so though it exists, it does not contain the data expected by the program.
Exceptions, files and streams:
Unchecked exceptions should not occur in normal program use, and their occurrence usually indicates that the programmer has failed to take into account a problem that was predictable and should have been guarded against.
Programming errors that can result in unchecked exceptions include:
- failing to test that an index is within the bounds of an array;
- dividing a number by zero; and,
- using null where an object was expected.
In this last case, a test is needed. Can you provide the if statement to ensure the program only runs if the pathname is not null (due to the user clicking cancel or “x”)
if (pathname != null)
{
File aFile = new File(pathname);
}
Exceptions, files and streams:
How can we tell at once that the FileWriter constructor may throw a checked exception from the following header:
public FileWriter(File file) throws IOException
because the header contains a throws clause.
———————————————
public FileWriter(File file) throws IOException
public File (String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
Parameters:
pathname – a pathname string
Throws:
NullPointerException – if the pathname argument is null