/* CIS 790                JAVA PROGRAMMING             PROF. AUCIELLO
(credit to 'Java Unleashed' -- Sams Publishing)
--------------------------------------------------------

Download this program by saving it as  randread.java
rename it to  randread.java from randread.htm
remove  html and pre tags at top and bottom.  4 total.
Save to your disk. then compile it.
create  fibtable.txt like this:

0
1
2
3
4
5
6
7
8
9
           (hit enter twice after 9)

create randread.class.   run program using fibtable.txt,
then any other file (best luck with text files.)

200 points for reconstructing this program.
answer all questions.
an additional 200 points given to write a program
that will accept a line from the keyboard, and write
it to a file.
---------------------------------------------------------------------
Here is the Core Dump of file  'fib.txt'
File: C:\JDK11~1.2\BIN\FIBTABLE.TXT                    HEX   (no mask)
----------------------------------------------------------------------
 000000   30 0D 0A 31  0D 0A 32 0D  0A 33 0D 0A  34 0D 0A 35   
 000010   0D 0A 36 0D  0A 37 0D 0A  38 0D 0A 39  0D 0A 0D 0A        
----------------------------------------------------------------------

The ASCII for this file is: 0 - 9 each followed by a Return & Line Feed.

The reader should see the ascii number for '0' (30) followed by
=(0D) and =(OA), through '9', followed by OD OA OD OA
for a total of 32 bytes.


    Topics:   Read a file sequentially.
              (Do you understand that a random-access
               file can be read sequentially, by advancing the 
               file pointer [FP] to the next line (which is a
               variable-length record delimited by "0D0A") or
               can be read 'directly' or 'randomly' by
               going directly to the line or record number?)
              Reads any file, a line-at-a-time. 
              (Do you understand that one integer at-a-time
               is read, so that  which is "0D" can
               tested for?)
              (Do you understand how to enter the 'filename'
               to be read as a char which is appended to a 
               buffer-string?)
              (Do you understand that 'buf' contains the
               name of the file?)
              Properties of "Random Access File" Class
              (listed below)
              Exits loop  at end-of-line (0D0A) = Unicode for  
              Char <--> Integer Conversion
              (How to convert Char to Int and vice versa?)
              Relationship operators:  (>  >=  <  <=  ==)
              (That "equals" equals  "=="?)
              (How 'try' .... 'catch' works?)

*/

import java.io.*;       // imports class of input/output methods.
import corejava.*;      // allows use of ConsoleRead class (libraries).
class randread           // names program.
{ public static void main(String args[]) // sets program parameters
  {char c;
   //  'c' is single character with a length of 1.
   int x = 0;
   //  'x' is an integer initialized to 0.

   StringBuffer buf = new StringBuffer();
   // 'buf' is defined as a string-buffer, subscriptable.

     try
     // 'try' starts the action ... main guts of program goes here.

      { System.out.println();
        System.out.print("Enter file name with extension (xxxx.doc)");
        // prompts for filename -- don't forget the extension!
        System.out.println("");

        while ((x = System.in.read()) != '\u000D')
          // interesting code.  read an integer from the file.
          // assign it to x.  continue as long as the integer
          // is not the 'enter' key which is hex 000D.

        { c = (char)x;
          // 'x' is converted to 'c'
          buf.append(c);
          // contents of 'c' appended to string buffer (buf).

          System.out.print("buf = " + buf);
          // display contents of 'buf'.
          System.out.print("  c = " + c);
          // display contents of 'c'.
          System.out.println("  x = " + x);    
          // You know what this does.
        }   // end of while group

        System.out.println("Reads one integer at-a-time that is converted to a character.  Note how buffer ");
        System.out.println("gets longer as it is appended to.  Note decimal equivalent of ASCII character.");
        System.out.println();
        System.out.println("Out of loop! Hit end of line(0D)!");
       
                   
        // What event caused control to reach this point?
     

        int xcount = 0;
        RandomAccessFile file = new RandomAccessFile(buf.toString(),"rw");
        System.out.println("File named "+buf+" has been opened.");
        // links with RandomAccessFile library routines (class) to
        // define a RAF file, that is "read/write" ("r/w") capable,
        // and has the file buffer area converted to a string.

        System.out.println("Hash Code    : " + file.hashCode());
        // converts data into unique number.

        System.out.println("Length of file "+buf+" = "+file.length()+ " bytes");
        // Note 'file.length function'

        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Press  to read next line.");
        System.out.println(" ");

         System.out.print("Byte number to start? 0 = 1st line: ");
         int ifp = Console.readInt(" ");
         file.seek(ifp);
         System.out.println(); 
         // an int is read from console and used as the
         // byte-address of where to start.  Each byte
         // in the file has its own address. (is addressable).

          System.out.println(" ");
          long filePointer = ifp;
          // define filePointer, set it to ifp (just read)!

          while (filePointer < file.length())
          // also very interesting.  a line is read with
          // 'file.getFilePointer()' as long as end-of-file
          // is not reached.

          { System.out.print("FP= "+ filePointer+ " ");
            // contents of FP displayed = location of filepointer.

            System.out.println(file.readLine()); 
            // Prints the line (record) just pointed to and read
            // by 'readLine()'.

            x = System.in.read();  
            // waits for user to press .  Very cool!
            // note to do any function, first prefix with name of RAF 

            filePointer = file.getFilePointer();
            // now filePointer is incremented automatically to
            // the first byte of the next line.

          }     //   end of while group

          System.out.println("FP = " + file.getFilePointer()
                             + "  Length of file = "+file.length() +
                             "  End of file -- Hit '0D0A'");
          // Displays value of FP & length of file -- preaches
          // more about "0D0A" being the delimiters ending the file.                            
      }     //   end of try   group

    catch (Exception e)
        { String err = e.toString();
          System.out.println("An error has occurred:  " + err);  }
          // when an error occurs in 'catch' group, control
          // passes here.

    }         // end of public group
}        // end of class  group

