Prev | Current Page 232 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"


* @author Paul Tymann
* @author Carl Reynolds
*/
public class FileCopy {
public static void main( String args[] ) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
int data;
// Check command line arguments
if ( args.length != 2 ) {
System.out.println(
"Usage: FileCopy sourceFile destFile");
System.exit(1);
}
try {
// Open the input file.
in = new BufferedInputStream(
new FileInputStream( args[0]) );
// Open the output file.
// If the output file exists, it will be overwritten.
out = new BufferedOutputStream(
new FileOutputStream( args[1] ) );
}
catch ( FileNotFoundException e ) {
System.err.println("FileCopy: " + e.getMessage() );
System.exit( 1 );
}
// Now copy the files one byte at a time
try {
while ( (data = in.read() ) != -1) {
out.write( data );
}
}
90 PROGRAMMING IN JAVA [CHAP. 5
catch ( IOException e ) {
System.err.println( "FileCopy: " + e.getMessage() );
System.exit( 1 );
}
finally {
// Close the files.
try {
in.close();
out.close();
}
catch( Exception e ) {}
}
}//main
} //FileCopy
When the program opens the BufferedOutputStream, the new FileOutputStream opens an
existing file for overwriting, or, if the output file does not exist, it creates a new output file for writing.


Pages:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
ochrona mienia Życie projekty domków letniskowych grzejniki łazienkowe Hotel spa