/* * T03n01.java -- Display information about a file whose name is entered * by the user, using the File object instance methods. Further, if the * file is a directory, display the files stored within it. * * Yes, there are things that are neither files nor directories. * Interestingly (to me, anyway), is that a symbolic link counts as a * directory if it links to a directory. At the moment, I don't know * how to distinguish it from a real directory in Java, but I'm sure that * it can be done. */ import java.io.*; // For File import java.util.*; // For Scanner public class T03n01 { public static void main (String [] args) { File fileRef; String fileName; String[] fileNameList; Scanner keyboard = new Scanner (System.in); System.out.print("Enter the name of a file; feel free to include\n" + "path information as part of the name : "); fileName = keyboard.nextLine(); fileRef = new File(fileName); System.out.println("\n" + fileName + ":"); if (fileRef.exists()) { if (fileRef.isFile()) { System.out.println("\t...is a normal file."); System.out.println("\t...contains " + fileRef.length() + " bytes."); if (fileRef.canRead()) { System.out.println("\t...can be read from."); } if (fileRef.canWrite()) { System.out.println("\t...can be written to."); } } else if (fileRef.isDirectory()) { System.out.println("\t...is a directory."); System.out.println("\t...contains these files:"); fileNameList = fileRef.list(); for (int i = 0; i < fileNameList.length; i++) { System.out.println("\t\t" + fileNameList[i]); } } else { System.out.println("\t...exists, but I don't know what " + "it is!"); } } else { // the filename doesn't match with an existing file System.out.println("\t...is not an existing file or directory."); } } }