//Files.java //demo file I/Os //File of bytes //File of primitve data types //File of text //File of objects import java.io.*; import javax.swing.*; public class Files { public static void main( String[] args ) { File myFile; File myFolder; int status; JFileChooser chooser = new JFileChooser(); status = chooser.showOpenDialog( null );//arg is frame to display in if ( status == JFileChooser.APPROVE_OPTION ) { //means Open clicked //.CANCEL_OPTION is the other Cancel button myFile = chooser.getSelectedFile(); myFolder = chooser.getCurrentDirectory(); System.out.println( "Selected file: " + myFile.getName() ); System.out.println( "Folder name: " + myFolder.getName() ); System.out.println( "File pathname: " + myFile.getAbsolutePath() ); } File startDir = new File( "../UncleDir" ); chooser.setCurrentDirectory( startDir ); chooser.showSaveDialog( null ); //Save and Cancel buttons myFile = new File( "test.data" ); if ( ! myFile.exists() ) System.out.println( "File not found: " + myFile.getName() ); //else use the file... if ( ! myFile.isFile() ) //also: .isDirectory() System.out.println( "Not a file: " + myFile.getName() ); //perhaps a directory... //File of bytes. Low-level file I/O File outFile = new File( "sampleByte.data" ); FileOutputStream outStream; //example data: byte[] B = {40,50,60,70,80,90,100, 65,66,67}; //open the file: try { outStream = new FileOutputStream( outFile ); outStream.write( B ); //the whole array outStream.write( B[7] ); //a byte outStream.close(); } catch (IOException e) { System.out.println( "threw an exception...deal with it..." ); } //turn around and read in the data File inFile = new File( "sampleByte.data" ); FileInputStream inStream; //open the file: try { //inStream = new FileInputStream( inFile ); inStream = new FileInputStream( "sampleByte.data" ); //bypass File object int filesize = (int) inFile.length(); //size of file B = new byte[filesize]; //byte array of same size as file inStream.read( B ); //the whole file into the array inStream.close(); for (int i=0; i