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 (common operation of 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.
Including but not limited to: flush,close, compare the contents of two streams, calculate the check code of the stream and return the line traverser, etc.
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
method | describe |
---|---|
cn.hutool.core.io.IoUtil.flush(java.io.Flushable) |
Flush data from cache
|
cn.hutool.core.io.IoUtil.close(java.io.Closeable) |
Closing < br > if closing fails, no exception will be thrown
|
cn.hutool.core.io.IoUtil.closeIfPosible(java.lang.Object) |
Try to close the specified object < br > judge if the object implements {@ link autoclosable}, call it
|
cn.hutool.core.io.IoUtil.contentEquals(java.io.InputStream, java.io.InputStream) |
Compare whether the contents of the two streams are the same < br > the internal stream will be converted to {@ link BufferedInputStream}
|
cn.hutool.core.io.IoUtil.contentEquals(java.io.Reader, java.io.Reader) |
Compare whether the contents of the two readers are consistent < br > the internal stream will be converted to {@ link BufferedInputStream}
|
cn.hutool.core.io.IoUtil.contentEqualsIgnoreEOL(java.io.Reader, java.io.Reader) |
Compare whether the contents of the two streams are the same. Ignore the EOL character < br > the internal stream will be converted to {@ link BufferedInputStream}
|
cn.hutool.core.io.IoUtil.checksumCRC32(java.io.InputStream) |
Calculate the CRC32 check code of the stream, and close the stream after calculation
|
cn.hutool.core.io.IoUtil.checksum(java.io.InputStream, java.util.zip.Checksum) |
Calculate the check code of the flow and close the flow after calculation
|
cn.hutool.core.io.IoUtil.checksumValue(java.io.InputStream, java.util.zip.Checksum) |
Calculate the check code of the flow and close the flow after calculation
|
cn.hutool.core.io.IoUtil.lineIter(java.io.Reader) |
Return line iterator
LineIterator it = null; try { it = IoUtil.lineIter(reader); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); } |
cn.hutool.core.io.IoUtil.lineIter(java.io.InputStream, java.nio.charset.Charset) |
Return line iterator
LineIterator it = null; try { it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); } |
Method details
Method name: CN hutool. core. io. IoUtil. flush(java.io.Flushable)
Method description
Flush data from cache
Supported version and above
4.2.2
Parameter Description:
Parameter name | describe |
---|---|
Flushable flushable |
flushable {@link Flushable}
|
Return value:
Reference case:
//Traditional writing File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ; FileOutputStream outputStream = null; OutputStreamWriter outputStreamWriter = null; try { //Create stream outputStream = new FileOutputStream(dest); //For the new OutputStreamWriter object, remember to turn off recycling outputStreamWriter = IoUtil.getUtf8Writer(outputStream); String content = "1hello Xiaoxuzhu\n2hello Xiaoxuzhu"; int c; for (int i = 0; i < content.length(); i++) { c = content.charAt(i); outputStreamWriter.write((char) c); } outputStreamWriter.flush(); } catch (IOException e) { //Throw a runtime exception (stop the program directly) throw new RuntimeException("Runtime exception",e); } finally { try { //If it is empty, it means that the creation of the stream fails and does not need to be closed if (outputStream != null) { outputStream.close(); } } catch (Exception e) { //Failed to close the resource. Stop the program throw new RuntimeException("Failed to close resource"); }finally { try { if (outputStreamWriter != null) { outputStreamWriter.close(); } } catch (Exception e) { throw new RuntimeException("Failed to close resource"); } } } //Use ioutil How to write flush File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ; FileOutputStream outputStream = null; OutputStreamWriter outputStreamWriter = null; try { //Create stream outputStream = new FileOutputStream(dest); //For the new OutputStreamWriter object, remember to turn off recycling outputStreamWriter = IoUtil.getUtf8Writer(outputStream); String content = "1hello Xiaoxuzhu\n2hello Xiaoxuzhu"; int c; for (int i = 0; i < content.length(); i++) { c = content.charAt(i); outputStreamWriter.write((char) c); } IoUtil.flush(outputStreamWriter); } catch (IOException e) { //Throw a runtime exception (stop the program directly) throw new RuntimeException("Runtime exception",e); } finally { IoUtil.close(outputStream); IoUtil.close(outputStreamWriter); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. close(java.io.Closeable)
Method description
close
If the shutdown fails, no exception will be thrown
Supported version and above
Parameter Description:
Parameter name | describe |
---|---|
Closeable closeable |
closeable closed object
|
Return value:
Reference case:
//Traditional writing File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ; FileOutputStream outputStream = null; OutputStreamWriter outputStreamWriter = null; try { //Create stream outputStream = new FileOutputStream(dest); //For the new OutputStreamWriter object, remember to turn off recycling outputStreamWriter = IoUtil.getUtf8Writer(outputStream); String content = "1hello Xiaoxuzhu\n2hello Xiaoxuzhu"; int c; for (int i = 0; i < content.length(); i++) { c = content.charAt(i); outputStreamWriter.write((char) c); } outputStreamWriter.flush(); } catch (IOException e) { //Throw a runtime exception (stop the program directly) throw new RuntimeException("Runtime exception",e); } finally { try { //If it is empty, it means that the creation of the stream fails and does not need to be closed if (outputStream != null) { outputStream.close(); } } catch (Exception e) { //Failed to close the resource. Stop the program throw new RuntimeException("Failed to close resource"); }finally { try { if (outputStreamWriter != null) { outputStreamWriter.close(); } } catch (Exception e) { throw new RuntimeException("Failed to close resource"); } } } //Use ioutil How to write close File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ; FileOutputStream outputStream = null; OutputStreamWriter outputStreamWriter = null; try { //Create stream outputStream = new FileOutputStream(dest); //For the new OutputStreamWriter object, remember to turn off recycling outputStreamWriter = IoUtil.getUtf8Writer(outputStream); String content = "1hello Xiaoxuzhu\n2hello Xiaoxuzhu"; int c; for (int i = 0; i < content.length(); i++) { c = content.charAt(i); outputStreamWriter.write((char) c); } IoUtil.flush(outputStreamWriter); } catch (IOException e) { //Throw a runtime exception (stop the program directly) throw new RuntimeException("Runtime exception",e); } finally { IoUtil.close(outputStream); IoUtil.close(outputStreamWriter); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. closeIfPosible(java.lang.Object)
Method description
Try to close the specified object < br >
If the judgment object implements {@ link autoclosable}, call it
Supported version and above
4.3.2
Parameter Description:
Parameter name | describe |
---|---|
Object obj |
obj closes the object
|
Return value:
Reference case:
public class AutoCloseableObject implements AutoCloseable{ @Override public void close() throws Exception { System.out.println("--close AutoCloseableObject--"); } public void demo(){ System.out.println("This is AutoCloseableObject In the object demo method"); //After 1.7, as long as the autocolosable interface is implemented try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt")) { System.out.println("--fileInputStream--"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //---------------------------------- //First understand the working principle of autoclosable //1,AutoCloseable jdk1.7 + is supported //2. try method can manage multiple resources and use; Number separation //3. The managed resources must implement the autoclosable interface try(AutoCloseableObject object = new AutoCloseableObject()) { System.out.println("--implement closeIfPosibleTest method--"); System.out.println("--demo--"); object.demo(); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Reference case 2:
try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt")) { System.out.println("--fileInputStream--"); IoUtil.closeIfPosible(fileInputStream); System.out.println(fileInputStream); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. contentEquals(java.io.InputStream, java.io.InputStream)
Method description
Compare whether the contents of two streams are the same < br >
The internal stream will be converted to {@ link BufferedInputStream}
Supported version and above
4.0.6
Parameter Description:
Parameter name | describe |
---|---|
InputStream input1 |
input1 first stream
|
InputStream input2 |
input2 second stream
|
Return value:
If the contents of the two streams are consistent, return true, otherwise false
Reference case 1:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ; File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ; try (FileInputStream srcFileInputStream = new FileInputStream(src); FileInputStream destFileInputStream = new FileInputStream(dest)) { Assert.assertEquals(Boolean.TRUE, IoUtil.contentEquals(srcFileInputStream,destFileInputStream)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Reference case 2:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ; File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ; try (FileInputStream srcFileInputStream = new FileInputStream(src); FileInputStream flushFileInputStream = new FileInputStream(flushFile)) { Assert.assertEquals(Boolean.FALSE, IoUtil.contentEquals(srcFileInputStream,flushFileInputStream)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. contentEquals(java.io.Reader, java.io.Reader)
Method description
Compare whether the contents of two readers are consistent < br >
The internal stream will be converted to {@ link BufferedInputStream}
Supported version and above
4.0.6
Parameter Description:
Parameter name | describe |
---|---|
Reader input1 |
input1 first reader
|
Reader input2 |
input2 second reader
|
Return value:
If the contents of the two streams are consistent, return true, otherwise false
Reference case 1:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ; File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ; try (FileReader srcFileFileReader = new FileReader(src); FileReader destFileFileReader = new FileReader(dest)) { Assert.assertEquals(Boolean.TRUE, IoUtil.contentEquals(srcFileFileReader,destFileFileReader)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Reference case 2:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ; File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ; try (FileReader srcFileFileReader = new FileReader(src); FileReader flushFileFileReader = new FileReader(flushFile)) { Assert.assertEquals(Boolean.FALSE, IoUtil.contentEquals(srcFileFileReader,flushFileFileReader)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. contentEqualsIgnoreEOL(java.io.Reader, java.io.Reader)
Method description
Compare whether the contents of the two streams are the same, and ignore the EOL character < br >
The internal stream will be converted to {@ link BufferedInputStream}
Supported version and above
4.0.6
Parameter Description:
Parameter name | describe |
---|---|
Reader input1 |
input1 first stream
|
Reader input2 |
input2 second stream
|
Return value:
If the contents of the two streams are consistent, return true, otherwise false
Reference case 1:
//Compare whether the contents of the two streams are the same. Ignore the EOL character. For example, \ R is the EOL character File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toContentEqualsIgnoreEOLTest1.txt") ; try (FileReader srcFileFileReader = new FileReader(src); FileReader destFileFileReader = new FileReader(dest)) { Assert.assertEquals(Boolean.TRUE, IoUtil.contentEqualsIgnoreEOL(srcFileFileReader,destFileFileReader)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Reference case 2:
//Compare whether the contents of the two streams are the same. Ignore the EOL character. For example, \ R is the EOL character File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest2.txt") ; try (FileReader srcFileFileReader = new FileReader(src); FileReader flushFileFileReader = new FileReader(flushFile)) { Assert.assertEquals(Boolean.TRUE, IoUtil.contentEqualsIgnoreEOL(srcFileFileReader,flushFileFileReader)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. checksumCRC32(java.io.InputStream)
Method description
Calculate the CRC32 check code of the stream, and close the stream after calculation
(what is CRC32: CRC is a value that is used to verify the correctness of data. Similar checks include MD5 check code)
Supported version and above
4.0.6
Parameter Description:
Parameter name | describe |
---|---|
InputStream in |
in file, cannot be directory
|
Return value:
CRC32 value
Reference case:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; try { FileInputStream srcFileInputStream = new FileInputStream(src); //Calculate the check code of the flow, and close the flow after calculation System.out.println( IoUtil.checksumCRC32(srcFileInputStream)); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. checksum(java.io.InputStream, java.util.zip.Checksum)
Method description
Calculate the check code of the flow and close the flow after calculation
Supported version and above
4.0.10
Parameter Description:
Parameter name | describe |
---|---|
InputStream in |
in flow
|
Checksum checksum |
checksum {@link Checksum}
|
Return value:
Checksum
Reference case:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; try { FileInputStream srcFileInputStream = new FileInputStream(src); //Calculate the check code of the flow, close the flow after calculation, and support the optional check code to realize Checksum System.out.println( IoUtil.checksum(srcFileInputStream,new CRC32()).getValue()); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. checksumValue(java.io.InputStream, java.util.zip.Checksum)
Method description
Calculate the check code of the flow and close the flow after calculation
Supported version and above
5.4.0
Parameter Description:
Parameter name | describe |
---|---|
InputStream in |
in flow
|
Checksum checksum |
checksum {@link Checksum}
|
Return value:
Checksum
Reference case:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; try { FileInputStream srcFileInputStream = new FileInputStream(src); //Calculate the check code of the flow, close the flow after calculation, and support the optional check code to realize Checksum System.out.println( IoUtil.checksumValue(srcFileInputStream,new CRC32())); }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. lineIter(java.io.Reader)
Method description
Return line iterator
LineIterator it = null; try { it = IoUtil.lineIter(reader); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
Supported version and above
5.6.1
Parameter Description:
Parameter name | describe |
---|---|
Reader reader |
reader {@link Reader}
|
Return value:
{@link LineIter}
Reference case:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; LineIter it = null; try (FileReader srcFileFileReader = new FileReader(src)) { //Return line iterator it = IoUtil.lineIter(srcFileFileReader); while (it.hasNext()) { String line = it.nextLine(); System.out.println("Row traverser row data:"+line); } }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); it.close(); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. IoUtil. lineIter(java.io.InputStream, java.nio.charset.Charset)
Method description
Return line iterator
LineIterator it = null; try { it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
Supported version and above
5.6.1
Parameter Description:
Parameter name | describe |
---|---|
InputStream in |
in {@link InputStream}
|
Charset charset |
charset coding
|
Return value:
{@link LineIter}
Reference case:
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ; LineIter it = null; try (FileInputStream srcFileInputStream = new FileInputStream(src);) { //Return line iterator it = IoUtil.lineIter(srcFileInputStream,CharsetUtil.CHARSET_UTF_8); while (it.hasNext()) { String line = it.nextLine(); System.out.println("Row traverser row data:"+line); } }catch (Exception e){ System.out.println("--exception--"); }finally { System.out.println("--finally--"); it.close(); }
Source code analysis:
Link: to be added