/* * Created on Apr 4, 2005 * */ package simple; import java.awt.Color; import java.io.*; import java.util.*; import javax.swing.*; /** * @author hornick * */ public class SimpleFileIO { public static void main(String[] args) { // a directory (or file) within the filesystem // String name = "D:\\My Documents\\MSOE\\Courses\\Example Programs\\se1021\\FileIO\\FileIO\\test.txt"; // String name = "D:/My Documents/MSOE/Courses/Example Programs/se1021/FileIO/test.txt"; String name = "test.txt"; // without path spec, JVM assumes the file is in the project directory // attach to a File object (file or directory) File fileObject = new File( name ); if( fileObject.exists() ) { // does the directory or file exist? if( fileObject.isDirectory() ) { // it's a directory System.out.println( "Directory " + name + " exists." ); String[] dirList = fileObject.list(); // get the files in the dir System.out.println( "Listing of files in " + name ); for(String dirItem: dirList ) { // print each filename System.out.println( dirItem ); } } else { // it's a file System.out.println( "File " + name + " exists." ); long nBytes = fileObject.length(); // file length System.out.println( "length: " + nBytes ); } } else { // specified path does not exist System.out.println( name + " does not exist. Exiting..." ); System.exit(0); // scram } } }