Skip to content

Commit

Permalink
Set the image hosting to github and upload 4 md files
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeaaaaaa committed Sep 15, 2024
1 parent df4ef13 commit 285613d
Show file tree
Hide file tree
Showing 7 changed files with 4,018 additions and 2 deletions.
100 changes: 98 additions & 2 deletions _posts/2023-10-01-java相关知识.md
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,105 @@ public class Test {




## Java

Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种所以一共四个):InputStreamOutputStreamReaderWriterJava中其他多种多样变化的流均是由它们派生出来的。

### 字节流与字符流区别:

字节流按照8位传输,字节流是最基本的,所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘;

1. 字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串;
2. 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以。
3. 既然字节流那么牛,为什么还要字符流:理论上任何文件都能够用字节流读取,但当读取的是文本数据时,为了能还原成文本你必须再经过一个转换的工序,相对来说字符流就省了这个麻烦,可以有方法直接读取。字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节, 操作字节和字节数组。所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好!

### 缓冲流

为了提高数据的读写效率,Java中又定义了四种缓冲流:BufferedInputStreamBufferedOutputStreamBufferedWriterBufferedWriter

![image-20240914115535628](https://raw.githubusercontent.com/mikeaaaaaa/cloudimg/main/img/image-20240914115535628.png)

#### 原理

Java 缓冲流的原理主要是通过内存中的缓冲区来提高输入输出操作的效率。在没有缓冲流的情况下,每次读取或写入数据时,都会进行一次实际的I/O操作,这样的频繁操作性能开销较大。缓冲流通过在内存中维护一个缓冲区,将数据暂时存储在缓冲区中,从而减少直接进行I/O操作的次数,提升性能。

![image-20240914120511593](https://raw.githubusercontent.com/mikeaaaaaa/cloudimg/main/img/image-20240914120511593.png)



高级流都是对基本流的封装,其底层依旧使用基本流读写数据,但是其新增了一些非常好用的方法。

#### 实例

使用字节流拷贝文件:

```java
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
/*
利用字节缓冲流拷贝文件
*/

//1. 创建缓冲流对象
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("test.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("copy.txt"));
//循环读取数据并写入文件
byte[] buffer = new byte[1024];
while ((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
//释放资源,不用释放传入的基本流
bos.close();
bis.close();
}
}
```



使用字符流实现文件拷贝:

```java
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
BufferedWriter bw = new BufferWriter(new FileWriter("test2.txt"));
String s;
while ((s=br.readLine())!=null){
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
}
}
```



### ByteArrayInputStream

这个流比较特殊,它允**许将内存中的字节数组作为数据源**,模拟一个输入流。它继承自 `InputStream` 类,专门用于从字节数组中读取数据,而不是从物理文件、网络连接等外部数据源读取;注:其外部没有必要再套一层 `BufferInputStream`了。

```java
public class ByteArrayInputStreamExample {
public static void main(String[] args) throws IOException {
byte[] data = "Hello, ByteArrayInputStream!".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(data);

int i;
while ((i = bais.read()) != -1) {
System.out.print((char) i);
}

bais.close(); // 对于 ByteArrayInputStream 来说,这个 close 不一定必须,因为它不占用外部资源
}
}
```






Expand Down
Loading

0 comments on commit 285613d

Please sign in to comment.