Deck 13: File Input and Output
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Match between columns
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/66
Play
Full screen (f)
Deck 13: File Input and Output
1
Some text files are ____ files that contain facts and figures, such as a payroll file that contains employee numbers, names, and salaries.
A) application
B) volatile
C) data
D) program
A) application
B) volatile
C) data
D) program
C
2
Java lets you assign a file to a(n) ____ object so that screen output and file output work in exactly the same manner.
A) Stream
B) Input
C) Output
D) File
A) Stream
B) Input
C) Output
D) File
A
3
FileSystems is a class that contains ____ methods, which assist in object creation.
A) factory
B) file
C) abstract
D) system
A) factory
B) file
C) abstract
D) system
A
4
You can use Java's ____ class to create objects that contain information about files or directories, such as their locations, sizes, creation dates, and whether they even exist.
A) Directory
B) Property
C) Path
D) File
A) Directory
B) Property
C) Path
D) File
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
5
A ____ is a group of characters that has some meaning.
A) record
B) file
C) byte
D) field
A) record
B) file
C) byte
D) field
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
6
Comma-separated values (CSV) is a file format strictly used for working with Java and databases.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
7
When you use the BufferedReader class, you must import the java.io package into your program.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
8
A data file can be used as a(n) ____ file when each record is accessed one after another in the order in which it was stored.
A) application
B) sequential access
C) stream
D) field
A) application
B) sequential access
C) stream
D) field
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
9
You can direct System.err to a new location, such as a disk file or printer, but you cannot direct System.out to a new location.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
10
After you create a FileSystem object, you can define a Path using the ____ method with it.
A) getPath()
B) createPath()
C) setPath()
D) getDefault()
A) getPath()
B) createPath()
C) setPath()
D) getDefault()
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
11
____ is an abstract class for reading character streams.
A) System.out
B) Reader
C) System.err
D) OutStream
A) System.out
B) Reader
C) System.err
D) OutStream
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
12
Java's Path class is used to create objects that contain information about files and directories, while the Files class performs operations on files and directories.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
13
The top-level element in a Path 's directory structure is located at index 1.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
14
In random access files, records can be retrieved in any order.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
15
If you simply want to display records in order based on their key field, you need to create a random access file.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
16
You can store data in variables within a program, but this type of storage is temporary.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
17
Permanent storage is usually called computer memory or random access memory (RAM).
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
18
InputStream is a child of FileInputStream .
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
19
InputStream and OutputStream are subclasses of the ____ class.
A) IO
B) Object
C) Stream
D) IOStream
A) IO
B) Object
C) Stream
D) IOStream
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
20
Placing a file in the ____ directory of your storage device is equivalent to tossing a loose document into a drawer.
A) root
B) path
C) back
D) loose
A) root
B) path
C) back
D) loose
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
21
Random files are also called ____________________ files.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
22
Because the backslash character starts the escape sequence in Java, you must use two ____ in a string that describes a Path in the DOS operating system.
A) dashes
B) quotes
C) backslashes
D) periods
A) dashes
B) quotes
C) backslashes
D) periods
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
23
The true benefit of using a(n) ____ file is the ability to retrieve a specific record from a file directly, without reading through other records to locate the desired one.
A) random access
B) text
C) open
D) input
A) random access
B) text
C) open
D) input
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
24
The BufferedWriter class contains a ____ method that uses the current platform's line separator.
A) lineSeparator()
B) systemSeparator()
C) newLine()
D) newSeparator()
A) lineSeparator()
B) systemSeparator()
C) newLine()
D) newSeparator()
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
25
You can use Java's ____ class to create your own random access files.
A) Path
B) FileStream
C) File
D) FileChannel
A) Path
B) FileStream
C) File
D) FileChannel
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
26
A(n) ____________________ is a collection of fields that contain data about an entity.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
27
A(n) ____________________ object is an avenue for reading and writing a file.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
28
The String class ____ method accepts an argument that identifies the field delimiter and returns an array of String s.
A) tokens()
B) divide()
C) tokenize()
D) split()
A) tokens()
B) divide()
C) tokenize()
D) split()
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
29
____ applications require that a record be accessed immediately while a client is waiting.
A) Sequential
B) Dependent
C) Real-time
D) Batch
A) Sequential
B) Dependent
C) Real-time
D) Batch
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
30
A(n) ____ field is the field in a record that makes the record unique from all others.
A) unique
B) key
C) search
D) individual
A) unique
B) key
C) search
D) individual
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
31
____________________ is an abstract class that contains methods for performing output.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
32
A file channel is ____, meaning you can search for a specific file location and operations can start at any specified position.
A) moveable
B) seekable
C) flexible
D) dynamic
A) moveable
B) seekable
C) flexible
D) dynamic
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
33
When you use the BufferedReader class, you must import the ____ package into your program.
A) java.nio.file
B) java.io
C) java.nio
D) java.io.input
A) java.nio.file
B) java.io
C) java.nio
D) java.io.input
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
34
A(n) ____ is a holding place for bytes that are waiting to be read or written.
A) ByteChannel
B) FileChannel
C) InputStream
D) ByteBuffer
A) ByteChannel
B) FileChannel
C) InputStream
D) ByteBuffer
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
35
An array of bytes can be wrapped, or encompassed, into a ByteBuffer using the ByteBuffer ____ method.
A) wrap()
B) convert()
C) export()
D) toArray()
A) wrap()
B) convert()
C) export()
D) toArray()
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
36
The ____ method returns the last Path element in a list of pathnames.
A) toString()
B) getNameCount()
C) getFileName()
D) getName(int)
A) toString()
B) getNameCount()
C) getFileName()
D) getName(int)
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
37
Which of the following statements will write a line separator?
A) newline();
B) BufferedWriter.newline();
C) FileChannel.newline();
D) ByteBuffer.newline();
A) newline();
B) BufferedWriter.newline();
C) FileChannel.newline();
D) ByteBuffer.newline();
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
38
Any of the file input or output methods in a Java program might throw an exception, so all the relevant code in the class is placed in a ____ block.
A) finally
B) throw
C) try
D) catch
A) finally
B) throw
C) try
D) catch
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
39
The value you store in memory is lost when the program ends or the computer loses power. This type of storage device is called ____________________.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
40
You can create a writeable file by using the Files class ____ method.
A) getOutputStream()
B) newOutputStream()
C) newFileOutputStream()
D) fileOutputStream()
A) getOutputStream()
B) newOutputStream()
C) newFileOutputStream()
D) fileOutputStream()
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
41
Describe and provide an example of the differences between real-time applications and interactive programs.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
42
Match between columns
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
43
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException;
public class PathDemo5
{
public static void main(String[] args)
{
Path myPath = Paths.get("C:\\Java\\Input.Output\\MyData.txt");
try
{
---------Code here ----------
System.out.println("Creation time " + attr.creationTime());
System.out.println("Last modified time " + attr.lastModifiedTime());
System.out.println("Size " + attr.size());
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
}
Using the above code, create a statement on the indicated line that uses the readAttributes() method of the Files class that takes two arguments-the Path object created in the code and a BasicFileAttributes.class -and returns an instance of the BasicFileAttributes class named attr .
import java.nio.file.attribute.*;
import java.io.IOException;
public class PathDemo5
{
public static void main(String[] args)
{
Path myPath = Paths.get("C:\\Java\\Input.Output\\MyData.txt");
try
{
---------Code here ----------
System.out.println("Creation time " + attr.creationTime());
System.out.println("Last modified time " + attr.lastModifiedTime());
System.out.println("Size " + attr.size());
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
}
Using the above code, create a statement on the indicated line that uses the readAttributes() method of the Files class that takes two arguments-the Path object created in the code and a BasicFileAttributes.class -and returns an instance of the BasicFileAttributes class named attr .
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
44
while(nextLine = reader.readLine() != null)
System.out.println(nextLine);
The above loop could be used to read multiple lines from a file. Explain how this loop will execute.
System.out.println(nextLine);
The above loop could be used to read multiple lines from a file. Explain how this loop will execute.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
45
What tasks are typically performed when working with stored files in an application?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
46
When you perform input and output operations in an application, what is actually occurring with the bytes of information?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
47
import java.util.Scanner;
import java.nio.file.*;
public class PathDemo2
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a file name >> ");
name = keyboard.nextLine();
Path inputPath = Paths.get(name);
------- Code here -----
System.out.println("Full path is " + fullPath.toString());
}
} Using the above code, add a statement on the indicated line that creates an absolute path by assigning the file to the current directory.
import java.nio.file.*;
public class PathDemo2
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a file name >> ");
name = keyboard.nextLine();
Path inputPath = Paths.get(name);
------- Code here -----
System.out.println("Full path is " + fullPath.toString());
}
} Using the above code, add a statement on the indicated line that creates an absolute path by assigning the file to the current directory.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
48
What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
49
Briefly describe the Path class in Java.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
50
writer.write(names, 0, names.length());
The above code represents a write() method of the BufferedWriter class. Describe the purpose of the method and how it accepts the String characters.
The above code represents a write() method of the BufferedWriter class. Describe the purpose of the method and how it accepts the String characters.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
51
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class CreateEmployeesRandomFile
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Path file =
Paths.get("C:\\Java\\Chapter.13\\RandomEmployees.txt");
String s = "000, ,00.00" +
System.getProperty("line.separator");
final int RECSIZE = s.length();
FileChannel fc = null;
String delimiter = ",";
String idString;
int id;
String name;
String payRate;
final String QUIT = "999";
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
System.out.print("Enter employee ID number >> ");
idString = input.nextLine();
while(!(idString.equals(QUIT)))
{
----Code here-----
System.out.print("Enter name for employee #" +
id + " >> ");
name = input.nextLine();
System.out.print("Enter pay rate >> ");
payRate = input.nextLine();
s = idString + delimiter + name + delimiter +
payRate + System.getProperty("line.separator");
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
----Code here-----
fc.write(buffer);
System.out.print("Enter next ID number or " +
QUIT + " to quit >> ");
idString = input.nextLine();
}
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
The above program will accept any number of employee records as user input and write the records to a file in a loop. In the first indicated line, create the statement to accept the employee data value from the keyboard as a String and convert it to an integer using the parseInt() method. In the second indicated line, create the statement to compute the record's desired position by multiplying the ID number value by the record size.
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class CreateEmployeesRandomFile
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Path file =
Paths.get("C:\\Java\\Chapter.13\\RandomEmployees.txt");
String s = "000, ,00.00" +
System.getProperty("line.separator");
final int RECSIZE = s.length();
FileChannel fc = null;
String delimiter = ",";
String idString;
int id;
String name;
String payRate;
final String QUIT = "999";
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
System.out.print("Enter employee ID number >> ");
idString = input.nextLine();
while(!(idString.equals(QUIT)))
{
----Code here-----
System.out.print("Enter name for employee #" +
id + " >> ");
name = input.nextLine();
System.out.print("Enter pay rate >> ");
payRate = input.nextLine();
s = idString + delimiter + name + delimiter +
payRate + System.getProperty("line.separator");
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
----Code here-----
fc.write(buffer);
System.out.print("Enter next ID number or " +
QUIT + " to quit >> ");
idString = input.nextLine();
}
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
The above program will accept any number of employee records as user input and write the records to a file in a loop. In the first indicated line, create the statement to accept the employee data value from the keyboard as a String and convert it to an integer using the parseInt() method. In the second indicated line, create the statement to compute the record's desired position by multiplying the ID number value by the record size.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
52
Explain the difference between an absolute path and a relative path. Provide an example of an absolute path to a file on a system, and then provide an example of a relative path.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
53
How can you categorize files by the way they store data?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
54
Briefly describe the ByteBuffer class.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
55
How can you assign program output to a file, rather than to the standard output device? Explain how this works. Give an example.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
56
FileChannel fc = null;
Path file =
Paths.get("C:\\Java\\Chapter.13\\RandomEmployees.txt");
Given the two declarations above, write a Java statement that creates a ByteChannel that is cast to a FileChannel . The file shoould be open for read and write operations.
Path file =
Paths.get("C:\\Java\\Chapter.13\\RandomEmployees.txt");
Given the two declarations above, write a Java statement that creates a ByteChannel that is cast to a FileChannel . The file shoould be open for read and write operations.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
57
Provide an example of batch processing.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
58
When a String is written, placing each record on a new line makes the output file easier to read and interpret. Since not all platforms use '\n' to separate lines, the BufferedWriter class has two alternatives: the newLine() method and the System.getProperty() method. Describe these two methods and how they operate.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
59
How can you write the system's newline character to a file in Java?
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
60
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class CreateOneRandomAccessRecord
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\RandomEmployees.txt");
String s = "002,Newmann,12.25" +
System.getProperty("line.separator");
--------- Code here -------------
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
FileChannel fc = null;
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
fc.position(2 * RECSIZE);
fc.write(buffer);
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
}
}
The above program creates a single employee record for employee number 002 with a last name of Newmann and a pay rate of 12.25. In the indicated space, create a statement that assigns the length of the string to RECSIZE .
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class CreateOneRandomAccessRecord
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\RandomEmployees.txt");
String s = "002,Newmann,12.25" +
System.getProperty("line.separator");
--------- Code here -------------
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
FileChannel fc = null;
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
fc.position(2 * RECSIZE);
fc.write(buffer);
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
}
}
The above program creates a single employee record for employee number 002 with a last name of Newmann and a pay rate of 12.25. In the indicated space, create a statement that assigns the length of the string to RECSIZE .
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
61
import java.nio.file.*;
import java.io.*;
public class ReadEmployeeFile2
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\Employees.txt");
String[] array = new String[3];
String s = "";
String delimiter = ",";
int id;
String name;
double payRate;
double gross;
final double HRS_IN_WEEK = 40;
double total = 0;
try
{
InputStream input = new
BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new
BufferedReader(new InputStreamReader(input));
System.out.println();
s = reader.readLine();
while(s != null)
{
-----Code here-----
id = Integer.parseInt(array[0]);
name = array[1];
payRate = Double.parseDouble(array[2]);
gross = payRate * HRS_IN_WEEK;
System.out.println("ID#" + id + " " + name +
" $" + payRate + " $" + gross);
total += gross;
s = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
System.out.println(" Total gross payroll is $" + total);
}
}
The above code demonstrates a retrieved file where Strings are split into usable fields. On the indicated line, write a String class split() method to split the String s that accepts an argument that identifies the field delimiter (in this example, a comma) and returns an array of Strings . The result should be assigned to the String[] array .
import java.io.*;
public class ReadEmployeeFile2
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\Employees.txt");
String[] array = new String[3];
String s = "";
String delimiter = ",";
int id;
String name;
double payRate;
double gross;
final double HRS_IN_WEEK = 40;
double total = 0;
try
{
InputStream input = new
BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new
BufferedReader(new InputStreamReader(input));
System.out.println();
s = reader.readLine();
while(s != null)
{
-----Code here-----
id = Integer.parseInt(array[0]);
name = array[1];
payRate = Double.parseDouble(array[2]);
gross = payRate * HRS_IN_WEEK;
System.out.println("ID#" + id + " " + name +
" $" + payRate + " $" + gross);
total += gross;
s = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
System.out.println(" Total gross payroll is $" + total);
}
}
The above code demonstrates a retrieved file where Strings are split into usable fields. On the indicated line, write a String class split() method to split the String s that accepts an argument that identifies the field delimiter (in this example, a comma) and returns an array of Strings . The result should be assigned to the String[] array .
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
62
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
public class WriteFile
{
public static void main(String[] args)
{
Path myFile = Paths.get("C:\\Java\\Input.Output\\myNumbers.txt");
String nums = "12345";
byte[] data = nums.getBytes();
OutputStream output = null;
try
{
-----Code here------
output.write(data)
output.flush();
output.close();
}
catch(Exception badNums)
{
System.out.println("My numbers: " + badNums);
}
}
}
Using the above code, add a statement on the indicated line that will create a writeable file by constructing a BufferedOutputStream object and assigning it to the OutputStream . No output should appear on the monitor, but a file should be created.
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
public class WriteFile
{
public static void main(String[] args)
{
Path myFile = Paths.get("C:\\Java\\Input.Output\\myNumbers.txt");
String nums = "12345";
byte[] data = nums.getBytes();
OutputStream output = null;
try
{
-----Code here------
output.write(data)
output.flush();
output.close();
}
catch(Exception badNums)
{
System.out.println("My numbers: " + badNums);
}
}
}
Using the above code, add a statement on the indicated line that will create a writeable file by constructing a BufferedOutputStream object and assigning it to the OutputStream . No output should appear on the monitor, but a file should be created.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
63
import java.nio.file.*;
import static java.nio.file.AccessMode.*;
import java.io.IOException;
public class PathDemo3
{
public static void main(String[] args)
{
Path myFile = Paths.get("C:\\Java\\Chapter.13\\Data.txt");
System.out.println("Path is " + myFile.toString());
try {
-------------- Code here -------------------
System.out.println("File can be read and executed"); }
catch(IOException e)
{
System.out.println("File cannot be used for this application");
}
}
}
Assuming you have declared a path named myFile , create the checkAccess() method on the indicated line that will verify that the file exists, and checks that the program can read and write to the file.
import static java.nio.file.AccessMode.*;
import java.io.IOException;
public class PathDemo3
{
public static void main(String[] args)
{
Path myFile = Paths.get("C:\\Java\\Chapter.13\\Data.txt");
System.out.println("Path is " + myFile.toString());
try {
-------------- Code here -------------------
System.out.println("File can be read and executed"); }
catch(IOException e)
{
System.out.println("File cannot be used for this application");
}
}
}
Assuming you have declared a path named myFile , create the checkAccess() method on the indicated line that will verify that the file exists, and checks that the program can read and write to the file.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
64
import java.nio.file.*;
import java.io.*;
public class ReadEmployeeFile
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\Employees.txt");
String s = "";
try
{
InputStream input = new
BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new
BufferedReader(new InputStreamReader(input));
-----Code here-----
while(s != null)
{
System.out.println(s);
-----Code here-----
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
} The above code represents a program that reads an Employees text file. An InputStream is declared for the file, and a BufferedReader is created using the InputStream . On the indicated lines, write a statement that will read the first line into the String . Likewise, in the while statement, write the statement to display the String and read a new line as long as the readLine() method does not return a null value.
import java.io.*;
public class ReadEmployeeFile
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\Employees.txt");
String s = "";
try
{
InputStream input = new
BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new
BufferedReader(new InputStreamReader(input));
-----Code here-----
while(s != null)
{
System.out.println(s);
-----Code here-----
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
} The above code represents a program that reads an Employees text file. An InputStream is declared for the file, and a BufferedReader is created using the InputStream . On the indicated lines, write a statement that will read the first line into the String . Likewise, in the while statement, write the statement to display the String and read a new line as long as the readLine() method does not return a null value.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
65
import java.nio.file.*;
public class PathDemo
{
public static void main(String[] args)
{
Path filePath = Paths.get("C:\\Java\\Input.Output\\LessonInfo.txt");
int count = filePath.getNameCount();
System.out.println("Path is " + filePath.toString());
System.out.println("File name is " +
filePath.getFileName());
System.out.println("There are " + count +
" elements in the file path");
for(int x = 0; x
System.out.println("Element " + x + " is " +
filePath.getName(x));
}
}
The above code demonstrates the creation of a Path and its method. Describe the output that will display when the code is executed.
public class PathDemo
{
public static void main(String[] args)
{
Path filePath = Paths.get("C:\\Java\\Input.Output\\LessonInfo.txt");
int count = filePath.getNameCount();
System.out.println("Path is " + filePath.toString());
System.out.println("File name is " +
filePath.getFileName());
System.out.println("There are " + count +
" elements in the file path");
for(int x = 0; x
System.out.println("Element " + x + " is " +
filePath.getName(x));
}
}
The above code demonstrates the creation of a Path and its method. Describe the output that will display when the code is executed.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck
66
import java.nio.file.*;
import java.io.*;
public class ReadFile
{
public static void main(String[] args)
{
Path file = Paths.get("C:\\Java\\Input.Output\\Grades.txt");
InputStream input = null;
try
{
----Code here-------
----Code here-------
String grade = null;
grade = reader.readLine();
System.out.println(grade);
input.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
} In the code above, a ReadFile class reads from the Grades.txt file. A path is declared, and an InputStream is declared using the Path . In the first indicated line, create the statement to assign a stream to the InputStream reference. In the second indicated line, declare a BufferedReader that reads a line of text from a character-input stream that buffers characters for more efficient reading.
import java.io.*;
public class ReadFile
{
public static void main(String[] args)
{
Path file = Paths.get("C:\\Java\\Input.Output\\Grades.txt");
InputStream input = null;
try
{
----Code here-------
----Code here-------
String grade = null;
grade = reader.readLine();
System.out.println(grade);
input.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
} In the code above, a ReadFile class reads from the Grades.txt file. A path is declared, and an InputStream is declared using the Path . In the first indicated line, create the statement to assign a stream to the InputStream reference. In the second indicated line, declare a BufferedReader that reads a line of text from a character-input stream that buffers characters for more efficient reading.
Unlock Deck
Unlock for access to all 66 flashcards in this deck.
Unlock Deck
k this deck