/** * Example of showing how to test if a file is readable and * if it does, how to read the file efficiently. * * Classroom example. */ import java.io.*; public class TestFile { public static void main(String[] args) { /* * Check input validity */ if (args.length != 1) { System.err.println("usage: java TestFile file-name"); System.exit(1); } File f = new File(args[0].trim()); if (f.canRead() == true) { // accessible if (f.isFile() == true) { // is a normal file try { DataInputStream d = new DataInputStream (new FileInputStream(f)); long len = f.length(); byte[] b = new byte[(int)len]; d.readFully(b); String content = new String(b); System.out.println(content); } catch (IOException ignored) {}; } else System.err.println("You can't access a directory."); } else System.err.println("You don't have access to this item"); } }