Chapter 11 File Input and output 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 11-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 1 Chapter 11 File Input and Output
Chapter 11 Objectives After, you have read and studied this chapter, you shoula be able to e Include a File Dialog object in your program to let the user specify a file. e Write bytes to a file and read them back from the file using FileOutputstream and FileInputStream e Write values of primitive data types to a file and read them back from the file using dataOutputStream and DataInputstream e Write text data to a file and read them back from the file using printWriter and BufferedReader. Write objects to a file and read them back from the file using Objectoutputstream and ObjectInputstream Write exception-handling routines using the try-catch block C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 2 Chapter 11 Objectives After you have read and studied this chapter, you should be able to Include a FileDialog object in your program to let the user specify a file. Write bytes to a file and read them back from the file using FileOutputStream and FileInputStream. Write values of primitive data types to a file and read them back from the file using DataOutputStream and DataInputStream. Write text data to a file and read them back from the file using PrintWriter and BufferedReader. Write objects to a file and read them back from the file using ObjectOutputStream and ObjectInputStream. Write exception-handling routines using the try–catch block
File objects r To operate on a file, we must first create a File object from java. io) Opens the file sample. dat File inFile new File(sample. dat")i in the current directory Opens the file one. txt in File inFile the directory new File(C: \\SamplePrograms C: ISample Programs one. txti Notice the use of the escape character \ Opens the file test. dat in File infile new File the directory (C:/SamplePrograms/test. dat")i C: \Sample Programs using the generic file separator/and providing the full pathname C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 3 File Objects To operate on a file, we must first create a File object (from java.io). File inFile = new File(“sample.dat”); File inFile = new File(“C:\\SamplePrograms”, “one.txt”); File inFile = new File (“C:/SamplePrograms/test.dat”); Opens the file sample.dat in the current directory. Opens the file one.txt in the directory C:\SamplePrograms. Notice the use of the escape character \. Opens the file test.dat in the directory C:\SamplePrograms using the generic file separator / and providing the full pathname
Some file methods To see if inFile is if( inFile exists())[. associated to a real file correctly To see if inFile is f( inFile isFile())[.j associated to a file or a direct File folder new List the name of all files File( C: /JavaProjects/ch11") in the directory C: \JavaProjects Ch11 string filename[]= folderlist()i for (int 1=0; i< filename length; i++ )i outputBox. printline( filename [i] )i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 4 Some File Methods if ( inFile.exists( ) ) { … } if ( inFile.isFile( ) ) { … } File folder = new File(“C:/JavaProjects/Ch11”); String filename[ ] = folder.list( ); for (int i=0; i < filename.length; i++ ){ outputBox.printLine( filename[i] ); } To see if inFile is associated to a real file correctly. To see if inFile is associated to a file or a directory. List the name of all files in the directory C:\JavaProjects\Ch11
FileDialog-Open r File Dialog is a standard file dialog for selecting a file FileDialog fileBox new FileDialog( mainWindow,Open", FileDialog LOAD fileBox setvisible( true )i e Chi1 倒国 口Ades B] TestFileOutputStrea 口 Address8o5ge百 TestPrint Stream 事 Person 口 TestBufferedReader BTestDataOutputS tr 口 TestFilelnputStream Files of type: All Files(") Cancel String filename fileBox. getFile()i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 5 FileDialog - Open FileDialog is a standard file dialog for selecting a file. FileDialog fileBox = new FileDialog( mainWindow, “Open”, FileDialog.LOAD ); fileBox.setVisible( true ); String filename = fileBox.getFile( );