### 方法明细
#### 方法名称:cn.hutool.core.io.IoUtil.writeUtf8(java.io.OutputStream, boolean, java.lang.Object[])
#### 方法描述
将多部分内容写到流中,自动转换为UTF-8字符串
#### 支持版本及以上
3.1.1
#### 参数描述:
| 参数名 | 描述 |
| --- | --- |
| OutputStream out |
o
![](https://oscimg.oschina.net/oscnet/up-aad3207a030bdfbb77979e397a0eef7edb2.jpg)
![](https://img-blog.csdnimg.cn/direct/1f61f12c78974561bd3d0f6a7de2e4d0.png)
ut 输出流
|
| boolean isCloseOut |
isCloseOut 写入完毕是否关闭输出流
|
| java.lang.Object[] contents |
contents 写入的内容,调用toString()方法,不包括不会自动换行
|
#### 返回值:
#### 参考案例:
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/writeTest2.txt") ;
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(dest);
boolean isCloseOut = false;
String str = "1hello 小虚竹\n2hello 小虚竹";
//覆盖,不是追加
IoUtil.writeUtf8(outputStream,isCloseOut,str);
}catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
IoUtil.close(outputStream);
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607201800791.png#pic_center)
#### 源码解析:
链接:待补充
### 方法明细
#### 方法名称:cn.hutool.core.io.IoUtil.write(java.io.OutputStream, java.lang.String, boolean, java.lang.Object[])
#### 方法描述
将多部分内容写到流中,自动转换为字符串
#### 支持版本及以上
#### 参数描述:
| 参数名 | 描述 |
| --- | --- |
| OutputStream out |
out 输出流
|
| String charsetName |
charsetName 写出的内容的字符集
|
| boolean isCloseOut |
isCloseOut 写入完毕是否关闭输出流
|
| java.lang.Object[] contents |
contents 写入的内容,调用toString()方法,不包括不会自动换行
|
#### 返回值:
#### 参考案例:
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/writeTest3.txt") ;
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(dest);
boolean isCloseOut = false;
String str = "1hello 小虚竹\n2hello 小虚竹";
//覆盖,不是追加
IoUtil.write(outputStream,"UTF-8",isCloseOut,str,str,str);
}catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
IoUtil.close(outputStream);
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607201815629.png#pic_center)
#### 源码解析:
链接:待补充
### 方法明细
#### 方法名称:cn.hutool.core.io.IoUtil.write(java.io.OutputStream, java.nio.charset.Charset, boolean, java.lang.Object[])
#### 方法描述
将多部分内容写到流中,自动转换为字符串
#### 支持版本及以上
3.0.9
#### 参数描述:
| 参数名 | 描述 |
| --- | --- |
| OutputStream out |
out 输出流
|
| Charset charset |
charset 写出的内容的字符集
|
| boolean isCloseOut |
isCloseOut 写入完毕是否关闭输出流
|
| java.lang.Object[] contents |
contents 写入的内容,调用toString()方法,不包括不会自动换行
|
#### 返回值:
#### 参考案例:
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/writeTest3.txt") ;
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(dest);
boolean isCloseOut = false;
String str = "1hello 小虚竹\n2hello 小虚竹";
//覆盖,不是追加
IoUtil.write(outputStream,CharsetUtil.UTF_8,isCloseOut,str,str,str);
}catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
IoUtil.close(outputStream);
}
#### 源码解析:
链接:待补充
### 方法明细
#### 方法名称:cn.hutool.core.io.IoUtil.writeObj(java.io.OutputStream, boolean, java.io.Serializable)
#### 方法描述
将多部分内容写到流中
#### 支持版本及以上
5.3.3
#### 参数描述:
| 参数名 | 描述 |
| --- | --- |
| OutputStream out |
out 输出流
|
| boolean isCloseOut |
isCloseOut 写入完毕是否关闭输出流
|
| Serializable obj |
obj 写入的对象内容
|
#### 返回值:
#### 参考案例:
public class StudentDto implements Serializable {
private static final long serialVersionUID = -3259523582894021714L;
private String name;
private Integer age;
private String sNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
@Override
public String toString() {
return "StudentDto{" +
"name='" + name + '\'' +
", age=" + age +
", sNo='" + sNo + '\'' +
'}';
}
}
try {
boolean isCloseOut = false;
StudentDto student = new StudentDto();
student.setName("小虚竹");
student.setAge(18);
student.setsNo("nb9527");
final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
IoUtil.writeObj(byteOut,isCloseOut,student);
byte[] bytes = byteOut.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
//从流中读取对象,即对象的反序列化
StudentDto studentDto = IoUtil.readObj(byteArrayInputStream);
System.out.println(studentDto.toString());
}catch (Exception e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607201828839.png#pic_center)
#### 源码解析:
链接:待补充
### 方法明细
#### 方法名称:cn.hutool.core.io.IoUtil.writeObjects(java.io.OutputStream, boolean, java.io.Serializable[])
#### 方法描述
将多部分内容写到流中
#### 支持版本及以上
#### 参数描述:
| 参数名 | 描述 |
| --- | --- |
| OutputStream out |
out 输出流
|
| boolean isCloseOut |
isCloseOut 写入完毕是否关闭输出流
|
| java.io.Serializable[] contents |
contents 写入的内容
|
#### 返回值:
#### 参考案例:
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/writeTest6.txt") ;
OutputStream outputStream = null;
ObjectInputStream inputStream = null;
FileInputStream fis = null;
try {
boolean isCloseOut = false;
StudentDto student = new StudentDto();
student.setName("小虚竹");
student.setAge(18);
student.setsNo("nb9527");
StudentDto student1 = new StudentDto();
student1.setName("张三");
student1.setAge(18);
student1.setsNo("nb007");
outputStream = new ObjectOutputStream(new FileOutputStream(dest));
IoUtil.writeObjects(outputStream,isCloseOut,student,student1);
IoUtil.close(outputStream);
//创建序列化流对象
fis = new FileInputStream(dest);
inputStream = new ObjectInputStream(fis);
//读取
Object obj =null;
//用available判断是否达到了文件末尾
while(fis.available()>0) {
obj=inputStream.readObject();
System.out.println(obj);
}
}catch (Exception e) {