Ch 6: File Processing

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

You are given a String variable named file. It contains the name of a one-line file. Write a sequence of statements that make an exact copy of the file and give it the same name but with the string ".cpy" attached to the filename. Make sure that the data written to the file has been flushed from its buffer and that any system resources used during the course of running these statements have been released.

File f1 = new File(file); Scanner scan = new Scanner(f1); File f2 = new File(file + ".cpy"); PrintWriter pw = new PrintWriter(f2); while(scan.hasNext()){ pw.println(scan.nextLine()); } scan.close(); pw.close();

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 i in line1. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

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

Declare a Scanner reference variable fileInput, and assign it a newly created Scanner object that is associated with a file named "data1".

Scanner fileInput = new Scanner(new File("data1"));

Declare a variable logfileName that is suitable for holding the name of a file.

String logfileName;

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()){ input.nextLine(); count++; }

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 them all out on a single line, separated by a space. There should NOT be a trailing space at the end of the line.

if (input.hasNext()) System.out.print(input.nextLine()); while(input.hasNext()){ System.out.print(" " + input.nextLine()); }

Given a String variable named line1 and given a Scanner reference variable stdin that has been assigned a reference to a Scanner object, read the next line from stdin and save it in line1. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

line1 = stdin.nextLine();

Suppose a reference variable of type File called myFile has already been declared. Create an object of type File with the initial file name input.dat and assign it to the reference variable myFile.

myFile = new File("input.dat");

Write an expression whose value is a reference to a newly created PrintWriter object associated with a file named "message.log". The b>PrintWriter object should be created so that existing content in the file is not destroyed or overwritten, but rather output will be appended to any existing content.

new PrintWriter("message.log")

One of the constructors for PrintStream class has a single OutputStream argument. Assume that dos is a variable that references an OutputStream object. Create a PrintStream object using dos and assign the resulting reference to ps, a PrintStream variable that has already been declared.

ps = new PrintStream(dos);

Define a public static method named f2s that takes a single String argument, the name of a file. The method returns a (conceivably very large!) String that is an exact copy of the contents of the file.

public static String f2s (String s) throws IOException { File f= new File(s); Scanner input = new Scanner(f); String ss = ""; while(input.hasNext()){ ss += input.nextLine(); } return ss; }

Given these two methods, which are defined in the class FileUtils, write a method f2f that receives two String arguments, each a file name. The method copies the contents of the file associated with the first argument into the file associated with the second argument. In this exercise you should only use the two available methods s2f and f2s: you should not directly involve the Java IO classes. Your method should return true if successful, false otherwise.

public static boolean f2f(String s1, String s2){ return s2f(s2,f2s(s1)); }

You have made 1 attempt with 1 attempt remaining. Define a public static method named s2f that takes two String arguments, the name of a file and some text. The method creates the file and writes the text to it. If all goes well the method returns true. If, however, either of its arguments are null or if there is any problem in creating or writing to the file, the method returns false.

public static boolean s2f(String s1, String s2){ if(s2 == s1) return false; try { File f = new File(s1); PrintWriter pw = new PrintWriter(f); pw.print(s2); pw.close(); return true; } catch (Exception e){ return false; } }

The method shown below, makeJunk generates compile-time error: "unreported exception java.io.IOException; must be caught or declared to be thrown". Without changing the behavior (any part of the method body) and without changing the number of arguments the function takes or its visibility, add the necessary code that will permit this code to compile without error.

public void makeJunk() throws IOException{ new File("junk").createNewFile(); }

Write the clause needed to identify a method as one that may throw an IOException:

throws IOException

Write the clause needed to identify a method as one that may throw an IOException or a WidgetException:

throws IOException, WidgetException

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.

while(input.hasNext()){ System.out.println(input.nextLine()); }


Ensembles d'études connexes

Art History Sem 2 Midterm study guide

View Set

Job-Order Costing (Problem Solving 3)

View Set

Quiz Questions - Business Practices

View Set

Values, Ethics, and Advocacy Nursing Concepts Fall Semester (1)

View Set

BRS - Upper Limb, BRS Gross Anatomy Upper Limbs Review Questions, Lippincott P&P, Gray's P&P, Gray's Anatomy Review - Pelvis and Perineum, Lower Limb (Lippincott), Embryology (Gray's Anatomy Review & Lippincott & BRS), Gray's UL Questions, Gray's Low...

View Set

The Canterbury Tales Character Chart

View Set

NR300 Biological Diversity EXAM 1

View Set