Lecture7-File IO

File IO

Intended Learning Outcomes

  • To scan a string using the Scanner class.
  • To discover file properties, delete and rename files using the File class.
  • To distinguish between text I/O and binary I/O.
  • To discover how I/O is processed in Java
  • To read/write files using various Java I/O classes
  • To set character encoding for text I/O
  • To understand how objects are serialized and what kind of objects can be serialized
  • To read/write the same file at random location using the RandomAccessFile class.
The File class
  1. To construct a File object:
File myFile=new File("/path/to/the/file");
  1. Accessor methods:
  • getAbsolutePath(),getName(),getPath()……etc
  1. Mutator methods
  • delete(),mkdir(),renameTo(),setReadOnly(),……etc
  1. Boolean methods
  • canRead(),canWrite(),exists()……
The scanner class
String S="welcome to Java!Java is fun!Java is cool!"
Scanner scan=new Scanner(s);
scan.useDelimiter("Java");
while(scan.hasNext())
	Syetem.out.println(scan.next());
  • To read some long numbers from a text file numbers.txt
Scanner sc=new Scanner(new File("numbers.txt"));
while(sc.hasNextLong()){
	long along= sc.nextLong();
	}
Text Data vs. Binary Data

Binary format is a more efficient representation in terms of data size and processing time.Its major drawback is difficult for human to process.

How is Data Being Read and Write?

I/O in java is built on streams.A stream means an unknown flow of data(which could be bytes, characters, objects, etc)
请添加图片描述
A file object encapsulates the properties of a file or a path, but doesn’t contain the methods for reading/writing data from/to a file.
In order to perform I/O,you need to create objects using appropriate java I/O classes.

  • FileReader, FileWriter // for text I/O, perform encoding/decoding implicitly using system default
  • FileInputStream, FileOutputStream // for binary I/O
  • DataInputStream, DataOutputStream // for binary I/O
  • BufferedInputStream, BufferedOutputStream // for binary I/O
  • ObjectInputStream, ObjectOutputStream // for binary I/O

请添加图片描述

The difference of Text and Binary I/O?
  • 字节流(Byte Streams):用于处理原始的字节数据,常用于二进制文件。
  • 字符流(Character Streams):用于处理字符数据,适合处理文本文件。
    Reader is a bridge from byte streams to character streams(Same as writer).It read bytes and decodes them into characters using a specified charset(character encoding).
  • character=byte+encoding
  • byte=character+decoding
    Binary I/O doesn’t alter any data-raw read/write.

The main difference between text I/O and binary I/O is the way data is represented and stored. Text I/O uses characters and strings to represent data, while binary I/O uses binary digits. Text I/O is human-readable and can be easily manipulated and processed, while binary I/O is not directly human-readable and requires special tools or software to interpret and process the data.

In terms of usage, text I/O is commonly used for reading and writing text files, such as configuration files, log files, or data files that contain textual information. Binary I/O is commonly used for reading and writing binary files, such as image files, audio files, or executable files.

Default character Encoding

JVM to convert bytes into a string of characters.
During JVM start-up, java gets character encoding by calling

System.getProperty("file.encoding","UTF-8");

In the absence of file.encoding attribute,Java uses "UTF-8"character encoding by default.

  • Charset provides a convinient static method Charset.defaultCharset() which returns default character encoding in Java.
  • StandardCharsets provides constant definitions for standard charsets that are available on every java platform.
Text I/O for files
  • FileReader and FileWriter are used for reading/writing streams of characters(16bits)from/to a file.
  • Read/Write using system default encoding
    请添加图片描述
Binary I/O for Files
  • FileInputStream and FileOutputStream are used for reading and writing streams of raw bytes(8bits) from/to a file.
  • Read/write without any encoding/decoding(suitable for image data)
  • common methods
    请添加图片描述
Filter streams

请添加图片描述
Filter streams are streams that filter bytes for some purpose. When they are constructed, an
InputStream or OutputStream object is supplied. The filter streams basically acts like a
wrapper and pass all requests to the contained stream object.
• Adding functionalities to underlying streams
• Have a buffer to temporarily cache the data from/to the underlying streams and convert or manipulate
the data accordingly.

Data I/O streams

While file streams are primitive streams whose sources or destinations are files,data streams are streams whose sources and destinations are other streams!
They are therefore known as wrappers because they can wrap other stream object mechanisms inside a more powerful one.
Data streams providing extra read/write methods.
用于读写 Java 原始数据类型
Constructors:

  • DataInputStream(InputStream instream)
  • DataOutputStream(OutputStream outstream)

For example:

  • DataInputStream infile = new DataInputStream(new FileInputStream(“in.dat”));
  • DataOutputStream outfile = new DataOutputStream(new FileOutputStream(“out.dat”));
Buffered I/O streams

Another pair of wrappers is BufferedInputStream and BufferedOutputStream.
Unlike data streams providing extra read/write methods, buffered streams create internal byte-buffer to speed up read/write operations. More bytes are read/write to fill up the buffer each time, and hence reduce the number of direct I/O operations to the underlying streams.
提供了内部的字节缓存->加速读写操作->更多字节被读写进缓存区->减少了直接的I/O操作
提供缓冲机制,提高 IO 操作效率

Object I/O streams(用于对象序列化和反序列化,以便保存到文件或通过网络传输)

ObjectInputStream and ObjectOutputStream is another pair of wrapper that
can be used to read/write objects from/to stream
During the write process, an object is first flatten or serialized to become a stream of bytes. The same stream of bytes are read and de-serialize to re-construct the object during the read process.

Flatten/serialized->stream of bytes->be read and de-serialized ->re construct the object

Similar to data streams, object streams provide extra methods in the form of readXX() and writeXX() where XX is a primitive data type or Object (check API for more details).

The Serializable Interface
  • Not all objects can be written to an output stream. Objects that can be
    written to an object stream is said to be serializable. A serializable object is an instance of the java.io.Serializable interface.
    So the class of a serializable object must implement java.io.Serializable.
  • The Serializable interface is a marker interface which has no method but
    merely for denoting certain properties in the class. So you don’t need to
    add additional code in your class that implements Serializable. Implementing
    this interface enables the Java serialization mechanism to automate the
    process of storing the objects and arrays.
    请添加图片描述
The transient keyword

If an object is an instance of Serializable,but it contains data fields that are non-Serializable, can the object be written to object stream?
Answer:NO
In this case, you can mark these non-serializable data fields with the keyword transient.This modifier tells the JVM to ignore these fields when writing the object to an object stream.
请添加图片描述

Random Access File

All of the streams you have used so far are known as read-only or write-only streams.
The external files of these streams are sequential files that data cannot be inserted in the middle of the file.(顺序外部文件or流中数据无法被插入在其中)It is often necessary to modify files or to insert new records into files. Java provides the RandomAccessFile class to allow a file to be read from and write to at random locations.
Many methods in RandomAccessFile are the same as those in
DataInputStream and DataOutputStream. For example, readInt(),
readLong(), writeDouble(), readLine(), writeInt(), and writeLong() can be
used in data input stream or data output stream as well as in RandomAccessFile streams.
请添加图片描述

  • 19
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值