4.10 Introduction to File Input and Output

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Have a look at the lines of code below. Over the next 3 questions, we will write code that opens a filenamed MyName.txt, read the first line from the file MyName.txt and display it, and then close it. What should line number 20 be? The aim is to open a file named MyName.txt. Enter the entire line 20 i.e. start with File file =.. 20 File file = new ________; 21 Scanner inputFile = new Scanner(file); 22 if (inputFile.hasNext()) 23 { 24 String str =__________; 25 System.out.println(str); 26 } 27 ___________;

20. File file = new File("MyName.txt");

Have a look at the lines of code below. Over the next 3 questions, we will write code that opens a filenamed MyName.txt, read the first line from the file MyName.txt and display it, and then close it. What should line number 24 be? The aim is to read the first line from the file MyName.txt and display it. Enter the entire line 24 i.e. String str = 20 File file = new ________; 21 Scanner inputFile = new Scanner(file); 22 if (inputFile.hasNext()) 23 { 24 String str =__________; 25 System.out.println(str); 26 } 27 ___________;

24.

Have a look at the lines of code below. Over the next 3 questions, we will write code that opens a filenamed MyName.txt, read the first line from the file MyName.txt and display it, and then close it. What should line number 27 be? The aim is to close the file. 20 File file = new ________; 21 Scanner inputFile = new Scanner(file); 22 if (inputFile.hasNext()) 23 { 24 String str =__________; 25 System.out.println(str); 26 } 27 ___________;

27. inputFile.close();

IOException

A type of Run-time error or exception that can occur while performing certain Input or Output operations

The _____ Java class can be used to write data to a file.

PrintWriter

Write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file named "output.txt". (Do not concern yourself with any possible exceptions here—assume they are handled elsewhere.)

PrintWriter output = new PrintWriter("output.txt");

Given an initialized String variable outfile, write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file whose name is given by outfile. (Do not concern yourself with any possible exceptions here—assume they are handled elsewhere.)

PrintWriter output = new PrintWriter(outfile);

Given a String variable named line1, write a sequence of statements that use a Scanner to read the first line of a file named "poem" and stores it in line1. (Do not concern yourself with any possible exceptions here—assume they are handled elsewhere.)

Scanner fileInput = new Scanner(new File("poem")); line1 = fileInput.nextLine();

True or False: Any method that uses PrintWriter objects and does not respond to their exceptions must have the clause throws IOEXception.

True

True or False: Programmers usually refer to the process of saving data in a file as writing data to the file.

True

Which type of file contains data that has not been converted to text?

binary file

Write the import statement needed in a program that performs file operations.

import java.io.*;

Complete the code to write the first name "Kathryn" to the file and then close the file. import java.io.*; public class WriteFile { public static void main (String[] args) throws IOException { PrintWriter outputFile = new PrintWriter("MyName.txt"); // Enter write to file code here // Enter close the file code here } }

import java.io.*; public class WriteFile { public static void main (String[] args) throws IOException { PrintWriter outputFile = new PrintWriter("MyName.txt"); outputFile.println("Kathryn"); outputFile.close(); } }

There are two text files, whose names are given by two String variables, file1 and file2. These text files have the same number of lines. Write a sequence of statements that creates a new file whose name consists concatenating the names of the two text files (with a "-" in the middle) and whose contents of merging the lines of the two files. Thus, in the new file, the first line is the first line from the first file, the second line is the first line from the second file. The third line in the new file is the second line in the first file and the fourth line is the second line from the second file, and so on.When finished, make sure that the data written to the new file has been flushed from its buffer and that any system resources used during the course of running your code have been released.(Do not concern yourself with any possible exceptions here—assume they are handled elsewhere.)

tbd

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of integers and an int variable named total,write the code necessary to add all the integers in the input source and place their sum into total.

total = 0; while (input.hasNext()) total += input.nextInt();

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print it out on its own line. Use input.hasNextLine() and input.nextLine().

while (input.hasNextLine()) System.out.println(input.nextLine());

close method

writes any unsaved data remaining in the file buffer.

. Date is read from what is known as a(n) _____ file.

input

input file

is a file that a program reads data from. It is called an input file because the data stored in it serves as input to the program.

output file

is a file that a program writes data to. It is called an output file because the program stores output in the file.

buffer

is a small "holding section" of memory.

delimiter

is an item that separates other items.

Given a PrintWriter reference variable named output that references a PrintWriter object, write a statement that writes the string "Hello, World" to the file in to which output streams.

output.print("Hello, World");

Given an initialized String variable message, and given a PrintWriter reference variable named output that references a PrintWriter object, write a statement that writes the string referenced by message to the file in to which output streams.

output.print(message);

What Java classes are needed to read data from a file?

File and Scanner

text file

contains data that has been encoded as text, using a scheme such as Unicode.

binary file

contains data that has not been converted to text.

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines of text and an int variable named count,write the code necessary to count the lines of text in the input source and place that value into count.

count = 0; while (input.hasNext()) { String s = input.nextLine(); count++; }


संबंधित स्टडी सेट्स

Biology 1202 Mastering Biology Questions Exam One

View Set

Chapter 15 InQuizitive: The Federal Courts

View Set

Tech 4 to 5 Streamline with Quiz

View Set

managing people final exam (past test questions)

View Set

Intermediate Accounting 2 - Exam 1 Chap. 15, 16, & 17

View Set

health & illness exam 3 review questions

View Set