You are on page 1of 2

PERMANENT STORAGE – UNIT REVIEW PAGE 1 OF 2 COMPUTER SCIENCE

PERMANENT STORAGE – UNIT REVIEW


COMPUTER SCIENCE – IN JAVA AND C#
FILE BASICS:

 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)

USER FRIENDLY DIALOGS FOR FILES:

 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

This is where the code will branch to if a major


error is ‘caught’ in the try block

SEQUENTIAL FILE SAVING:

FileWriter object connects to a filename String or a


File object

PrintWriter object connects to the FileWriter object


or a File object

PrintWriter object calls the print() or println()


method to write the data type (string or otherwise) to the file

PrintWriter object must call the close() method to


sever the connection to the file

 The two java objects are used to create a buffer for


efficiency when connecting to the file
 The print() and println() methods can
accept most primitive data types, Strings, and even type Object
 To write multiple lines to a file, execute the println() method multiple times, each time a newline character will be written to the file
PERMANENT STORAGE – UNIT REVIEW FRIDAY, JANUARY 08, 2016 COMPUTER SCIENCE
PERMANENT STORAGE – UNIT REVIEW PAGE 2 OF 2 COMPUTER SCIENCE

SEQUENTIAL FILE OPENING:

FileReader object connects to a filename String or a File object

BufferedReader object connects to the FileReader object or a File object

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 FILE CLASS:

 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:

 With more complicated classes and ADTs, Java has a mechanism


known as serialization which is a process where Java can represent
an object as a sequence of bytes that includes the object’s data as
well as information about the object’s type and the types of data
stored in the object
 Once serialized, an object can be written to a file as that sequence
of bytes and then read back from the file and “deserialized” back to
recreate the same object (an object you have defined in your code)
in memory
 The imported objects used to stream serialized byte data to files
are: FileOutputStream, ObjectOutputStream,
FileInputStream, and ObjectInputStream with their
methods
 In addition, to allow this process, any class we want to serialize will
have to implement the Serializable interface
 Examples of this code can be found here

IN C#...

 Code in C# is similar but the names of the used (‘imported’) classes


are slightly different, as are the method calls, see an example of C#
code here:

PERMANENT STORAGE – UNIT REVIEW FRIDAY, JANUARY 08, 2016 COMPUTER SCIENCE

You might also like