5hutool actual combat: IoUtil stream operation tool class (write content to stream)

Technical work should be rewarded
 Like and watch again to form a habit

hutool actual combat (take you to master various tools inside) directory

Purpose: IO tool class (write content to stream)

Usage scenario

The IO tool class is only used to read and write auxiliary streams and is not responsible for closing streams. The reason is that the stream may be read and written many times, which is easy to cause problems after reading and writing is closed.

(write content to stream)

(write content to stream)

(write content to stream)

Project reference

The basis of this blog post: hutool-5.6.5 version source code

        <dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-core</artifactId>
			<version>5.6.5</version>
		</dependency>

Method summary

methoddescribe
cn.hutool.core.io.IoUtil.write(java.io.OutputStream, boolean, byte[])
Write byte to stream
cn.hutool.core.io.IoUtil.writeUtf8(java.io.OutputStream, boolean, java.lang.Object[])
Write multiple parts to the stream and automatically convert them to UTF-8 strings
cn.hutool.core.io.IoUtil.write(java.io.OutputStream, java.lang.String, boolean, java.lang.Object[])
Write multiple parts of content to the stream and automatically convert them to strings
cn.hutool.core.io.IoUtil.write(java.io.OutputStream, java.nio.charset.Charset, boolean, java.lang.Object[])
Write multiple parts of content to the stream and automatically convert them to strings
cn.hutool.core.io.IoUtil.writeObj(java.io.OutputStream, boolean, java.io.Serializable)
Write multipart content to the stream
cn.hutool.core.io.IoUtil.writeObjects(java.io.OutputStream, boolean, java.io.Serializable[])
Write multipart content to the stream

Method details

Method name: CN hutool. core. io. IoUtil. write(java.io.OutputStream, boolean, byte[])

Method description

Write byte [] to the stream

Supported version and above

Parameter Description:

Parameter namedescribe
OutputStream out
out output stream
boolean isCloseOut
isCloseOut write completed. Close output stream
byte[] content
Content written content

Return value:

Reference case:

		File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/writeTest1.txt") ;
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(dest);
			boolean isCloseOut = false;
			String str = "1hello Xiaoxuzhu\n2hello Xiaoxuzhu";
			byte[] sb = str.getBytes();
			//Overwrite, not append
			IoUtil.write(outputStream,isCloseOut,sb);
		}catch (IOException e) {
			//Throw a runtime exception (stop the program directly)
			throw new RuntimeException("Runtime exception",e);
		} finally {
			IoUtil.close(outputStream);
		}

Source code analysis:

Link: to be added

Method details

Method name: CN hutool. core. io. IoUtil. writeUtf8(java.io.OutputStream, boolean, java.lang.Object[])

Method description

Write multiple parts to the stream and automatically convert them to UTF-8 strings

Supported version and above

3.1.1

Parameter Description:

Parameter namedescribe
OutputStream out
out output stream
boolean isCloseOut
isCloseOut write completed. Close output stream
java.lang.Object[] contents
The contents written by contents calls the toString() method, excluding the contents that will not wrap automatically

Return value:

Reference case:

	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 Xiaoxuzhu\n2hello Xiaoxuzhu";
			//Overwrite, not append
			IoUtil.writeUtf8(outputStream,isCloseOut,str);
		}catch (IOException e) {
			//Throw a runtime exception (stop the program directly)
			throw new RuntimeException("Runtime exception",e);
		} finally {
			IoUtil.close(outputStream);
		}

Source code analysis:

Link: to be added

Method details

Method name: CN hutool. core. io. IoUtil. write(java.io.OutputStream, java.lang.String, boolean, java.lang.Object[])

Method description

Write multiple parts of content to the stream and automatically convert them to strings

Supported version and above

Parameter Description:

Parameter namedescribe
OutputStream out
out output stream
String charsetName
charsetName the character set of the content written out
boolean isCloseOut
isCloseOut write completed. Close output stream
java.lang.Object[] contents
The contents written by contents calls the toString() method, excluding the contents that will not wrap automatically

