io
io
Input/output
Input/Output
java.io Bag
File
Randomaccessfile
Inputstream/outputstream the abstract parent class of a byte stream
Fileinputstream/fileoutputstream insert files, read and write directly to file data
Bufferedinputstream/bufferedoutputstream memory buffer to improve single byte read-write efficiency
Datainputstream/dataoutputstream Fixed byte format writeint () writedouble () writeUTF ()
PrintStream any data into a string output print () println ()
Bytearrayinputstream/bytearrayoutputstream with an in-memory byte[] array Tobytearray ()
Objectinputstream/objectoutputstream serialization, deserialization writeobject () ReadObject ()
Abstract parent class for Reader/writer character streams
Inputstreamreader/outputstreamwriter Coded Conversion Stream
Filereader/filewriter is "file byte stream" and "encoded conversion stream"
Bufferedreader/bufferedwriter character Buffering, ReadLine ()
PrintWriter and PrintStream the same File wrapper disk path string
Can encapsulate
*) file path
*) directory path
*) A path that does not exist
Creating objects
1.File f = new File ("D:\abc\a.txt");
2.File f = new File ("D:\abc\", "a.txt");
Method
*) file or directory properties
CanRead () CanWrite () CanExecute () is readable writable
executable
Ishidden () whether the path represented by the file exists () is hidden
GetAbsolutePath () full path
getName () file name
GetParent () Parent directory
lastmodified ( ) The last modification time, the millisecond value
length () file byte amount, invalid directory
isdirectory () whether the folder
Isfile () whether the file
Gettotalspace () Total space size
getfreespace () free space
*) file or directory operation
CreateNewFile () Create a file delete
() Delete a file or "Empty directory"
mkdir () Create a single-tier directory Mkdirs () create a multi-tier directory
Renameto () renaming, moving
*) Creating a Delete rename method returns a Boolean indicating whether the operation
was successful file.createtempfile (prefix, suffix) create temporary files in the system temp directory
*) file List
List () returns string[], contains child files, subdirectory name
listfiles () return file[], file object list containing child files, subdirectories
(filenamefilter)
Listfiles (FilenameFilter)
listfiles (filefilter)
lists only files, directories, and parameters that match the filter criteria
: External filters
RandomaccessfileTools for reading and writing files
File as an array of bytes,
Use subscript to read and write byte values at specified locations
Creating objects
Randomaccessfile RAF =
New Randomaccessfile (file, "R");
R-Read Only
Randomaccessfile RAF =
New Randomaccessfile (File, "RW");
RW-Read and write
Method
Write (int b)
int four bytes, output only the end of a byte value,
[1] [2] [3] [4]–> [4]
Write (byte[] buf)
All byte values in the output array
Write (byte[] buf, int from, int length)
The length byte value in the output array from from
Read ()
Reads a byte, complements 3 0 bytes, and turns int
[4]–> [1][2][3][4]
Read end, read again, return-1
Read (byte[] buf)
Reads a batch of byte values based on the array length, and puts the array
Returns the number of bytes in this batch
Read end, read again, return-1
Seek (int position)
Locate the subscript to the specified location
Getfilepointer ()
Get the current subscript position
Writeint (int i) Full output 4 bytes of int
Writedouble (double D) Full output 8 bytes of double
...
writeUTF (String s)
Output two bytes to represent the byte length of a string
Then output the byte value of the string
READINT () reads 4 byte values into int
Readdouble () reads 8 byte values into double
...
readUTF ()
Reads 2 byte values first to determine the byte length of the string,
And then read these byte values and turn them into strings.
*) Read the end of the read, will appear eofexception
Eof-end of FILE
Flow StreamData read and write operations, abstracted into data flowing through the pipeline
The stream is one-way.
*) input stream
*) Output stream
Flows past the data, cannot repeat the flow,
Data can only be read and written from beginning to end Inputstream/outputstream
Byte Stream Abstract Parent class
Method OutputStream
Write (int b)
Write (byte[] buf)
Write (byte[] buf, int from, int length)
Flush () Brush out cached data InputStream
Read ()
Read (byte[] buf)
Available () Gets the remaining amount of readable bytes Fileinputstream/fileoutputstream
Directly to the file, read and write the data in the file
Creating objects
FileOutputStream in =
New FileOutputStream (file);
Regardless of whether the file exists,
Creates a new empty file
FileOutputStream in =
New FileOutputStream (file, true);
Not present, new;
exist, append. advanced flow, operational flow and other streams, provide specific data processing functions to the advanced flow operation, will perform the same operation to the connected stream Bufferedinputstream/bufferedoutputstream
Provides memory buffers to improve single byte read and write efficiency
Creating objects
1.BufferedOutputStream out =
New Bufferedoutputstream (phase-connected flow);
Internal Cache Array Capacity 8192
2.BufferedOutputStream out =
New Bufferedoutputstream (phase-connected flow, 16*1024);
Internal Cache Array Capacity 16k
Datainputstream/dataoutputstream
Read and write data in fixed byte format
Creating objects
DataOutputStream out =
New DataOutputStream (phase-connected flow);
Method DataOutputStream
Writeint (int i)
Writedouble (double D)
...
writeUTF (String s) DataInputStream
ReadInt ()
Readdouble ()
...
readUTF () PrintStream
Any type of data is converted to a string output
writeint
the "
9 ' 7" ("The") ("the" "")
Creating objects
PrintStream out = new PrintStream (phase-connected stream);
PrintStream out = new PrintStream (file);
PrintStream out = new PrintStream (file, character encoding);
Method
Print ()
println ()
Character encoding Encoding character Set Charset
Asc-ii
0-127
Iso-8859-1
Latin-1
160-255
Western European character
GB2312 Double Byte
7+ in the text Fu Yi, Rong
GBK
20902 Chinese
BIG5
Jpxxx
Krxxx
Alxxx
Unicode
* Unified code, Universal Code
*) 700,000 +
*) Common characters table, double-byte
*) uncommon character tables, three bytes or more
*) Java char type with UNICODE encoding
*) UTF-8 Uni CDE Transmission Format
*) English, single-byte
*) Some characters, Double-byte
*) Chinese, three bytes
Java character-coded conversion operations
*) unicode–> Other codes
String s = "abc Chinese";
Convert to default encoding
byte[] A = S.getbytes ();
Convert to UTF-8 code
byte[] A = s.getbytes ("UTF-8");
*) Other encodings –> Unicode
Converts a
string s = new string (byte[] array from the default encoding);
Converts a
string s = new string (byte[] array, "UTF-8") from the specified encoding;
Reader/writer
Abstract parent class for character streams
Method Writer
Write (int c)
The two bytes at the end of int are a char type character,
Two byte values at the end of the output
Write (char[] buf)
Write (char[] buf, int from, int length)
Write (String s) Reader
Read ()
Read char type characters,
Two more 0 bytes into int
Read (char[] buf) inputstreamreader/outputstreamwriter
Character encoding conversion stream
OutputStreamWriter
Converts Unicode characters to other encoded byte value output
InputStreamReader
Converts the other encoded characters that are read into Unicode encoded characters
Creating objects
OutputStreamWriter out =
New OutputStreamWriter (connected byte stream);
OutputStreamWriter out =
New OutputStreamWriter (phase-connected byte stream, character encoding); Filereader/filewriter It is "file byte stream" and "encoded conversion stream"
Cannot specify character encoding, can only use default encoding
Creating objects
FileWriter out = new FileWriter (file);
Equivalent to:
OutputStreamWriter out =
New OutputStreamWriter (
New FileOutputStream (file)); Bufferedreader/bufferedwriter
Character Buffer stream
Creating objects
BufferedWriter out =
New BufferedWriter (the character stream of the phase);
BufferedReader method
ReadLine ()
Reads a line of strings without trailing line breaks
Read-end re-read, return null printwriter and printsream the same printwriter can be a stream of characters, you can also access the word throttling
PrintStream byte stream
Creating objects
1.PrintWriter out = new PrintWriter (the character stream of the phase);
2.PrintWriter out = new PrintWriter (byte stream of phase);
3.PrintWriter out = new PrintWriter (file);
3.PrintWriter out = new PrintWriter (file, encoding); Objectinputstream/objectoutputstream serialization, deserialization ObjectOutputStream
To turn an object into a sequence of byte sequences output
ObjectInputStream
Reading serialized data, recovering objects
Members that are not serialized
*) static belongs to the class and will not be serialized with the object output
*) Transient temporary,
exists in memory only while the program is running
Will not be persisted by serialization
Serializing the header byte value
*) When you create a new ObjectOutputStream object,
Automatically outputs 4 byte header values
*) When you create a new ObjectInputStream object,
Automatically reads 4 header byte values,
To confirm that the following data is the object serialization data
Serialization version number
Static final Long Serialversionuid
* Control the old version of the data,
Not allowed to revert to a new version of the type
Creating objects
ObjectOutputStream out =
New ObjectOutputStream (phase-connected flow);
Method ObjectOutputStream
WriteObject (Object obj)
Serialized output ObjectInputStream
ReadObject ()
Deserialization Read Bytearrayinputstream/bytearrayoutputstream
With an in-memory byte[] array,
Directly read and write data in an array
Creating objects
Bytearrayinputstream in =
New Bytearrayinputstream (byte[] array);
Bytearrayoutputstream out =
New Bytearrayoutputstream ();
There is an array inside, the output data,
placed in its internal array,
after the output is finished, you can take out the internal array
Method Bytearrayoutputstream
Tobytearray ()
To remove an internal array