Exam 13: File Input and Output

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

You can use Java's ____ class to create your own random access files.

Free
(Multiple Choice)
4.8/5
(30)
Correct Answer:
Verified

D

In random access files, records can be retrieved in any order.

Free
(True/False)
4.8/5
(40)
Correct Answer:
Verified

True

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.

Free
(Multiple Choice)
4.9/5
(39)
Correct Answer:
Verified

C

InputStream is a child of FileInputStream .

(True/False)
4.8/5
(35)

When you use the BufferedReader class, you must import the java.io package into your program.

(True/False)
4.8/5
(29)

Briefly describe the Path class in Java.

(Essay)
4.8/5
(30)

Briefly describe the ByteBuffer class.

(Essay)
4.8/5
(33)

Random files are also called ____________________ files.

(Short Answer)
4.9/5
(42)

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.

(Short Answer)
4.8/5
(46)

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 .

(Essay)
4.8/5
(29)

What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations.

(Essay)
4.8/5
(39)

____________________ is an abstract class that contains methods for performing output.

(Short Answer)
4.8/5
(40)

Some text files are ____ files that contain facts and figures, such as a payroll file that contains employee numbers, names, and salaries.

(Multiple Choice)
4.8/5
(30)

____ is an abstract class for reading character streams.

(Multiple Choice)
4.7/5
(36)

The top-level element in a Path 's directory structure is located at index 1.

(True/False)
4.7/5
(39)

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 .

(Short Answer)
4.8/5
(35)

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.

(Short Answer)
4.9/5
(38)

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.

(Essay)
4.8/5
(38)

After you create a FileSystem object, you can define a Path using the ____ method with it.

(Multiple Choice)
4.8/5
(33)

The value you store in memory is lost when the program ends or the computer loses power. This type of storage device is called ____________________.

(Short Answer)
4.8/5
(32)
Showing 1 - 20 of 66
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)