Contents
- ./FileTest.java
- ./FinallyTest.java
- ./SystemIOExample.java
./FileTest.java 1/3
[top][prev][next]
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 {
/**
* @param args
*/
public static void main(String[] args) {
// String basedir =
// "/Users/sprenkle/Documents/WLU/CS209/";
String basedir = ".";
// create a file that represents the current directory
// File f = new File(basedir + File.separator + "chicken.data");
File f = new File("chicken.data");
System.out.println("File is " + 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();
}
File notAFile = new File("/this/is/not/a/file");
displayInfo(f);
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());
}
}
./FinallyTest.java 2/3
[top][prev][next]
import java.io.IOException;
/**
* Example using finally block.
* Enables playing with try/catch/finally executions--
* just uncomment/comment out the thrown exceptions
*
* @author CSCI209
*/
public class FinallyTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println("Statement 1");
// throw new ArrayIndexOutOfBoundsException();
System.out.println("Statement 2");
//throw new IllegalArgumentException();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("In Catch");
} finally {
System.out.println("In Finally");
}
}
}
./SystemIOExample.java 3/3
[top][prev][next]
import java.io.IOException;
/**
* Demonstrating some use of the System.in and System.out objects
*
* @author CSCI209
*
*/
public class SystemIOExample {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Trying out System.out and System.in classes");
System.out.print("Enter an integer: ");
int number = 0;
try {
number = System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("User did not enter a number?");
}
System.out.println("You entered " + number);
}
}
Generated by GNU Enscript 1.6.6.