Java Reference
In-Depth Information
public class Test {
public static void main(String[] args)
throws java.io.IOException {
java.io.PrintWriter output =
new java.io.PrintWriter( "t.txt" );
output.printf( "%s" , "1234" );
output.printf( "%s" , "5678" );
output.close();
}
}
19.19
After the program is finished, how many bytes are in the file t.dat ? Show the contents
of each byte.
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
new FileOutputStream( "t.dat" ));
output.writeInt( 1234 );
output.writeInt( 5678 );
output.close();
}
}
19.20 For each of the following statements on a DataOutputStream output , how many
bytes are sent to the output?
output.writeChar( 'A' );
output.writeChars( "BC" );
output.writeUTF( "DEF" );
19.21 What are the advantages of using buffered streams? Are the following statements
correct?
BufferedInputStream input1 =
new BufferedInputStream( new FileInputStream( "t.dat" ));
DataInputStream input2 = new DataInputStream(
new BufferedInputStream( new FileInputStream( "t.dat" )));
ObjectInputStream input3 = new ObjectInputStream(
new BufferedInputStream( new FileInputStream( "t.dat" )));
19.5 Case Study: Copying Files
This section develops a useful utility for copying files.
Key
Point
In this section, you will learn how to write a program that lets users copy files. The
user needs to provide a source file and a target file as command-line arguments using
the command:
java Copy source target
VideoNote
Copy file
The program copies the source file to the target file and displays the number of bytes in the
file. The program should alert the user if the source file does not exist or if the target file
already exists. A sample run of the program is shown in Figure 19.14.
 
 
Search WWH ::




Custom Search