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( );
FileDialog- Save FileDialog fileBox new FileDialog( mainWindow,Save As", FileDialog. SAVE fileBox setvisible( true )i Save As 区 ave In Ch11 团圈回 自 Address Booki 目 TestFileOutputStream ADdress Storage BTestPrintStream 目 TestBufferedReader ETestD 百 TestDataDutput Stream 口 TestFilelnputStream Save as type: All Files(:") Cancel C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 6 FileDialog - Save FileDialog fileBox = new FileDialog( mainWindow, “Save As”, FileDialog.SAVE ); fileBox.setVisible( true );
LOW-Level File I/0-output //set up file and stream File outFile new File("samplel data")i Fileoutputstream outstream new Fileoutputstream( outFile //data to save byte[] byteArray =110, 20, 30,40 50,60,70,80} //write data to the stream outstream write( byteArray )i //output done, so close the stream outstream close()i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 7 Low-Level File I/O – Output //set up file and stream File outFile = new File("sample1.data"); FileOutputStream outStream = new FileOutputStream( outFile ); //data to save byte[] byteArray = {10, 20, 30, 40, 50, 60, 70, 80}; //write data to the stream outStream.write( byteArray ); //output done, so close the stream outStream.close();
LOW-Level File I/0-Input //set up file and stream File inFile new File("samplel data) FileInputstream inStream new FileInputstream(inFile)i //set up an array to read data in int filesize (int)inFile. length(i byte[] byteArray new byte[filesize] //read data in and display them instream read(byteArray) for (int i =0; i< filesize; i++)i outputBox. printLine (byteArray [il)i //input done, so close the stream inStream close() C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 8 Low-Level File I/O – Input //set up file and stream File inFile = new File("sample1.data"); FileInputStream inStream = new FileInputStream(inFile); //set up an array to read data in int fileSize = (int)inFile.length(); byte[] byteArray = new byte[fileSize]; //read data in and display them inStream.read(byteArray); for (int i = 0; i < fileSize; i++) { outputBox.printLine(byteArray[i]); } //input done, so close the stream inStream.close();
High-Level File I/o-output File outFile new File(sample data Fileoutputstream outFilestream new Fileoutputstream(outFile)i DataOutputstream outDataStream new DataOutputstream(outFilestream)i writeln writeFloat vriteDouble Primitive data type values are written to outDataStream outDataStream Primitive data type values are outFilestrean verted to byte 3) Converted bytes are written to the outFile fil C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 11-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 9 High-Level File I/O – Output File outFile = new File("sample2.data"); FileOutputStream outFileStream = new FileOutputStream(outFile); DataOutputStream outDataStream = new DataOutputStream(outFileStream); Primitive data type values are written to outDataStream. Primitive data type values are converted to bytes. Converted bytes are written to the file. outDataStream writeFloat writeInt writeDouble 1 outFileStream 2 outFile 3
High-Level File I/0-Input Fi nFile new File(sample data") FileInputStream inFilestream new FileInputstream(inFile)i DataInputStream inDataStream new DataInputstream(inFilestream)i readInt readFloat redoUble Primitive data type values are read from 3 inDataStrear in Datastream Bytes are converted to primitive data in Filestrear type values Bytes are read from the file outFile C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 11-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 11 - 10 High-Level File I/O – Input File inFile = new File("sample2.data"); FileInputStream inFileStream = new FileInputStream(inFile); DataInputStream inDataStream = new DataInputStream(inFileStream); Primitive data type values are read from inDataStream. Bytes are converted to primitive data type values. Bytes are read from the file. readFloat readInt readDouble 3 inDataStream 2 inFileStream outFile 1