Please send questions to st10@humboldt.edu .
CIS 480 - Advanced Java - Random notes, 4-2-01

*   quick aside: from Morelli, "Java, Java, Java", p. 276:
        *   "Within a class definition, elements that are
	declared *static* are called *class elements* ---
	class methods or class variables --- and are associated
	with the class itself, not with its instances. ....
	They are referenced by qualifying their name with the
	name of the class."

	*   "Within a class definition, any variables that
	are declared *final* are ... constants. They must be 
	given an initial value, which cannot be changed by the 
	program."
	
	*   "Constants are frequently declared *static* as well
	as *final*, since then one copy of the constant can be
	shared by all instances of the class."

*   File I/O

*   References: 
[1] Chapter 8, "Input/Output", from Java Examples in a
Nutshell, FIRST EDITION, Flanagan, O'Reilly, 1997, pp. 152-184.
[much of this material is likely covered similarly in SECOND
edition, also, pp. 44-69]

*   Java's java.io package provides file i/o capabilities;

*   a common class in this package: File
        *   Beware! It isn't the file, itself --- it represents 
	a file NAME;

	*   attempts to HIDE OS file naming details; 

	*   (yes, this is very like some of C's i/o libraries;)

	*   also includes methods for dealing with files:
	        *   deleting them
		*   creating directories
		*   listing direcrtories' contents
		*   asking about a file's size, modification time
		*   and more...

	*   note, however: File does NOT provide methods to 
	manipulate a file's *contents*;

*   Java's I/O is *sequential* --- follows the *stream* 
abstraction
        *   (and, it sounds a lot like the C stream i/o 
	library...) 

*   (you can do random access file i/o using class 
RandomAccessFile, but Flanagan notes that sequential/stream 
i/o is "much more common" (p. 152, Java Examples in a Nutshell)

*   so: how is sequential i/o handled with streams in Java,
via the java.io package?
        *   [1], p. 153: "A stream is simply an object...

		 *   ...from which data can be READ sequentially or

		 *   ...to data can be WRITTEN sequentially."

	*   (java.io includes 40 stream classes...!)

*   example Delete.java, adapted from [1]

*   aside: a tidbit more regarding "static"

	*   we saw static used with data fields in CIS 235 ---
	for a data field for which there was only one copy
	for the entire class, no matter how many instances

	*   (used it in Widget.java, for example, to have a
	data field counting the number of widgets; that code's
	now available on the 480 web page)

	*   did you notice that our main() methods include the
	word "static", too? And so can other methods;

	*   a method defined without "static" is an *instance method* ---
	        *   operates on the fields of an object --- of
		a specific INSTANCE of that class;

	*   a static METHOD --- also called a class method ---
	[1] "operate on the class itself, rather than on 
	an individual instance of a class"
	 
	*   both delete() and main(), in Delete.java, are static;   

*   some tidbits about class File, to start:

        *   note that one of File's constructors takes as argument
	the name of the file in the form of a String (demo'd in
	Delete.java)

	*   some of class File's methods:
	        *   exists()
		    arguments: none
		    returns:   boolean
		    action:    returns true if the file corresponding
			       to the filename represented by the File
			       instance exists, and false otherwise.
		    (demo'd in Delete.java)

		*   isFile()
		    arguments: none
		    returns:   boolean
		    action:    returns true if the file corresponding
			       to the filename represented by the File
			       instance is indeed a file (and not a
			       directory), false otherwise
		    (demo'd in FileCopy.java)

		*   canWrite()
		    arguments: none
		    returns:   boolean
		    action:    returns true if the file corresponding
			       to the filename represented by the File
			       instance is able to be written (is not 
			       write-protected), false otherwise
		    (demo'd in Delete.java)

	        *   canRead() - analogous to canWrite()
		    (demo'd in FileCopy.java)
	
		*   isDirectory()
		    arguments: none
		    returns:   boolean
		    action:    returns true if the file corresponding
			       to the filename represented by the File
			       instance is a directory file, false
			       otherwise
		    (demo'd in Delete.java)			       

		*   list()
		    arguments: none
		    returns:   an array of String's
		    action:    The returned array of String's contains
			       all the names of files within the
			       directory file corresponding to the
			       filename represented by the File
			       instance
		    (demo'd in Delete.java)

	        *   delete()
		    arguments: none
		    returns:   boolean
		    action:    returns true if the file corresponding
			       to the filename represented by the
			       File instance was successfully
			       removed, false otherwise
		    (demo'd in Delete.java)

*   warning: if you try to run Delete.java deom BlueJ, guess where
System.err.println messages show up? in the DOS window associated
with BlueJ!

*   NOTE though --- try it under DOS, on sorrel --- no problem!
        *   File IS hiding OS differences between Windows, UNIX...

*   (please note demo of how to THROW an exception in Delete.java,
too...)

*   UseDelete.java then uses the delete() method in Delete.java ---
        *   I believe it is because delete() is a static method
	that it is called using Delete.delete() (with the class name
	itself before the method name)

	*   notice that you can get the name of the current
	working directory!

	String currentDirectory = System.getProperty("user.dir");

*   now --- that was FILE stuff, but not file I/O stuff;
*   on to file I/O now!
       *   see FileCopy.java

*   there are 40 stream classes in java.io! So, many options;

*   in GENERAL:
        *   binary files are processed by subclasses of
		   InputStream
		   OutputStream

	*   text files are processed by subclasses of
		   Reader
		   Writer

*   FileInputStream and FileOutputStream are used for binary
input and output on files;
        *   please note: you CAN use these on text files,
	too, even though they tend to be byte-oriented;
	indeed, we will in the FileCopy.java example.

	*   but the Reader/Writer subclasses are more 
	*convenient* for text;

*   and, there are buffered-version classes for these as well:
        BufferedInputStream
	BufferedOutputStream
	BufferedReader
	BufferedWriter

	*   when using buffers, do not forget to flush()!

*   examples using File I/O:
        FileCopy.java
	TryWriteToFile.java
	TryReadFromFile.java