网站首页 > 基础教程 正文
I/O流的典型使用方式
IO流种类繁多,可以通过不同的方式组合I/O流类,但平时我们常用的也就几种组合。下盘通过示例的方式盘点几种I/O流的典型用法。
缓冲输入文件
1public class BufferedInutFile {
2 public static String readFile(String fileName) throws IOException {
3 BufferedReader bf = new BufferedReader(new FileReader(fileName));
4 String s;
5
6 // 这里读取的内容存在了StringBuilder,当然也可以做其他处理
7 StringBuilder sb = new StringBuilder();
8 while ((s = bf.readLine()) != null){
9 sb.append(s + "\n");
10 }
11 bf.close();
12 return sb.toString();
13 }
14
15 public static void main(String[] args) throws IOException {
16 System.out.println(BufferedInutFile.readFile("d:/1.txt"));
17 }
18}
格式化内存输入
要读取格式化的数据,可以使用DataInputStream。
1public class FormattedMemoryInput {
2 public static void main(String[] args) throws IOException {
3 try {
4 DataInputStream dataIns = new DataInputStream(
5 new ByteArrayInputStream(BufferedInutFile.readFile("f:/FormattedMemoryInput.java").getBytes()));
6 while (true){
7 System.out.print((char) dataIns.readByte());
8 }
9 } catch (EOFException e) {
10 System.err.println("End of stream");
11 }
12 }
13}
上面程序会在控制台输出当前类本身的所有代码,并且会抛出一个EOFException异常。抛出异常的原因是已经到留的结尾了还在读数据。这里可以使用available()做判断还有多少可以的字符。
1package com.herp.pattern.strategy;
2
3import java.io.ByteArrayInputStream;
4import java.io.DataInputStream;
5import java.io.IOException;
6
7public class FormattedMemoryInput {
8 public static void main(String[] args) throws IOException {
9 DataInputStream dataIns = new DataInputStream(
10 new ByteArrayInputStream(BufferedInutFile.readFile("FormattedMemoryInput.java").getBytes()));
11 while (true){
12 System.out.println((char) dataIns.readByte());
13 }
14 }
15}
基本的文件输出
FileWriter对象可以向文件写入数据。首先创建一个FileWriter和指定的文件关联,然后使用BufferedWriter将其包装提供缓冲功能,为了提供格式化机制,它又被装饰成为PrintWriter。
1public class BasicFileOutput {
2 static String file = "BasicFileOutput.out";
3
4 public static void main(String[] args) throws IOException {
5 BufferedReader in = new BufferedReader(new StringReader(BufferedInutFile.readFile("f:/BasicFileOutput.java")));
6 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
7
8 int lineCount = 1;
9 String s;
10 while ((s = in.readLine()) != null){
11 out.println(lineCount ++ + ": " + s);
12 }
13 out.close();
14 in.close();
15 }
16}
下面是我们写出的BasicFileOutput.out文件,可以看到我们通过代码字节加上了行号
11: package com.herp.pattern.strategy;
22:
33: import java.io.*;
44:
55: public class BasicFileOutput {
66: static String file = "BasicFileOutput.out";
77:
88: public static void main(String[] args) throws IOException {
99: BufferedReader in = new BufferedReader(new StringReader(BufferedInutFile.readFile("f:/BasicFileOutput")));
1010: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
1111:
1212: int lineCount = 1;
1313: String s;
1414: while ((s = in.readLine()) != null){
1515: out.println(lineCount ++ + ": " + s);
1616: }
1717: out.close();
1818: in.close();
1919: }
2020: }
数据的存储和恢复
为了输出可供另一个“流”恢复的数据,我们需要使用DataOutputStream写入数据,然后使用DataInputStream恢复数据。当然这些流可以是任何形式(这里的形式其实就是我们前面说过的流的两端的类型),比如文件。
1public class StoringAndRecoveringData {
2 public static void main(String[] args) throws IOException {
3 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));
4 out.writeDouble(3.1415926);
5 out.writeUTF("我是二营长");
6 out.writeInt(125);
7 out.writeUTF("点赞加关注");
8 out.close();
9
10 DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data.txt")));
11 System.out.println(in.readDouble());
12 System.out.println(in.readUTF());
13 System.out.println(in.readInt());
14 System.out.println(in.readUTF());
15 in.close();
16 }
17}
输出结果:
13.1415926
2我是二营长
3125
4点赞加关注
需要注意的是我们使用writeUTF()和readUTF()来写入和读取字符串。
猜你喜欢
- 2024-11-17 第三篇 hadoop的核心概念&存取策略
- 2024-11-17 Java 17 的 I/O 基础 OutputStream 篇
- 2024-11-17 数据湖(十七):Flink与Iceberg整合DataStream API操作
- 2024-11-17 javaUDP协议DatagramPacket、DatagramSocket
- 2024-11-17 JavaSE---02(javase下载安装教程)
- 2024-11-17 每秒8.8亿次请求!Lindorm让数据存得起,看得见
- 2024-11-17 Java IO: 文件读写与数据流操作(java文件读取和写入实例)
- 2024-11-17 面试官:请说下适配器模式、代理模式和装饰者模式的不同
- 2024-11-17 HDFS和NFS的架构及原理(hdfs fs -ls)
- 2024-11-17 这篇文章过后,别说你不懂NIO(这篇文章告诉你答案)
- 06-18单例模式谁都会,破坏单例模式听说过吗?
- 06-18Objective-c单例模式的正确写法「藏」
- 06-18单例模式介绍(单例模式都有哪些)
- 06-18前端设计-单例模式在实战中的应用技巧
- 06-18PHP之单例模式(php单例模式连接数据库)
- 06-18设计模式:单例模式及C及C++实现示例
- 06-18python的单例模式(单例 python)
- 06-18你认为最简单的单例模式,东西还挺多
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)