Deck 13: File Input and Output
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/74
العب
ملء الشاشة (f)
Deck 13: File Input and Output
1
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
2
The top-level element in a Path's directory structure is located at index 1.
False
3
Comma-separated values (CSV) is a file format strictly used for working with Java and databases.
False
4
____ is an abstract class used in Java for input and output (IO) operations.
A) System.out
B) Reader
C) System.err
D) OutStream
A) System.out
B) Reader
C) System.err
D) OutStream
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
5
You can store data in variables within a program, but this type of storage is temporary.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
6
A data file can be used as a(n) ____ file when each record is stored in order based on the value in some field.
A) application
B) sequential access
C) stream
D) field
A) application
B) sequential access
C) stream
D) field
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
7
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
8
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
9
When you store records in a file, it is often more useful to be able to access the 34th record rather than the 34th byte.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
10
InputStream and OutputStream are subclasses of the ____ class.
A) IO
B) Object
C) Stream
D) IOStream
A) IO
B) Object
C) Stream
D) IOStream
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
11
Even if you simply want to display records in order based on their key field, you still need to create a random access file.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
12
In random access files, records can be retrieved directly in any order.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
13
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
14
InputStream is a child of FileInputStream.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
15
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
16
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
17
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
18
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()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
19
Permanent storage is usually called computer memory or random access memory (RAM).
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
20
While System.err and System.out are both directed by default to the monitor command line, System.err is usually reserved for error messages and System.out is reserved for valid output.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
21
The String class ____ method accepts an argument that identifies the field delimiter and returns an array of Strings.
A) tokens()
B) divide()
C) tokenize()
D) split()
A) tokens()
B) divide()
C) tokenize()
D) split()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
22
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
23
A(n) ____________________ object is an avenue for reading and writing a file.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
24
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();
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
25
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
26
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
27
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()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
28
____________________ is an abstract class that contains methods for performing output.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
29
Random files are also called ____________________ files.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
30
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()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
31
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
32
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()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
33
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
34
A(n) ____________________ is a collection of fields that contain data about an entity.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
35
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
36
____ 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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
37
The value you store in memory is lost when the program ends or the computer loses power. This type of storage device is called ____________________.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
38
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
39
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)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
40
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
41
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Writes text to an output stream, buffering the characters
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Writes text to an output stream, buffering the characters
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
42
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
The main directory of your storage device
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
The main directory of your storage device
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
43
How can you assign program output to a file, rather than to the standard output device? Explain how this works. Give an example.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
44
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
45
When you perform input and output operations in an application, what is actually occurring with the bytes of information?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
46
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
To make files no longer available to your application
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
To make files no longer available to your application
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
47
How can you categorize files by the way they store data?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
48
Briefly describe the ByteBuffer class.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
49
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
The memory location where bytes are held after they are logically output but before they are sent to the output device
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
The memory location where bytes are held after they are logically output but before they are sent to the output device
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
50
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Functions as a pipeline or channel between a Java program and an input device
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Functions as a pipeline or channel between a Java program and an input device
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
51
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
A collection of related records
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
A collection of related records
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
52
Provide an example of batch processing.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
53
What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
54
Describe and provide an example of the differences between real-time applications and interactive programs.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
55
What tasks are typically performed when working with stored files in an application?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
56
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Returns the default line separator for a system
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Returns the default line separator for a system
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
57
Briefly describe the Path class in Java.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
58
How can you write the system's newline character to a file in Java?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
59
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Involves performing the same tasks with many records
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Involves performing the same tasks with many records
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
60
Match each term with the correct statement below.
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Clears any bytes that have been sent to a buffer for output but have not yet been output to a hardware device
a.batch processing
b.root
c.buffer
d.flushing
e.stream
f.System.getProperty("line.separator")
g.data file
h.close the files
i.BufferedWriter
Clears any bytes that have been sent to a buffer for output but have not yet been output to a hardware device
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
61
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
{
__________________________________________
__________________________________________
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 shaded line, create the statement to assign a stream to the InputStream reference. In the second shaded 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
{
__________________________________________
__________________________________________
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 shaded line, create the statement to assign a stream to the InputStream reference. In the second shaded line, declare a BufferedReader that reads a line of text from a character-input stream that buffers characters for more efficient reading.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
62
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)))
{
_____________________________
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);
_____________________________
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 shaded 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 shaded 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)))
{
_____________________________
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);
_____________________________
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 shaded 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 shaded line, create the statement to compute the record's desired position by multiplying the ID number value by the record size.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
63
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 < count; ++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 < count; ++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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
64
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
65
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
{
_______________________________________________
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 in the blank line provided 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
{
_______________________________________________
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 in the blank line provided that will verify that the file exists, and checks that the program can read and write to the file.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
66
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
67
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
{
_____________________________________
output.write(data)
output.flush();
output.close();
}
catch(Exception badNums)
{
System.out.println("My numbers: " + badNums);
}
}
}
Using the above code, complete the blank line with a statement 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
{
_____________________________________
output.write(data)
output.flush();
output.close();
}
catch(Exception badNums)
{
System.out.println("My numbers: " + badNums);
}
}
}
Using the above code, complete the blank line with a statement 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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
68
class Rectangle
{
int length, breadth;
void show(int x, int y)
{
length = x;
breadth = y;
}
int calculate()
{
return(length * breadth);
}
}
public class EnteredValues
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle();
_____________________________
_____________________________
rectangle.show(a, b);
System.out.println("You entered: " + a + " and " + b);
int area = rectangle.calculate();
System.out.println("The area of the rectangle is: " + area);
}
}
In the above program, the area of a rectangle is calculated by using two classes named Rectangle and EnteredValues. In the first class, two methods are used: show(int x, int y) and calculate(). The show() method takes two variables as input, and the calculate() method calculates the area of a rectangle.
In the second class, the main() method is declared. Inside this method, an object of a Rectangle class is created. The user will input two values and store the values in the variables. The entered values will be changed into integers using the parseInt() method. The variables are then passed in the show()method, and the area will be calculated by the calculate() method.
On the lines provided, create the parseInt() method statements to reformat the two String values.
{
int length, breadth;
void show(int x, int y)
{
length = x;
breadth = y;
}
int calculate()
{
return(length * breadth);
}
}
public class EnteredValues
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle();
_____________________________
_____________________________
rectangle.show(a, b);
System.out.println("You entered: " + a + " and " + b);
int area = rectangle.calculate();
System.out.println("The area of the rectangle is: " + area);
}
}
In the above program, the area of a rectangle is calculated by using two classes named Rectangle and EnteredValues. In the first class, two methods are used: show(int x, int y) and calculate(). The show() method takes two variables as input, and the calculate() method calculates the area of a rectangle.
In the second class, the main() method is declared. Inside this method, an object of a Rectangle class is created. The user will input two values and store the values in the variables. The entered values will be changed into integers using the parseInt() method. The variables are then passed in the show()method, and the area will be calculated by the calculate() method.
On the lines provided, create the parseInt() method statements to reformat the two String values.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
69
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
70
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);
__________________
System.out.println("Full path is " + fullPath.toString());
}
}
Using the above code, complete the shaded line with a statement 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);
__________________
System.out.println("Full path is " + fullPath.toString());
}
}
Using the above code, complete the shaded line with a statement that creates an absolute path by assigning the file to the current directory.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
71
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)
{
________________________
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. In the blank line provided, 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)
{
________________________
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. In the blank line provided, 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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
72
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));
__________________________
while(s != null)
{
System.out.println(s);
____________________________
}
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. In the blank lines provided, 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));
__________________________
while(s != null)
{
System.out.println(s);
____________________________
}
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. In the blank lines provided, 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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
73
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
{
_______________________________________
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 in the blank line provided 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
{
_______________________________________
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 in the blank line provided 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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck
74
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");
______________________________________
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 shaded line provided, 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");
______________________________________
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 shaded line provided, create a statement that assigns the length of the string to RECSIZE.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 74 في هذه المجموعة.
فتح الحزمة
k this deck