Exam 10: File IO
An ____________ allows data to flow from your program.
B
Explain the differences between a text file,an ASCII file and a binary file.
Text files are files that appear to contain sequences of characters when viewed in a text editor or read by a program.Text files are sometimes also called ASCII files because they contain data encoded using a scheme known as ASCII coding.Files whose contents must be handled as sequences of binary digits are called binary files.
Write a complete Java program using a BufferedReader object that opens a file named autos.txt and displays each line to the screen.
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; public class TextDemo { public static void main(String args[]) { BufferedReader inputStream = null; try { inputStream = new BufferedReader(new FileReader("autos.txt")); String line = inputStream.readLine(); while(line != null) { System.out.println(line); line = inputStream.readLine(); } inputStream.close(); } catch(FileNotFoundException e) { System.out.println("Error opening files."); } catch(IOException e) { System.out.println("Error reading from file."); } } }
Using BufferedReader to read integers from a file requires the String input to be parsed to an integer type.
Write a complete Java program using a Scanner object that opens a file named autos.txt and displays each line to the screen.
Write a complete Java program that opens a binary file containing integers and displays the contents to the screen.
The methods of the scanner class do not behave the same when reading from a text file as they do when used to read from the keyboard.
The class ObjectOutputStream contains all of the following methods except:
The method _________ reads a single character from an input stream.
The class ObjectInputStream contains all of the following methods except:
The preferred stream classes for processing binary files are ObjectInputStream and ObjectOutputStream.
Write a Java statement that creates a stream that provides read/write access to the file named autos.txt.
Use the output stream to the file autos.txt created above in number 2 to write the line "Mercedes" to the file.
Every input file and every output file used by your program has only one name which is the same name used by the operating system.
The scanner class has a series of methods that checks to see if there is any more well-formed input of the appropriate type.These methods are called __________ methods:
The File class contains methods that allow you to check various properties of a file.
The read()method of the class RandomAccessFile returns the type:
Use the output stream created in number 11 above to write the String BBC to the file named statistics.dat.
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)