/*

Methods contained within RandomAccessFile Class:

	skipBytes	getFilePointer		seek
	read		read(byte ..)		readBoolean
	readint		readshort		readChar
	readFloat	readDouble		readUTFString
	readFully	write			writeByte
	write(..etc)	length			close 


ADDITIONAL TOPICS:

	RANDOM FILE		RELATIVE FILE 		CHAR to INT
	INT to CHAR		NOT EQUAL (!=)		UNICODE
	APPEND			FILE.SEEK()		ADDRESSABLE BYTES
	CATCHING ERRORS	HEX 	CORE DUMP		EOF DELIMTERS

 // note to do any function, first prefix with name of RAF 


Convert the Psudo-Code below into Java to create this output:

buf = f  c = f  x = 102
buf = fi  c = i  x = 105
buf = fib  c = b  x = 98
buf = fibt  c = t  x = 116
buf = fibta  c = a  x = 97
buf = fibtab  c = b  x = 98
buf = fibtabl  c = l  x = 108
buf = fibtable  c = e  x = 101
buf = fibtable.  c = .  x = 46
buf = fibtable.t  c = t  x = 116
buf = fibtable.tx  c = x  x = 120
buf = fibtable.txt  c = t  x = 116

Out of loop! Hit end of line(0D)!
File named fibtable.txt has been opened.
Length of file fibtable.txt = 32 bytes

Press  to read next line.

Line number to start? 0 = 1st line:   Not an integer. Please try again!
  0
FP= 0  0
FP= 3  1
FP= 6  2
FP= 9  3
FP= 12 4      (2 extra bytes for 0D 0A)
FP= 15 5
FP= 18 6
FP= 21 7
FP= 24 8
FP= 27 9
FP= 30
FP = 32  Length of file = 32  End of file -- Hit '0D0A'

--------------------------------------------------------------
Use these Java Statement to create program.

link to io and console libraries.
define internal name of program.
start class group.
give attributes of class (public, static, etc)
start public group.
define char c and int x.
define buf as a indexable stringbuffer.
start "try" group.
print blank line.
prompt for file name to be read.
blank line.
start loop while not at end-of-file (0D).
convert x to char c.
append contents of 'c' to buf (string buffer).
trace statement:  print out contents of buf.
trace statement:  print out contents of c.
trace statement:  print out contents of x.
end of while group.
print message that "out of loop.  0D was hit!"
define xcount as int.
define a RandomAccessFile, call it 'file', define its buffer,
and make it read/writeable.
trace statement:  show that file has been opened.
trace statement:  print out length of file.
2 blank lines.
prompt user to press .
blank line.
prompt for byte number to start.
read an integer from console, store it in ifp.
set the file pointer = to ifp.
2 blank lines.
define filepointer as long floating point number, set it to ifp.
start loop while filepointer is less than file length.
trace statement:  display contents of FP.
read a line from file
wait for the user to press  from the keyboard, set to x.
get updated filepointer (system updates file pointer.)
end while group.
print out location of file pointer, length of file, etc ...
end of try group.
start catch group.
define err as string.
trace statement:  print type of error occurred.
end of public group
end of class  group
---------------------------------------------------------------
Java Statements in random order.


import java.io.*;       // imports class of input/output methods.
  }     //   end of try   group
    catch (Exception e)
        { String err = e.toString();
          System.out.println("An error has occurred:  " + err);  }
import corejava.*;      // allows use of ConsoleRead class (libraries).
class randread           // names program.
      { System.out.println();
        System.out.print("Enter file name with extension (xxxx.doc)");
        System.out.println("");
        while ((x = System.in.read()) != '\u000D')
        { c = (char)x;
  System.out.println("Hash Code    : " + file.hashCode());
        System.out.println("Length of file "+buf+" = "+file.length()+ " bytes");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Press  to read next line.");
        System.out.println(" ");
         System.out.print("Byte number to start? 0 = 1st line: ");          buf.append(c);
          System.out.print("buf = " + buf);
          System.out.print("  c = " + c);
          System.out.println("  x = " + x);    
        }   // end of while group
{ public static void main(String args[]) // sets program parameters
  {char c;    //  'c' is single character with a length of 1.
   int x = 0; //  'x' is an integer initialized to 0.
   StringBuffer buf = new StringBuffer();  // 'buf' is defined.
     try 
        System.out.println("Reads one integer at-a-time that is converted to a character.  Note how buffer ");
        System.out.println("gets longer as it is appended to.  Note decimal equivalent of ASCII character.");
        System.out.println();
        System.out.println("Out of loop! Hit end of line(0D)!");
        int xcount = 0;
          System.out.println(" ");
          long filePointer = ifp;
          while (filePointer < file.length())
  RandomAccessFile file = new RandomAccessFile(buf.toString(),"rw");
        System.out.println("File named "+buf+" has been opened.");
         int ifp = Console.readInt(" ");
         file.seek(ifp);
         System.out.println(); 
          { System.out.print("FP= "+ filePointer+ " ");
            System.out.println(file.readLine()); 
            x = System.in.read();  
            filePointer = file.getFilePointer();
          }     //   end of while group
          System.out.println("FP = " + file.getFilePointer()
                             + "  Length of file = "+file.length() +
                             "  End of file -- Hit '0D0A'");
    
    }         // end of public group
}        // end of class  group

---------------------------------------------------------------------






----------------------------------------------------------------
Methods contained within RandomAccessFile Class:
	skipBytes	getFilePointer		seek
	read		read(byte ..)		readBoolean
	readint		readshort		readChar
	readFloat	readDouble		readUTFString
	readFully	write			writeByte
	write(..etc)	length			close 

ADDITIONAL TOPICS:
	RANDOM FILE		RELATIVE FILE 		CHAR to INT
	INT to CHAR		NOT EQUAL (!=)		UNICODE
	APPEND			FILE.SEEK()		ADDRESSABLE BYTES
	CATCHING ERRORS	HEX 	CORE DUMP		EOF DELIMTERS

 // note to do any function, first prefix with name of RAF 


end of listing ...

*/