Java之IO流

本文遵循BY-SA版权协议,转载请附上原文出处链接。


本文作者: 黑伴白

本文链接: http://heibanbai.com.cn/posts/7e6242c7/

Java之IO流

Java中的IO流基础概念

在Java中,IO流(Input/Output Stream)用于处理输入和输出操作。IO流可以分为两大类:

  1. 字节流(Byte Streams):以字节为单位进行数据的读写,主要用于处理二进制数据。

    • InputStreamOutputStream 是所有字节流的基类。
  2. 字符流(Character Streams):以字符为单位进行数据的读写,主要用于处理文本数据。

    • ReaderWriter 是所有字符流的基类。

如何定义和使用IO流

  1. 字节流

    • InputStreamOutputStream 是抽象类,常用的实现类有 FileInputStreamFileOutputStream
  2. 字符流

    • ReaderWriter 是抽象类,常用的实现类有 FileReaderFileWriter

文件读取和写入的代码示例

1. 使用字节流读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.FileInputStream;
import java.io.IOException;

public class ByteStreamReadExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input.txt")) {
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

2. 使用字节流写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreamWriteExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
String text = "Hello, World!";
byte[] bytes = text.getBytes();
fos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

3. 使用字符流读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.FileReader;
import java.io.IOException;

public class CharacterStreamReadExample {
public static void main(String[] args) {
try (FileReader fr = new FileReader("input.txt")) {
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

4. 使用字符流写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStreamWriteExample {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("output.txt")) {
String text = "Hello, World!";
fw.write(text);
} catch (IOException e) {
e.printStackTrace();
}
}
}

使用缓冲流提高效率

为了提高IO操作的效率,Java提供了缓冲流(Buffered Streams),它们通过在内存中缓冲数据来减少实际的IO操作次数。

1. 使用缓冲字节流读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedByteStreamReadExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input.txt");
BufferedInputStream bis = new BufferedInputStream(fis)) {
int data;
while ((data = bis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

2. 使用缓冲字节流写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedByteStreamWriteExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("output.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
String text = "Hello, World!";
byte[] bytes = text.getBytes();
bos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

3. 使用缓冲字符流读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedCharacterStreamReadExample {
public static void main(String[] args) {
try (FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

4. 使用缓冲字符流写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedCharacterStreamWriteExample {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("output.txt");
BufferedWriter bw = new BufferedWriter(fw)) {
String text = "Hello, World!";
bw.write(text);
} catch (IOException e) {
e.printStackTrace();
}
}
}

总结

  • 字节流:适用于处理二进制数据,如图片、音频等。
  • 字符流:适用于处理文本数据。
  • 缓冲流:通过缓冲数据来提高IO操作的效率。

在实际开发中,根据具体需求选择合适的流类型,并结合缓冲流来提高性能。


蚂蚁🐜再小也是肉🥩!


Java之IO流
http://heibanbai.com.cn/posts/7e6242c7/
作者
黑伴白
发布于
2024年5月9日
许可协议

“您的支持,我的动力!觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付