package example8_3_FileOpening; import java.io.*; public class Example { public static void main(String[] args) { File file = new File("D:\\MyDocs\\Desktop\\Example.txt"); // Solution to in-class exercise #1. boolean isFile = file.isFile(); System.out.println("Is it a file? "+isFile); // Should be true -- it's a file, not a directory. //FileReader -- reads text InputStream inputStream = null; try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { System.err.println("Warning: Could not fine file:"+file.getAbsolutePath()); } int myInt = 0; // 4 bytes // Integer.MAX_VALUE; // Integer.MIN_VALUE; char myChar = (char) myInt; // 2 bytes (utf-16 character) byte myRealByte = (byte) myInt; // 1 byte (8 bits) try { while(-1 != (myInt = inputStream.read())){ System.out.println("byte: "+myInt+" "+ Integer.toHexString(myInt)+" "+(char)myInt); } } catch (IOException e) { System.err.println("Warning: Error while attempting" + " to read byte from file!"); } // In-class exercise #2: Shown on student laptop. } /** * Example of reading a directory */ private static void outOfWay() { File dir = new File("D:\\MyDocs\\Desktop\\"); File[] files = dir.listFiles(); // for(File f: files) { // System.out.println("Filename: "+f.getName()); // } for(int i =0; i< files.length; i++) { File f = files[i]; System.out.println("Filename: "+f.getName()); } } }