04 - Permanent Storage (Unit Review)
04 - Permanent Storage (Unit Review)
04 - Permanent Storage (Unit Review)
Files are permanent (also known as persistent or non-volatile) storage of data on a file system (e.g. hard drive, flash drive, etc.) as opposed to
temporary (also known as volatile) memory in things like variables, arrays, etc.
Files are organized on a file system by a first name (also known as a file path starting at a root directory), a middle name (more commonly called
the file’s name itself – which is often the only part the user names when saving or opening a file), and the last name (also known as the file
extension, which identifies the type of file stored in the file system)
No two files on a system can have the same first, middle, and last name (and some file system apply other restrictions such as the type of
characters allowed in the names, the length of the names, etc.)
In a sequential file, data is stored in the file as a stream of text based data (including characters like line breaks and end of file markers)
Java makes use of three default streams of data (System.out, System.in, and System.err) to stream from and to default devices (the
keyboard and the monitor) but streams can be redirected to file objects
To redirect streams to files, the java.io core library will be imported for input/output
As well, to make file handling more efficient, java uses buffer objects (waiting lines) and error handling (error traps)
The imported javax.swing extention library can use a class called JFileChooser to show a user-friendly dialog box for saving and opening files, including the
following useful methods:
- The overloaded constructors can set the opening dialog to a specific directory
- setDialogTitle(String) – sets the top title of the dialog box to the String parameter
- setCurrentDirectory(File) – sets the starting directory of the dialog box to the File object directory
- setFileSelectionMode(int) – sets the dialog box to just select files, directories, or both
- setMultiSelctionEnabled(boolean) – sets whether or not the user can select multiple files
- showOpenDialog(parent) – displays the open file dialog box
- showSaveDialog(parent) – displays the save file dialog box
- getSelectedFile() – returns a File object selected by the user
- getSelectedFiles() – returns an array of File[] objects selected by the user
ERROR HANDLING:
The try catch block will handle exceptions and is necessarily All the code that you want to ‘try’ to execute
enforced when using file handling goes here in this block
For example:
This is the type of error to ‘catch’ that is
‘thrown’ from the try block
BufferedReader object calls the read() or readLine() method to read from the file
BufferedReader object must call the close() method to sever the connection to the file
The readLine() method reads all data up to, but not including the newline
character
The read() method reads a single character from the file and returns it’s ascii
value as a int
If the readLine() method returns a null value, it has reached the end of
the file, if the read() method returns a -1 then it has read the end of file
marker
The java.io core library class File can be instantiated and has several useful methods for working with files, methods include:
- canExecute() – returns a Boolean whether or not this file is executable
- createNewFile() – creates a new file with the constructed filename
- delete() – deletes the file with the constructed filename
- getAbsolutePath() – returns a String of the first, middle, and last name of the constructed filename
- isDirectory() – returns a Boolean determining whether or not the constructed filename is actually a directory
- isFile() – returns a Boolean determining whether or not the constructed filename is actually a normal file
- isHidden() – returns a Boolean determining whether or not the constructed filename is actually a hidden file
- length() – returns a long of the time indicated by the file system when this file was last modified
- lastModified() – returns a long of the number of bytes this file occupies on the file system
- list() – returns a String array of all filenames in the directory of the constructed filename (if it is a directory)
- listFiles() – returns a File object array of all filenames of the constructed filename (if it is a directory)
- mkdir() – creates a directory with the constructed filename
- renameTo(File) – renames the file with the constructed filename to the parameter’s constructed filename
SAVING OBJECTS:
IN C#...
PERMANENT STORAGE – UNIT REVIEW FRIDAY, JANUARY 08, 2016 COMPUTER SCIENCE