Return value:

Reference case:

		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 Xiaoxuzhu\n2hello Xiaoxuzhu";
			//Overwrite, not append
			IoUtil.write(outputStream,"UTF-8",isCloseOut,str,str,str);
		}catch (IOException e) {
			//Throw a runtime exception (stop the program directly)
			throw new RuntimeException("Runtime exception",e);
		} finally {
			IoUtil.close(outputStream);
		}

Source code analysis:

Link: to be added

Method details

Method name: CN hutool. core. io. IoUtil. write(java.io.OutputStream, java.nio.charset.Charset, boolean, java.lang.Object[])

Method description

Write multiple parts of content to the stream and automatically convert them to strings

Supported version and above

3.0.9

Parameter Description:

Parameter namedescribe
OutputStream out
out output stream
Charset charset
Charset the character set of the content written out by charset
boolean isCloseOut
isCloseOut write completed. Close output stream
java.lang.Object[] contents
The contents written by contents calls the toString() method, excluding the contents that will not wrap automatically

Return value:

Reference case:

		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 Xiaoxuzhu\n2hello Xiaoxuzhu";
			//Overwrite, not append
			IoUtil.write(outputStream,CharsetUtil.UTF_8,isCloseOut,str,str,str);
		}catch (IOException e) {
			//Throw a runtime exception (stop the program directly)
			throw new RuntimeException("Runtime exception",e);
		} finally {
			IoUtil.close(outputStream);
		}

Source code analysis:

Link: to be added

Method details

Method name: CN hutool. core. io. IoUtil. writeObj(java.io.OutputStream, boolean, java.io.Serializable)

Method description

Write multipart content to the stream

Supported version and above

5.3.3

Parameter Description:

Parameter namedescribe
OutputStream out
out output stream
boolean isCloseOut
isCloseOut write completed. Close output stream
Serializable obj
Object content written by obj

Return value:

Reference case:

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("Xiaoxuzhu");
			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);
			//Read the object from the stream, that is, the deserialization of the object
			StudentDto studentDto = IoUtil.readObj(byteArrayInputStream);
			System.out.println(studentDto.toString());
		}catch (Exception e) {
			//Throw a runtime exception (stop the program directly)
			throw new RuntimeException("Runtime exception",e);
		} finally {
		}

Source code analysis:

Link: to be added

Method details

Method name: CN hutool. core. io. IoUtil. writeObjects(java.io.OutputStream, boolean, java.io.Serializable[])

Method description

Write multipart content to the stream

Supported version and above

Parameter Description:

Parameter namedescribe
OutputStream out
out output stream
boolean isCloseOut
isCloseOut write completed. Close output stream
java.io.Serializable[] contents
contents

Return value:

Reference case:

		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("Xiaoxuzhu");
			student.setAge(18);
			student.setsNo("nb9527");
			StudentDto student1 = new StudentDto();
			student1.setName("Zhang San");
			student1.setAge(18);
			student1.setsNo("nb007");
			 outputStream = new ObjectOutputStream(new FileOutputStream(dest));
			IoUtil.writeObjects(outputStream,isCloseOut,student,student1);
			IoUtil.close(outputStream);

			//Create serialized stream object
			fis = new FileInputStream(dest);
			inputStream = new ObjectInputStream(fis);
			//read
			Object obj =null;
			//Use available to determine whether the end of the file has been reached
			while(fis.available()>0) {
				obj=inputStream.readObject();
				System.out.println(obj);
			}
		}catch (Exception e) {
			//Throw a runtime exception (stop the program directly)
			throw new RuntimeException("Runtime exception",e);
		} finally {
			IoUtil.close(outputStream);
			IoUtil.close(inputStream);
			IoUtil.close(fis);
		}

Source code analysis:

Link: to be added

Keywords: Java Back-end hutool

Added by ghe on Mon, 31 Jan 2022 17:50:10 +0200