next next up down toc toc news schedule RIT students in OS allInOne PDF PDF mail
all, section 7.12.

7.12.  Reading From Files

Src/7/FileIO.java.minusSTART_STOP


 1      import java.io.*;
 2      public class FileIO {
 3      
 4        static void cp(String inF, String outF )      {
 5              DataInputStream in;
 6              DataOutputStream out;
 7      
 8              try {
 9                      in = new DataInputStream(
10                              new FileInputStream(inF) );
11                      out = new DataOutputStream(
12                              new FileOutputStream(outF) );
13                      try {
14                              do {
15                                      out.writeByte (in.readByte() );
16                              } while ( true );
17                      }
18                      catch ( EOFException e )        {
19                              in.close();
20                              out.close();
21                      }
22              }
23              catch ( FileNotFoundException e )       {
24                      e.printStackTrace();
25                      System.out.println("Can't find the file!");
26              }
27              catch ( IOException e ) {  // Throws: IOException !!!
28                      e.printStackTrace();
29                      System.out.println("Could not be opened for writing!");
30              }
31              catch ( Exception e )   {
32                      System.out.println("Can't find the file!");
33              }
34        }
35      
36        public static void main(String args[]) {
37              if ( args.length != 2 ) 
38                      System.out.println("Usage: java FileIO f1 f2");
39              else    {
40                      System.out.println(args[0] + "  " + args[1] );
41                      cp(args[0], args[1]);
42              }
43        }
44      }

Source Code: Src/7/FileIO.java

Result:

% java FileIO x xx
x  x
% ls -l x
-rw-------   1 hpb      fac           24 Mar 19 11:06 x
% java FileIO x xx
x  xx
% chmod 000 x
% java FileIO x xx
x  xx
java/gio.FileNotFoundException: x
        at java/gio.FileInputStream.<init>(FileInputStream.java)
        at FileIO.cp(FileIO.java:13)
        at FileIO.main(FileIO.java:45)
Can't find the file!
% chmod 644 x ; chmod 000 xx; ls -l x xx
-rw-r--r--   1 hpb      fac           24 Mar 19 11:06 x
----------   1 hpb      fac           24 Mar 19 11:08 xx
% java FileIO x xx
x  xx
java/gio.FileNotFoundException: xx
        at java/gio.FileOutputStream.<init>(FileOutputStream.java)
        at FileIO.cp(FileIO.java:15)
        at FileIO.main(FileIO.java:45)
Can't find the file!
% rm x xx
rm: xx: override protection 0 (yes/no)? y
% java FileIO x xx
x  xx
java/gio.FileNotFoundException: x
        at java/gio.FileInputStream.<init>(FileInputStream.java)
        at FileIO.cp(FileIO.java:13)
        at FileIO.main(FileIO.java:45)
Can't find the file!


back next up down toc toc news schedule RIT students in OS allInOne pdf PDF mail

Created by unroff, java2html & & hp-tools. © by csfac. All Rights Reserved (2003).
It is not allowed to print these pages on a CAST printer.
Last modified: 11/February/03 (21:12)