import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Demonstrate using File objects and FileInputStreams in Java.
 * 
 * @author CSCI209
 * 
 */
public class FileTest {

	// Can easily change the base directory: 
	private static final String BASE_DIR = ".";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//~~~~  create a few different files:   ~~~~
		File baseDir = new File(BASE_DIR);
		File notAFile = new File("/this/is/not/a/file");
		// create a file that represents a file in the current directory
		// in 3 different ways: 
		File f = new File("chicken.data"); // default is current directory
		File f2 = new File(baseDir + File.separator + "chicken.data");
		File f3 = new File(baseDir, "chicken.data");

		System.out.println("Reading file: " + f.getAbsolutePath());
		try {

			FileInputStream fin = new FileInputStream(f);
			while (fin.available() > 0) {
				System.out.println(fin.read());
			}

			/*
			 * Consider how we'd write the code if we don't use the available() method: When
			 * would the loop stop? The loop would keep going, waiting for more input. That
			 * could be valid code, but more often, you probably want to stop looping when
			 * you run out of stuff to read, like from a file.
			 */
			/*
			 * while( true ) { int input = fin.read(); System.out.println(input); }
			 */
			fin.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		displayInfo(baseDir);
		displayInfo(f);
		displayInfo(f2);
		displayInfo(f3);
		displayInfo(notAFile);

	}

	/**
	 * Displays info (if it's a file and if it's a directory) about the given file
	 * 
	 * @param myFile the file to display information about
	 */
	private static void displayInfo(File myFile) {
		System.out.println(myFile.getAbsolutePath() + " is a file: " + myFile.isFile());
		System.out.println(myFile.getAbsolutePath() + " is a directory: " + myFile.isDirectory());
	}

}
