1、概述java
StreamUtils是spring中用於處理流的類,是java.io包中inputStream和outputStream,不是java8中Steam。使用時僅依賴spring-corespring
2、使用數組
基本的輸入流讀取成字符串spa
public static String getStringFromInputStream(InputStream input) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(input, writer, "UTF-8"); return writer.toString(); }
2.一、拷貝inputStream內容至outputStream:StreamUtils.copy(in, out);code
兩個參數,第一個爲輸入,第二個爲拷貝至的blog
@Test public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; String outputFileName = "src/test/resources/output.txt"; File outputFile = new File(outputFileName); InputStream in = new FileInputStream(inputFileName); OutputStream out = new FileOutputStream(outputFile); StreamUtils.copy(in, out); assertTrue(outputFile.exists()); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(inputFileContent, outputFileContent); }
2.二、拷貝inputStream部份內容至outputStream,使用copyRange()方法拷貝必定範圍的內容:字符串
copyRange方法有四個參數,inputStream,outputStream,開始拷貝位置,結束拷貝位置。若是咱們指定的長度超過inputStream的長度呢?copyRange方法僅拷貝至流的結尾。get
@Test public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; String outputFileName = "src/test/resources/output.txt"; File outputFile = new File(outputFileName); InputStream in = new FileInputStream(inputFileName); OutputStream out = new FileOutputStream(outputFileName); StreamUtils.copyRange(in, out, 1, 10); assertTrue(outputFile.exists()); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(inputFileContent.substring(1, 11), outputFileContent); }
2.三、拷貝字符串至outputStreaminput
copy方法帶三個參數:被拷貝的字符串,寫文件時指定的字符集,指定目的地(outputStream)string
@Test public void whenCopyStringToOutputStream_thenCorrect() throws IOException { String string = "Should be copied to OutputStream."; String outputFileName = "src/test/resources/output2.txt"; File outputFile = new File(outputFileName); OutputStream out = new FileOutputStream(outputFileName); StreamUtils.copy(string, StandardCharsets.UTF_8, out); assertTrue(outputFile.exists()); String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(outputFileContent, string); }
2.四、將inputStream內容拷貝爲字符串
@Test public void whenCopyInputStreamToString_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; InputStream is = new FileInputStream(inputFileName); String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); System.out.println(content); assertEquals(inputFileContent, content); }
2.五、拷貝inputStream內容至字節數組
@Test public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; InputStream is = new FileInputStream(inputFileName); byte[] out = StreamUtils.copyToByteArray(is); String content = new String(out); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); assertEquals(inputFileContent, content); }
2.六、刪除流中全部剩餘數據
StreamUtils.drain(in);
2.七、得到一個有效空輸入流
public InputStream getInputStream() { return StreamUtils.emptyInput(); }
2.八、兩個重載方法nonClosing(),inputStream和outputStream流能夠做爲參數,用於返回無需關閉的inputStream和outputStream流
public InputStream getNonClosingInputStream() throws IOException { InputStream in = new FileInputStream("src/test/resources/input.txt"); return StreamUtils.nonClosing(in); }