home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP10 / FILEIO2 / FILEIO.JAVA < prev    next >
Encoding:
Java Source  |  1996-06-16  |  2.1 KB  |  65 lines

  1. import java.io.*;
  2.  
  3. public class FileIO
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         try
  8.         {
  9.             // use a normal input stream for bulk input
  10.             FileInputStream in = new FileInputStream(args[0]);
  11.             BufferedInputStream bin = 
  12.                                    new BufferedInputStream(in);
  13.  
  14.             // create a buffered print stream for output
  15.             FileOutputStream out = new FileOutputStream(args[1]);
  16.             BufferedOutputStream bout =
  17.                                    new BufferedOutputStream(out);
  18.             PrintStream pout = new PrintStream(bout);
  19.  
  20.             // read up 8 bytes at a time
  21.             byte bArray[] = new byte[8];
  22.             int nBytesRead;
  23.             while(bin.available() > 0)
  24.             {
  25.                // first output them as hex numbers
  26.                nBytesRead = bin.read(bArray);
  27.                for (int i = 0; i < nBytesRead; i++)
  28.                {
  29.                    int nByte = (int)bArray[i];
  30.                    String s = Integer.toString(nByte, 16);
  31.                    if (s.length() == 1)
  32.                    {
  33.                        pout.print(" ");
  34.                    }
  35.                    pout.print(s + ", ");
  36.                }
  37.  
  38.                // then, if they are printable output the
  39.                // character (if not, output a ".")
  40.                pout.print("-");
  41.                for (int i = 0; i < nBytesRead; i++)
  42.                {
  43.                    char c = (char)bArray[i];
  44.                    if (Character.isDigit(c)     ||
  45.                        Character.isLowerCase(c) || 
  46.                        Character.isUpperCase(c))
  47.                    {
  48.                        // retain c -- do nothing
  49.                    }
  50.                    else
  51.                    {
  52.                        c = '.'; // replace with a "."
  53.                    }
  54.                    pout.print(c);
  55.                 }
  56.                 pout.println(" ");
  57.             }
  58.         }
  59.         catch(IOException ioe)
  60.         {
  61.             System.out.println(ioe.toString());
  62.         }
  63.     }
  64. }
  65.