欢迎来到天天文库
浏览记录
ID:51617562
大小:1.23 MB
页数:42页
时间:2020-03-26
《Java语言程序设计10E LiangPPT习题案例17slide.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1Chapter17BinaryI/O2MotivationsDatastoredinatextfileisrepresentedinhuman-readableform.Datastoredinabinaryfileisrepresentedinbinaryform.Youcannotreadbinaryfiles.Theyaredesignedtobereadbyprograms.Forexample,Javasourceprogramsarestoredintextfilesandcanbe
2、readbyatexteditor,butJavaclassesarestoredinbinaryfilesandarereadbytheJVM.Theadvantageofbinaryfilesisthattheyaremoreefficienttoprocessthantextfiles.3ObjectivesTodiscoverhowI/OisprocessedinJava(§17.2).TodistinguishbetweentextI/OandbinaryI/O(§17.3).Torea
3、dandwritebytesusingFileInputStreamandFileOutputStream(§17.4.1).ToreadandwriteprimitivevaluesandstringsusingDataInputStream/DataOutputStream(§17.4.3).TostoreandrestoreobjectsusingObjectOutputStreamandObjectInputStream,andtounderstandhowobjectsareserial
4、izedandwhatkindofobjectscanbeserialized(§17.6).ToimplementtheSerializableinterfacetomakeobjectsserializable(§17.6.1).Toserializearrays(§17.6.2).ToreadandwritethesamefileusingtheRandomAccessFileclass(§17.7).4HowisI/OHandledinJava?AFileobjectencapsulate
5、sthepropertiesofafileorapath,butdoesnotcontainthemethodsforreading/writingdatafrom/toafile.InordertoperformI/O,youneedtocreateobjectsusingappropriateJavaI/Oclasses.PrintWriteroutput=newPrintWriter("temp.txt");output.println("Java101");output.close();S
6、cannerinput=newScanner(newFile("temp.txt"));System.out.println(input.nextLine());5TextFilevs.BinaryFileDatastoredinatextfilearerepresentedinhuman-readableform.Datastoredinabinaryfilearerepresentedinbinaryform.Youcannotreadbinaryfiles.Binaryfilesaredes
7、ignedtobereadbyprograms.Forexample,theJavasourceprogramsarestoredintextfilesandcanbereadbyatexteditor,buttheJavaclassesarestoredinbinaryfilesandarereadbytheJVM.Theadvantageofbinaryfilesisthattheyaremoreefficienttoprocessthantextfiles.Althoughitisnotte
8、chnicallypreciseandcorrect,youcanimaginethatatextfileconsistsofasequenceofcharactersandabinaryfileconsistsofasequenceofbits.Forexample,thedecimalinteger199isstoredasthesequenceofthreecharacters:'1','9','9'inatextfileandthesameintegerisstoredas
此文档下载收益归作者所有