file stream

According to the transmission data type, byte stream reads one byte at a time and character stream reads two bytes at a time

Unicode is 2 bytes representing a character
UTF-8 is an upgraded version of Unicode, it will

1. Byte stream Input is to read into memory, Output is to write to hard disk

  • Input stream InputStream abstract class
  • Output stream OutputStream abstract class
  • The function of reading and writing files is mainly divided into three stages
    • Create input stream/output stream
    • Convective operation
    • close stream

input stream

graph TD
a[InputStream抽象类]-->b[FileInputStream]
FileInputStream fs = new FileInputStream(String PathName); //通过完整文件路径创建流对象
FileInputStream fs = new FileInputStream(File FileObj); //通过File对象创建流对象

close() //关闭流
read() //读取一个字节
read(byte[] b)  //读取b.length长度个字节,在某些输入可用之前此方法将阻塞
available() //下次将不受阻塞的读取剩余字节或b.length个字节

output stream

graph TD
a[OutputStream抽象类]-->b[FileInputStream]
//通过完整文件路径创建流对象
FileOutputStream fs = new FileOutputStream(String PathName); 

//通过完整文件路径创建流对象,是否追加写入
FileOutputStream fs = new FileOutputStream(String PathName,boolean append); 

//通过File对象创建流对象
FileOutputStream fs = new FileOutputStream(File FileObj); 

//通过File对象创建流对象,是否追加写入
FileOutputStream fs = new FileOutputStream(File FileObj); 

flush() //强制写出所有缓冲区的字节
close() //关闭流
write(int b) //将指定字节写入文件
write(byte[] b) //将b.length个字节从b中写入文件

2. Character stream

Mainly manipulate text files,

graph TD
a[Reader抽象类]-->b[InputStreamReader]
b-->d[FileStream]
read(char[] c) //读取c.length长度的字符流对象
graph LR
A[字符流]-->B[OutputStreamWriter]
B-->C[字节流]
graph RL
A[字节流]-->B[InputStreamReader]
B-->C[字符流]
OutputStreamWriter(OutputStream out)
OutputStreamWriter(OutputStream out,Charset cs) //创建指定编码的处理流

InputStreamReader(InputStream in)
InputStreamReader(InputStream in,Charset cs)  //创建指定编码的处理流
  • Since these two streams are processing streams, they must be built on top of the node stream

BufferedReader 和 BufferedWriter

graph LR
A[BufferedWriter]-->B[OutputStreamWriter]
B-->C[FileOutputStream]
graph RL
A[FileInputStream]-->B[InputStreamReader]
B-->C[BufferedReader]
BufferedReader(Reader in)
BufferedReader(Reader in,int sz) //创建一个指定大小的缓冲区,存储字符流
这是一个处理流,必须依赖节点流 readLine() 读取一行


BufferedWriter(Writer out)
BufferedWriter(Writer out,int sz) //创建一个指定大小的缓冲区,存储字符流
这是一个处理流,必须依赖节点流 newLine() 另起一行

Both DataInputStream and DataOutputStream are processing streams

Implemented the DataInput and DataOutput interfaces respectively

The two interfaces are declared separately, get bytes and convert them to basic data types and convert basic data types to bytes as specified

ObjectInputStream and ObjectOutputStream handle streams

ObjectOutputStream //Convert objects directly into byte objects, called serialization

  • The class being serialized must implement the Serializable interface
  • Set a
    ```
    private static final long serialVersionUID = a large string of numbers;
    //because if this is not set, then each serialization jvm will automatically generate one,
    //then when reading objects from the same file, because the serialVersionUID is different, Throws an InvalidClassException

readObject(); read object

> ObjectInputStream //把字节转化直接转化为对象,称为反序列化

writeObject(); //write object



### 标准流
**是位于java.lang包**
* 标准输入流  System.in
* 标准输出流  System.out
* 标准错误输出流  System.err

//Get the input on the keyboard and output the screen
InputStream worldByte = System.in; //Accept the byte data input by the keyboard through InputStream
//Convert the byte data to character data
InputStreamReader world = new InputStreamReader(worldByte);
// put Character data is stored in the character buffer pool
BufferedReader worlds = new BufferedReader(world);
try{
System.out.println(worlds.readLine()); //read a line
}catch(IOException e){ //may throw IOException
e .printStackTrace();
}

Scanner scan = new Scanner(new InputStreamReader(System.in));
Scan.next();


## 其他流
### RandomAccessFile 对磁盘文件进行随机访问
> 这个又有点类似于python的open函数

* 没有继承InputStream 和 OutputStream
* 实现了DataInput和DataOutput接口
* 同时拥有读写文件的操作
* 只支持文件类型的定位访问,不支持内存网络等数据流的搜索

public RandomAccessFile(String name, STring mode) //Specify the file and open method
public RandomAccessFile(File file, STring mode) //Specify the file and open method through the File object


### PrintWriter 处理流

* 主要用于向文本输出流打印对象的格式化表示
*  

// 演示了利用PrintWriter进行的文件复制功能
public void demo(){
    BufferedReader buffRead = null;
    BufferedWriter buffWrite = null;
    try{
        buffRead = new BufferedReader(new FileReader("s.txt"));
        buffWrite = new BufferedWriter(new FileWriter("333333.txt"));
        PrintWriter pw = new PrintWriter(buffWrite);
        String s = null;
        while ((s = buffRead.readLine()) != null){
            pw.println(s);
            pw.flush();

        }
        System.out.print("复制完成");


    }catch(Exception e){
        e.printStackTrace();
    }
}
### 内存流
* #### ByteArrayInputStream
* #### ByteArrayOutputStream
    * 速度快
    * 主要是针对内存缓冲区的操作,先在内部创建一个byte数组然后向数组内读取或写入
    * 数据缓冲区会随着数据而不断增大

### 线程流

* PipeInputStream
* PipeOutputStream
* #### 主要用于两个独立线程之间的通信


### 压缩流 处理流

* **ZipInputStream** 
* **ZipOutputStream**

* **GZIPInputStream**
* **GZIPOutputStream**

* **CheckedOutputStream** 冗余验证流 CRC方式

#### Zip 和 GZIP 只是算法的不同

FileOutputStream fos = new FileOutputStream("路径");
CheckedOutputStream check = new CheckedOutputStream(fos,new CRC32());

ZipOutputStream zos = new ZipOutputStream(check);

zos.putNextEntry(new ZipEntry("ff.txt"));
String text = "被写数据";
zos.write(text.getBytes());
```

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325677018&siteId=291194637