JAVA基础_IO_04
IO
1. file
1.1. 创建
-
- createNewFile
-
- mkdir/mkdirs
1.2. 属性
-
- length
-
- getName
-
- getAbsolutePath
-
- listFiles
-
- isFile/isDirectory
1.3. 操作
-
- renameTo
-
- FileFilter
-
- delete
-
- walk
2. IOStream
2.1. 概述
2.2. 分类
-
2.2.1. 流的方向
- 输入流
- 输出流
-
2.2.2. 数据类型
-
- 字节流
-
字节输出流OutputStream
-
FileOutputStream
- strFilePath, append = false/true
- write(byte/bytes)
-
-
字节输入流InputStream
-
FileInputStream
-
strFilePath
-
read(byte/bytes)
- readNum = > 0 / -1
-
-
-
- 字节流问题
-
一个中文字符3个字节
-
字符集
- UTF-8
- Unicode
-
IDEA字符集设置
-
字节流装饰(设计模式)成字符流
-
- 字符流
-
字符输出流Writer
-
FileWriter
- strFilePath, append = false/true
- write
- Writer.append链式写入
-
-
字符输入流Reader
-
FileReader
-
strFilePath
-
read(char/chars)
- readNum = >0 / -1
-
-
-
2.3. 使用
-
2.3.1. flush
- FileOutputStream/FileWriter
-
2.3.2. 字节流装饰为字符流
-
- InputStreamReader(FileInputStream, “UTF-8”)
-
- OutputStreamWriter(FileOutputStream, “UTF-8”)
-
-
2.3.3. 打印流
-
- System.out
-
- PrintStream(strFilePath)
- print/println(str)
- write(byte/int/str)
- 不需要flush
-
- PrintWriter(strFilePath)
- print/println(str)
- write(byte/int/str)
- 需要flush
-
-
2.3.4. 缓存读取
-
- BufferedReader(FileReader)
-
- readLine
-
-
2.3.5. BugReport
-
- PrintWriter(strFilePath)
-
- e.printStackTrace(PrintWriter)
-
- flush
-
-
2.3.6. Properties
-
- put
-
- FileWriter(strFilePath)
-
- store(FileWriter, strComment)
-
- close
-
-
2.3.7. try-with-resources
-
- normalTryReader
-
- JDK1_7
-
- JDK1_9
-
- CloseableDemo
-
3. 序列化/反序列化
3.1. Serializable
-
- 概念
-
- ObjectOutputStream(FileOutputStream(strFilePath))
- write/writeObject
- close
-
- ObjectInputStream(FileInputStream(strFilePath))
- read/readObject
- Object cast to
3.2. 部分序列化实现方法
-
- transient修饰
-
- static修饰
-
- Serializable
- private void writeObject(java.io.ObjectOutputStream out)
- private void readObject(java.io.ObjectInputStream in)
-
- Externalizable
- public void writeExternal(ObjectOutput out) throws IOException {
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {