Busy. Please wait.

show password
Forgot Password?

Don't have an account?  Sign up 

Username is available taken
show password


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
We do not share your email address with others. It is only used to allow you to reset your password. For details read our Privacy Policy and Terms of Service.

Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.
Don't know
remaining cards
To flip the current card, click it or press the Spacebar key.  To move the current card to one of the three colored boxes, click on the box.  You may also press the UP ARROW key to move the card to the "Know" box, the DOWN ARROW key to move the card to the "Don't know" box, or the RIGHT ARROW key to move the card to the Remaining box.  You may also click on the card displayed in any of the three boxes to bring that card back to the center.

Pass complete!

"Know" box contains:
Time elapsed:
restart all cards
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how

Chapter 13

File Input and Output

Volatile Storage Is temporary; are lost when a computer loses power. Most computer professionals call volatile storage memory
Random Access Memory(RAM) The temporary storage within a computer
Nonvolatile Storage Permanent storage; it is not lost when a computer loses power
Computer File A collection of data stored on a nonvolatile device. Files exist on permanent storage devices, such as hard disks, Zip disks, USB drives, reels or cassettes or magnetic tape, and compact disks
Text Files Contain data that can be read in a text editor because the data has been encoded using a scheme such as ASCII or Unicode
Data Files Some text files are data files that contain facts and figures, such as a payroll file that contains employee numbers, names, and salaries
Program or Application Files Files that store software instructions
Binary Files Contain data that has not bee encoded in text. Their contents are n binary format, which means that you cannot understand them by viewing them in a text editor
Root Directory When you store a permanent file, you can place it in the main or root directory or your storage device
Path A complete list of the disk drive plus the hierarchy or directories in which a file resides
Path Delimiter The backslash character(\) in the Windows operating system used to separate path components
Path Class Can be used to create object that contain information about files and directories, such as their locations, sizes, creation dates, and whether they even exist
Files Class Can be used to perform operations on files and directories, such as deleting them, determining their attributes, and creating input and output streams
Path and Files Import Package You can include the following statement in a Java program to use both the Path and Files classes... input java.nio.file.*;...The nio in java.nio stands for new input/output.
1. Creating a Path First determine the default file system on the host computer by using a statement such as follows... FileSystem fs = FileSystems.getDefault();...This statement creates a FileSystem object using getDefault() method in FileSystems class.
2. Creating a Path After you create a FileSystem object, you can define a Path using the getPath() method with it... Path path = fs.getPath("C:\\Java\\Chapter.13\\Data.txt");.
Factory Methods FileSystems, with an ending s, is a class that contains factory methods, which assist in object creation
1. Alternative Way to Create a Path Another way to create a Path is to use the Paths class. You can create a path object by using the following statement... Path filePath = Paths.get("C:\\Java\\Chapter.13\\SampleFile.txt");.
2. Alternative Way to Create a Path After the Path is created, you use its identifier(in this case, filePath) to refer to the file and perform operations on it
Absolute Path Every Path is either absolute or relative. An absolute path is a complete path; it does not need any other information to locate a file on a system. Ex: C:\\Java\\Chapter.13\\SampleFile.txt
Relative Path Depends on other path information. Ex: SampleFile.txt
1. Selected Path Class Methods 1. Method[String toString()]..Description[Returns the String representation of the Path, eliminating double backslashes]...
2. Selected Path Class Methods 2. Method[Path getFileName()]..Description[Returns the file or directory denoted by this Path; this is the last item in the sequence of name elements]...
3. Selected Path Class Methods 3. Method[int getNameCount()]..Description[Returns the number of name elements in the Path]... 4. Method[Path getName(int)]..Description[Returns the name in the position of the Path specified by the integer parameter]
toAbsolutePath() Method Converts a relative path to an absolute path
1. checkAccess() Method Verifies that a file exists and that the program can access it as needed. The import statement...import static java.nio.file.AccessMode.*;...allows you to access constants that can be used as arguments to the method.
2. checkAccess() Method Assuming that you have declared a Path named filePath, the syntax you use with checkAccess() is as follows... filePath.getFileSystem().provider().checkAccess();
1. Arguments to the checkAccess() Method 1. No argument - Checks that the file exists; you can substitute the Files.exists() method and pass it a Path argument... 2. READ - Checks that the file exist as and that the program has permission to read the file...
2. Arguments to the checkAccess() Method 3. WRITE - Checks that the file exists and that the program has permission to write to the file... 4. EXECUTE - Checks that the file exists and that the program has permission to execute the file
1. Deleting a Path The Files class delete() method accepts a path parameter and deletes the last element(file or directory) in a path or throws an exception if the deletion fails
2. Deleting a Path 1. If you try to delete a file that does not exist, a NoSuchFileException is thrown... 2. A directory cannot be deleted unless it is empty. If you attempt to delete a directory that contains files, a DirectoryNotEmptyException is thrown...
3. Deleting a Path 3. If you try to delete a file but you don't have permission, a SecurityException is thrown... 4. Other input/output errors cause an IOException
deleteIfExists() Method Can be used to delete a file, but if the file does not exist, no exception is thrown
1. readAttributes() Method A Files class method that retrieves useful information about a file. Takes two arguments - a Path object and BasicFileAttributes.class - and return an instance of the BasicFileAttributes class.
2. readAttributes() Method Ex: BasicFileAttributes art = Files.readAttributes(filePath, BasicFileAttributes.class);
BasicFileAttributes object Methods The size() method returns the size of a file in bytes. Methods creationTime() and lastModifiedTime() return important file times
FileTime Object Format FileTime objects are represented in the following format: yyy-mm-ddThh:mm:ss...The four-digit year is followed by the two-digit month and two-digit day. Following a T for Time, the hour, minute, and seconds are separated by colons
1. compareTo() Method Used to determine the time relationship between files. Returns a value less than 0 if first FileTime comes before the argument's FileTime and greater than 0 if first FileTime is later than argument's FileTime.
2. compareTo() Method It returns 0 if the FileTime values are the same
Character Can be any letter, number, or other special symbol that makes up data. This is the smallest useful piece of data to most users. Characters are made up of bits(ones and zeros that represent computer circuitry)
Field A group of characters that has some meaning. Ex: The characters T, o, and m might represent you first name(Tom). Characters are grouped together into fields
Record A collection of fields that contain data about an entity. Fields are grouped together to form records. For example, a persons first and last names, zip code, and salary represent that person's record
What Data Files Consist of Data files consist or related records, such as a company's personnel file that contains one record for each company employee. Records are grouped to create files
Sequential Access File A data file can be used as a sequential access file when each record is accessed one after another in the order in which it was stored. Often each record is stored in order based on the value in some field.
Comma-separated Values(CSV) A file in which each value in a record is separated from the next by a comma; CSV is a widely used format for files used in all sorts of applications, including databases and spreadsheets
Opening a File You must open a file before the application can use it. A Java application opens a file by creating an object and associated a stream of bytes with it
Closing a File When you finish using file, program should close it(make it not available). If you fail to close an input file(reading data from it) there are not serious consequences. If you fail to close an output file(writing data to it) data might become inaccessible
1. Stream A stream is an object, and like all object, streams have data and methods. The methods allows you to perform actions such as opening, closing, reading, and writing.
2. Stream When you perform input operations in an application, bytes flowing into your program from an input device through a stream, which functions as a pipeline or channel. Most streams flow in only one direction; each stream is either a input or output stream
Buffer A memory location where bytes are held after they are logically output but before they are sent to the output device. Using a buffer to accumulate input or output before issuing the actual IO command improves program performance
Flushing Clears any bytes that have been sent to a buffer for output but have not yet been output to a hardware device. When you use an output buffer, you sometimes flush it before closing it
1. Classes Used for Input and Output 1. Class[InputStream]..Description[Abstract class that contains method for performing input]... 2. Class[FileInputStream]..Description[Child of InputStream that provides the capability to read from disk files]...
2. Classes Used for Input and Output 3. Class[BufferedInputStream]..Description[Child of FIlterInputStream, which is a child of InputStream; BufferedInputStream handles input from a system's standard input device, usually the keyboard]...
3. Classes Used for Input and Output 4. Class[OutputStream]..Description[Abstract class that contains method for performing output]... 5. Class[FileOutputStream]..Description[Child of OutputStream that allows you to write to disk files]...
4. Classes Used for Input and Output 6. Class[BufferedOuptutStrean]..Description[Child of FIlterOutputStream, which is a child of OuptuStream; BufferedOutputStream handles input from a system's standarad output device, usually the monitor]...
5. Classes Used for Input and Output 7. Class[PrintStream]..Description[Child of FilterOutputStream, which is child of OutputStream; System.out is a PrintStream object]...
6. Classes Used for Input and Output 8. Class[Reader]..Description[Abstract class for reading character streams; the only methods that a subclass must implement are rea(char[], int, int) and close()]...
7. Classes Used for Input and Output 9. Class[BufferedReader]..Description[Reads text from a character-input stream, buffering characters t provide for efficient reading of characters, arrays, and lines]...
8. Classes Used for Input and Output 10. Class[BufferedWriter]..Description[Writes text to a character-output stream, buffering characters to provide for the efficient writing of characters, arrays, and lines]
1. OutputStream Methods 1. Method[void close()]..Description[Closes the output stream and releases any system resources associated with the stream]...
2. OutputStream Methods 2. Method[void flush()]..Description[Flushes the output stream, if any bytes are buffered, they will be written]... 3. Method[void write(byte[] b)]..Description[Writes all the bytes to the output stream from the specified byte array]....
3. OutputStream Methods 4. Method[void write(byte[] b, int off, int len)]..Description[Writes bytes to the output stream from the specified byte array starting at offset position off for a length of len characters]
1. newOutputStream() Method You can create a writeable file by using the Files class newOutputStream() method. You pass a Path and a StarndardOpenOption argument to this method.
2. newOutputStream() Method Creates a file if it does not exist, opens for writing, and returns an OutputStream that can be used to write bytes to the file.
1. StandardOpenOption Constants 1. Constant[WRITE]..Description[Opens the file for writing]... 2. Constant[APPEND]..Description[Appends new data to the end of the file; using this option with WRITE or CREATE]...
2. StandardOpenOption Constants 3. Constant[TRUNCATE_EXISTING]..Description[Truncates the existing file to 0 bytes so the file contents are replaced; use this option with the WRITE option]...
3. StandardOpenOption Constants 4. Constant[CREATE_NEW]..Description[Creates a new file only if it does not exist; throws an exception if the file already exists]...
4. StandardOpenOption Constants 5. Constant[CREATE]..Description[Opens the file if it exists ro creates a new file if it does not exist]... 6. Constant[DELETE_ON_CLOSE]..Description[Deletes file when stream closed; often used for temporary files existing only for duration of program]
newInputStream() Method To open a file for reading, you can use the Files class newInputStream() method. Accepts a Path parameter and return a stream that can read bytes from a file
1. BufferedReader Methods 1. Method[close()]..Description[Closes the stream and any resources associated with it]... 2. Method[read()]..Description[Reads a single character]...
2. BufferedReader Methods 3. Method[read(char[] buffer, int off, int len)]..Description[Reads character into a portion of an array from position off for len characters]... 4. Method[readLine()]..Description[Reads a line of text]...
3. BufferedReader Methods 5. Method[skip(long n)]..Description[Skips the specified number of characters]
1. BufferedWriter Method 1. Method[close()]..Description[Closes the stream, flushing it first]... 2. Method[flush()]..Description[Flushes the stream]... 3. Method[newline()]..Description[Writes a line separator]...
2. BufferedWriter Method 4. Method[write(String s, int off, int len)]..Description[Writes a String from position off for length len]... 5. Method[write(char[], int off, int len)]..Description[Writes a character array from position off for length len]...
3. BufferedWriter Method 6. Method[write(int c)]..Description[Writes a single character]
String Class split() Method Accepts an arguments that identifies a field delimiter and returns an array of Strings in which each array element holds one field. Then you can use methods such as parseInt() and parseDouble() to convert the split strings to other data types
Batch Processing Involves performing the same tasks with many records, one after the other
Real-time Applications For many applications, sequential access is inefficient. These applications, known as real-time applications, require that a record be accessed immediately while a client is waiting
Interactive Program A program in which the user makes direct requests.
Random Access Files Files in which records can be retrieved directly in any order. Random files are also called direct access files and instant access files
File Channel A file channel object is an avenue for reading and writing a file. You can use Java's FileChannel class to create your own random access files
Seekable A file channel is seekable, meaning you can search for a specific file location and operations can start at any specified position
1. FileChanel Methods 1. Method[FileChannel open(Path file, OpenOption...options)]..Description[Opens or creates a file, returning a file channel to access the file]...
2. FileChanel Methods 2. Method[long position()]..Description[Returns the channel's file position]... 3. Method[FileChannel position(long newPosition)]..Description[Sets the channel's file position]...
3. FileChanel Methods 4. Method[int read(ByteBuffer buffer)]..Description[Reads a sequence of bytes from the channel into the buffer]... 5. Method[long size()]..Description[Returns the size of the channel's size]...
4. FileChanel Methods 6. Method[int write(ByteBuffer buffer)]..Description[Writes a sequence of bytes to the channel from the buffer]
ByteBuffer Is simply a holding place for bytes waiting to be read or written.
ByteBuffer wrap() Method An array of bytes can be wrapped, or encompassed, into a ByteBuffer using this method. Wrapping a byte array into a buffer causes changed made to the buffer to change the array, and causes changes made to the array to change the buffer
Key Field The field in a record that makes the record unique from all others