Input and output
Input and output
-API: p67
2.1.4 Text Input and Output
-When saving data, you choice between binary and text
formats. Although binary I/O is fast and efficient, it’s
readable by humans.
-When saving text strings, consider character encoding:
+Java use UTF-16 encoding internally. Example: string
“Jose” is encoded as “00 4A 00 6F 00 73 00 E9” in hex.
+Many programs expect that text files use a different
encoding. UTF-8 is the encoding most used on I.
-OutputStreamWriter class turns an output stream of
Unicode code units into a stream of bytes, using a chosen
character encoding. InputStreamReader turn an input
stream that contains bytes into a reader that emits
Unicode code units.
+ make an input reader that reads
from console and convert them to Unicode.
+The reader assumes the default character encoding
used by the host system (Windons 1252, MacRoman).
You should always choose a specific encoding:
-In early versions of Java, the only way for processing text
input was BufferedReader class:
+Read records:
2.1.8 Character Encodings
-Input and output streams are for sequences of bytes,
but texts are sequences of characters. It’s matters how
characters are encoded into bytes
-Java uses Unicode standard for characters. Each
character (or code point) has a 21-bit integer number.
There are different character encodings-methods for
packaging those 21-bit numbers into bytes.
+The most common encoding is UTF-8, which encodes
each Unicode code point into a sequence of one to four
bytes.
+Another common encoding is UTF-16, which encodes
each Unicode code point into one or two 16-bit values.
This is the encoding used in Java strings.
-Note: JAR files are simply ZIP files with a special entry:
manifest. Use JarInputStream and JarOutputStream to
read and write manifest entry.
-API: p86
2.3 Object Input/Output Streams and
Serialization
-Using a fixed-length record format is a good choice to
store data of the same type. We don’t have data format
to store polymorphic collections.
-Java supports general mechanism: object serialization to
write any object to an output stream and read it later.
2.3.1 Saving and Loading Serializable Objects
-To save object data, 1st open ObjectOutputStream:
+use writeObject()
+Use readObject()
-Write a string:
+Append to a file:
-Delete a file:
+This method throws an exception if file doesn’t exist
+Another delete:
+These methods can be used to remove empty directory.
-API: p115
2.4.5 Getting File Information
-Static methods return boolean value to check a property
of a path:
-size(): return number of bytes in a file
-API: p117
2.4.6 Visiting Directory Entries
-Files.list(): return Stream<Path> reads the entries of a
directory. Use a try block:
+list() doesn’t enter subdirectories. Use Files.walk()
-API: p124
2.5 Memory-Mapped Files
-Most OS can take advantage of a virtual memory
implementation to map a file or a region of a file into
memory.
2.5.1 Memory-Mapped File Performance
-API: p130
2.5.2 The Buffer Data Structure
-A buffer is an array of values of the same type. Buffer
class is an abstract class with concrete subclasses
ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer,
IntBuffer, LongBuffer, ShortBuffer.
+Note: StringBuffer class isn’t related to these buffers
-A buffer has:
-API: p134
2.6 File Locking
- A file lock controls access to a file or a range of byte
within a file in multiple simultaneously executing.
-Lock a file: lock() or tryLock() of FileChannel class
+shared flag is false to lock the file for both reading and
writing. It’s true for a shared lock (allows multiple
processes to read from the file, while preventing any
process from acquiring an exclusive lock). Call isShared()
-Be sure to unlock the lock when you are done. This is
best done with a try-with resources:
-API: p136
2.7 Regular Expressions
-Regular expressions are used to specify string patterns.
You can use them to locate strings that match a
particular pattern.
-Check regex: https://regex101.com/
2.7.1 The Regular Expression Syntax
2.7.2 Matching a String
-The simplest use for regex is to test whether a string
matches it
+Construct a Pattern object from a string containing the
regex. Then get a Matcher object from the Pattern and
call matches():
+The input of the matcher is an object of class
implements CharSequence interface (String,
StringBuilder, CharBuffer).
+You can set flags to pattern:
+The flags:
-Match elements in a collection or stream, turn the
pattern into a predicate: