《JAVA OOP开发》英文版 Chapter 11 File Input and Output

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第五章 循环结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第二章 数据类型与运算符.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第四章 MCS-51单片机存储器的扩展.ppt
- 《网络安全设计》 第一章 安全设计简介.ppt
- 《网络安全设计》 第二章 创建网络安全计划.ppt
- 《网络安全设计》 第三章 确定网络安全威胁.ppt
- 《网络安全设计》 第四章 分析安全风险.ppt
- 《网络安全设计》 第五章 创建物理资源安全设计.ppt