JHTP小结_第十五章_文件、流和对象序列化(Files, Streams, and Object Serialization)


 Summary

Section 15.1 Introduction

Computers use files for long-term retention of large amounts ofpersistent data (p. 645), even after the programs that created the dataterminate.

  Computers storefiles on secondary storage devices (p. 645) such as hard disks.

Section 15.2 Files andStreams

Java views each file as a sequential stream of bytes (p. 645).

Every operating system provides a mechanism to determine theend of a file, such as an end-of file marker (p. 645) or a count of the totalbytes in the file.

Byte-based streams (p. 646) represent data in binary format.

Character-based streams (p. 646) represent data as sequences ofcharacters.

Files created using byte-based streams are binary files (p.646). Files created using character based streams are text files (p. 646). Textfiles can be read by text editors, whereas binary files are read by a programthat converts the data to a human-readable format.

Java also can associate streams with different devices. Threestream objects are associated with devices when a Java program begins executingSystem.in,System.outandSystem.err.

Section 15.3 Using NIO Classes and Interfaces to Get Fileand Directory Information

A Path(p. 647) represents the location of a file or directory. Pathobjects do not open files or provide any file-processing capabilities.

Class Paths(p. 647) is used to get aPathobject representing a file or directory location.

Class Files(p. 647) providesstaticmethods for common file and directory manipulations, includingmethods for copying files; creating and deleting files and directories; gettinginformation about files and directories; reading the contents of files; gettingobjects that allow you to manipulate the contents of files and directories; andmore.

A DirectoryStream(p. 647) enables a program to iterate through the contents of adirectory.

The staticmethodget(p. 647) of classPathsconverts aStringrepresenting a files or directorys location into a Pathobject.

Character-based input and output can be performed with classes ScannerandFormatter.

Class Formatter(p. 647) enables formatted data to be output to the screen or toa file in a manner similar to System.out.printf.

  An absolute path (p.647) contains all the directories, starting with the root directory (p. 647),that lead to a specific file or directory. Every file or directory on a diskdrive has the same root directory in its path.

A relative path (p. 647) starts from the directory in which theapplication began executing.Files static methodexists(p. 648) receives aPathand determines whether it exists (either as a file or as adirectory) on disk.

 PathmethodgetFileName(p. 648) gets theStringname of a file or directory without any location information.

Files staticmethodisDirectory(p. 648) receives aPathand returns abooleanindicating whether thatPathrepresents a directory on disk.

PathmethodisAbsolute(p. 648) returns abooleanindicating whether aPathrepresents an absolute path to a file or directory.

 Files staticmethodgetLastModifiedTime(p. 648) receives aPathand returns aFileTime(packagejava.nio.file.attribute) indicating when the file was last modified.

Files staticmethodsize(p. 648) receives aPathand returns alongrepresenting the number of bytes in the file or directory. Fordirectories, the value returned is platform specific.

PathmethodtoString(p. 648) returns aStringrepresentation of thePath.

PathmethodtoAbsolutePath(p. 648) converts thePathon which its called to an absolute path. Files static methodnewDirectoryStream(p. 648) returns aDirectoryStream<Path>containingPathobjects for a directorys contents.

A separator character (p. 650) is used to separate directoriesand files in the path.

Section 15.4Sequential-Access Text Files

Java imposes no structure on a file. You must structure filesto meet your applications needs.

To retrieve data sequentially from a file, programs normallystart from the beginning of the file and read all the data consecutively untilthe desired information is found.

Data in many sequential files cannot be modified without therisk of destroying other data in the file. Records in a sequential-access fileare usually updated by rewriting the entire file.Section 15.5 Object Serialization

Java provides a mechanism called object serialization (p. 662)that enables entire objects to be written to or read from a stream.

A serialized object (p. 662) is represented as a sequence ofbytes that includes the objects data as well asinformation about the objects type and the types of data it stores.

  After a serializedobject has been written into a file, it can be read from the file anddeserialized (p. 662) to recreate the object in memory.

Classes ObjectInputStream(p. 662) andObjectOutputStream(p. 662) enable entire objects to be read from or written to astream (possibly a file).

Only classes that implement interface Serializable(p. 663) can be serialized and deserialized.The ObjectOutputinterface (p. 662) contains methodwriteObject(p. 663), which takes anObjectas an argument and writes its information to anOutputStream. A class that implements this interface, such as ObjectOutputStream, would ensure that the ObjectisSerializable.

The ObjectInputinterface (p. 662) contains methodreadObject(p. 663), which reads and returns a reference to anObjectfrom anInputStream. After an object has been read, its reference can be cast tothe objects actual type.

Section 15.6 Opening Fileswith JFileChooser

Class JFileChooser(p. 670) is used to display a dialog that enables users of aprogram to easily select files or directories from a GUI.

Section 15.7 (Optional)Additional java.ioClasses

InputStreamandOutputStreamareabstractclasses for performing byte-based I/O.

Pipes (p. 673) are synchronized communication channels betweenthreads. One thread sends data via aPipedOutputStream(p. 673). The target thread reads information from the pipe viaa PipedInputStream(p. 673).

A filter stream (p. 674) provides additional functionality,such as aggregating data bytes into meaningful primitive-type units.FilterInputStream(p. 674) andFilterOutputStreamare typically extended, so some of their filtering capabilitiesare provided by their concrete subclasses.

A PrintStream(p. 674) performs text output.System.outandSystem.errarePrintStreams.

Interface DataInputdescribes methods for reading primitive types from an inputstream. Classes DataInputStream(p. 674) andRandomAccessFileeach implement this interface.

Interface DataOutputdescribes methods for writing primitive types to an outputstream. Classes DataOutputStream(p. 674) andRandomAccessFileeach implement this interface.

  Buffering is anI/O-performance-enhancement technique. Buffering reduces the number of I/Ooperations by combining smaller outputs together in memory. The number ofphysical I/O operations is much smaller than the number of I/O requests issuedby the program.

With a BufferedOutputStream(p. 674) each output operation is directed to a buffer (p. 674)large enough to hold the data of many output operations. Transfer to the outputdevice is performed in one large physical output operation (p. 674) when thebuffer fills. A partially filled buffer can be forced out to the device at anytime by invoking the stream objects flushmethod (p. 674).

With a BufferedInputStream(p. 675), manylogicalchunks of data from a file are read as one large physical input operation (p.675) into a memory buffer. As a program requests data, itstaken from the buffer. When the buffer is empty, the next actual physical inputoperation is performed.

A ByteArrayInputStreamreads from abytearray in memory. AByteArrayOutputStreamoutputs to abytearray in memory.

A SequenceInputStreamconcatenates severalInputStreams. When the program reaches the end of an input stream, thatstream closes, and the next stream in the sequence opens.

The Reader(p. 675) andWriter(p. 675)abstractclasses are Unicode character-based streams. Most byte-basedstreams have corresponding character-based concrete ReaderorWriterclasses.

ClassesBufferedReader(p. 675) andBufferedWriter(p. 675) buffer character-based streams.

Classes CharArrayReader(p. 676) andCharArrayWriter(p. 676) manipulatechararrays.

A LineNumberReader(p. 676) is a buffered character stream that tracks the numberof lines read. Classes FileReader(p. 676) andFileWriter(p. 676) perform character-based file I/O.

  Class PipedReader(p. 676) and classPipedWriter(p. 676) implement piped-character streams for transferring databetween threads.

Class StringReader(p. 676) andStringWriter(p. 676) read characters from and write characters toStrings, respectively. A PrintWriter(p. 654) writes characters to a stream.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值