[Lehr] personal understanding of Java's IO
Other related content
File type
java.io.File 类
Not related to a specific content file is used to represent files
relates only to its basic properties path name of the file type
The method of constructing objects:
File d = new File("地址字符串");
(Java中路径分割最安全的方法是 File.separator 来代替\\在Windows)
Common method
isFile () to determine whether the file
isDirectory () judgment is not a folder is to see is not a directory
getName () to get the name of the file
getParent () to get the parent directory
getPath () Get file full path
length () Gets the size of the file
lastModified () Gets the last modified event
listFiles () returns all subfolders return value is an array class file file []
the delete () delete function
exist (); returns a Boolean to determine the presence of class
d.mkdirs (); create multi-level directory
d. createNewFile (); create the file
如何在文件不存在的情况下创建目录?
exist()
d.mkdirs()
d.createNewFile()##
这三个一起配合使用即可
Coding related
Byte and Character
Computer character can not be artificially divided understand I do not know 0 a This is
the byte computer only know the binary digits 0 and 1
字符利于人来阅读,但是不利于计算机阅读
Encoding type
ASCII codeA byte Byte eight small bits to store common English and characters represent up to 256 characters, it is clearly not enough
Spreading code (add byte)
EG:
ISO8859 ----- Western European
GBK --------- (the most commonly used more than 20,000 Chinese characters)
GB18030 ---- is GB series represents the most complete coding seventy thousand Chinese characters and characters
Traditional Chinese big5 ------------
即使这样了但还是不能共通,所以,有了Unicode
Unicode
All over the world, including characters include
goal is to continue to expand to include global language
encoding scheme
UTF-8: compatible with ASCII, the use of variable-length character, 1-4 characters stored in the character of small letters (such as a, b, c) on a ; Chinese characters with a 2.
UTF-16: 2-4 variable length
UTF-32: with four bytes. Anyway, 32bits certainly big enough, but not economic. So UTF-8 is the most commonly used
ANSI code
Windows on non-Unicode encoding default
when the system is simplified Chinese GBK, Traditional Chinese system is Big5.
The default is to use Notepad ANSI send each other so you save files in different countries in the region are likely garbled.
The input and output streams
Abstract base class stream
Byte stream
InputStream
OutputStream
Character stream
Reader
Writer
巧记:结尾的时候,字节stream,字符er
the difference
Mode of operation is basically the same but different things returned. Next, an input stream by way of example:
int read();
Remove the single byte and returns an int. Since it is only the bytes 8bit range 0-255
So when you can not read Chinese characters will flow in bytes. Read the tail -1 is returned
when you output will be like this:
int c = f.read();
System.out.println((char)c);
int read(byte[] b);
Read more bytes are read method returns a value of how many
, but if you read 100 each, and finally only 40, it will repeatedly read 60, read from the reciprocal of 100
byte[] buffer = new byte[1024]
int len = System.in.read(buffer);
read读的所有IO操作都带着exception的异常处理或者抛出!
Note here that is byte at a time to read, so if the input stream will come in the carriage length will also read one more than the actual
int read(byte[] int off, int len);
Select Location
skip(long n);
How many skips
int available();
Tell you how much flow there is also something to read
mark();
Where is read to make a mark
reset();
Back to the last time that mark where the mark is valid only played once
close();
Be sure to turn off! ! ! ! Otherwise, two lines of tears! ! !
Reader
Basically the same but the difference in taking a bunch of drops of water droplets and take a sip of water and a bunch of small saliva meaning.
(Or, meal times, before one meter one meter or meter pile a bunch of rice, now directly get a bowl, a bowl of a bowl on the more convenient)
int read();
But the return value of a character stream can be a very big
Java int 2 bytes in a range on the great 0-60000 in more than a
其他操作基本一样
Output stream
OutputWriter / Writer
op.write(byte b)
This byte writes to go inside
write(byte[] b)
write(byte[] b, int start , int len)
the same
但是对于Writer类。由于是字符,所以可以String形式。
像write(String s)这样来。
But the abstract base class itself can not create objects so the need for packaging design
Entity class
Node Class
Direct file read and write
FileOutputStream out = new FileOutputStream("文件地址或者File对象");
out.write(buf) //buf 是个byte[] 意思就是一次写入这么多个字节
out.close(); //关了
A document file ------------- B
the middle of this connecting channel is ------
Wrapper class
Conversion class
Byte character conversion type, such as encoding "UTF-8"
Transformation class InputStreamReader
when reading a file, byte character into Java can understand
The OutputStreamWriter
the Java class characters converted into text input bytes
E.g:
fis = new FileInputStream("c:/temp/abc.txt");
isr = new InputStreamReader(fis,"UTF-8");
A (Java) ======= ---- B (the target file)
- refer to the ordinary encoding
== means into a UTF-8
so that from B to A then it would be appreciated by Java UTF-8 of
Decoration
Filtering package based on the existing flow on
a more powerful functions to read and write
E.g:
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(file)));
DataInputStream DataOutputStream for encapsulating the data stream and
may output a direct Int 4 bytes of a double byte to be automatically converted into eight
so you out with this thing directly out.writeInt (i) this method is substantially directly put inside type rather than a byte
Another example:
fis = new FileInputStream("c:/temp/abc.txt");
isr = new InputStreamReader(fis,"UTF-8");
br = new BufferedReader(isr);
BufferedInputStream BufferOutputStream cache byte stream
is say he wanted to open in memory read and write such a buffer zone can be faster
BufferedReader, BufferedWriter cache character stream
later with Buffer can have in.readLine such usage
but read here () methods are read default string delimiter is a space