hutool 실전:IoUtil 흐름 조작 도구 클래스(내용 을 흐름 에 기록)

용도:IO 도구 클래스(스 트림 에 내용 쓰기)
필드 사용
IO 도구 류 는 보조 흐름 의 읽 기와 쓰기 일 뿐 흐름 을 닫 는 것 은 책임 지지 않 습 니 다.흐름 이 여러 번 읽 히 고 쓸 수 있 기 때문에 읽 기와 쓰기 가 닫 히 면 문제 가 생기 기 쉽다.
항목 참조
이 박문 의 근거:hutool-5.6.5 버 전 소스 코드

<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-core</artifactId>
			<version>5.6.5</version>
		</dependency>
방법 요약
방법.
묘사 하 다.
cn.hutool.core.io.IoUtil.write(java.io.OutputStream, boolean, byte[])
스 트림 에 byte[]쓰기
cn.hutool.core.io.IoUtil.writeUtf8(java.io.OutputStream, boolean, java.lang.Object[])
스 트림 에 여러 부분 을 쓰 고 UTF-8 문자열 로 자동 변환
cn.hutool.core.io.IoUtil.write(java.io.OutputStream, java.lang.String, boolean, java.lang.Object[])
여러 부분 을 스 트림 에 쓰 고 자동 으로 문자열 로 변환 합 니 다.
cn.hutool.core.io.IoUtil.write(java.io.OutputStream, java.nio.charset.Charset, boolean, java.lang.Object[])
여러 부분 을 스 트림 에 쓰 고 자동 으로 문자열 로 변환 합 니 다.
cn.hutool.core.io.IoUtil.writeObj(java.io.OutputStream, boolean, java.io.Serializable)
여러 부분 을 스 트림 에 쓰기
cn.hutool.core.io.IoUtil.writeObjects(java.io.OutputStream, boolean, java.io.Serializable[])
여러 부분 을 스 트림 에 쓰기
방법 내 역
방법 이름:cn.hutool.core.io.IoUtil.write(java.io.Output Stream,boolean,byte[])
방법 설명
스 트림 에 byte[]쓰기
버 전 이상 지원
매개 변수 설명:
매개 변수 이름
묘사 하 다.
OutputStream out
출력 흐름
boolean isCloseOut
isCloseOut 기록 완료 출력 흐름 닫 을 지 여부
byte[] content
내용
반환 값:
참고 사례:

		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    
2hello "; byte[] sb = str.getBytes(); // , IoUtil.write(outputStream,isCloseOut,sb); }catch (IOException e) { // ( ) throw new RuntimeException(" ",e); } finally { IoUtil.close(outputStream); }
在这里插入图片描述
방법 내 역
방법 이름:cn.hutool.core.io.IoUtil.writeUtf 8(java.io.OutputStream,boolean,java.lang.Object[])
방법 설명
스 트림 에 여러 부분 을 쓰 고 UTF-8 문자열 로 자동 변환
버 전 이상 지원
3.1.1
매개 변수 설명:
매개 변수 이름
묘사 하 다.
OutputStream out
출력 흐름
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    
2hello "; // , IoUtil.write(outputStream,"UTF-8",isCloseOut,str,str,str); }catch (IOException e) { // ( ) throw new RuntimeException(" ",e); } finally { IoUtil.close(outputStream); }
在这里插入图片描述
원본 분석:
방법 내 역
방법 이름:cn.hutool.core.io.IoUtil.write(java.io.OutputStream,java.lang.String,boolean,java.lang.Object[])
방법 설명
여러 부분 을 스 트림 에 쓰 고 자동 으로 문자열 로 변환 합 니 다.
버 전 이상 지원
매개 변수 설명:
매개 변수 이름
묘사 하 다.
OutputStream out
출력 흐름
String charsetName
charset Name 이 작성 한 내용 의 문자 집합
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    
2hello "; // , IoUtil.writeUtf8(outputStream,isCloseOut,str); }catch (IOException e) { // ( ) throw new RuntimeException(" ",e); } finally { IoUtil.close(outputStream); }
在这里插入图片描述
방법 내 역
방법 이름:cn.hutool.core.io.IoUtil.write(java.io.OutputStream,java.nio.charset.Charset,boolean,java.lang.Object[])
방법 설명
여러 부분 을 스 트림 에 쓰 고 자동 으로 문자열 로 변환 합 니 다.
지원 버 전 및 이상:3.0.9
매개 변수 설명:
매개 변수 이름
묘사 하 다.
OutputStream 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    
2hello "; // , 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
출력 흐름
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 {
		}
在这里插入图片描述
방법 내 역
방법 이름:cn.hutool.core.io.IoUtil.writeObjects(java.io.OutputStream,boolean,java.io.Serializable[])
방법 설명
여러 부분 을 스 트림 에 쓰기
매개 변수 설명:
매개 변수 이름
묘사 하 다.
OutputStream out
출력 흐름
boolean isCloseOut
isCloseOut 기록 완료 출력 흐름 닫 을 지 여부
java.io.Serializable[] 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) {
			//         (       )
			throw new RuntimeException("     ",e);
		} finally {
			IoUtil.close(outputStream);
			IoUtil.close(inputStream);
			IoUtil.close(fis);
		}
在这里插入图片描述
총결산
이 글 은 여기까지 입 니 다.당신 에 게 더욱 도움 을 줄 수 있 기 를 바 랍 니 다.또한 당신 이 우리 의 더 많은 멋 진 내용 을 도 울 수 있 기 를 바 랍 니 다!

좋은 웹페이지 즐겨찾기