IO流只能操作一次,如果需要多次读取流获取其中的信息,就需要将流进行复制,再对每个流进行单独操作(流只能使用一次!)
/**
* 复制操作流
*/
public void uploadFile(MultipartFile file,HttpSession session){
InputStream is = file.getInputStream();
ByteArrayOutputStream same = cloneInputStream(is);
//stream1 stream2 两个流可进行独自流的操作
InputStream stream1 = new ByteArrayInputStream(same.toByteArray());
InputStream stream2 = new ByteArrayInputStream(same.toByteArray());
}
/**
* InputStream 改为ByteArrayOutputStream
*/
private static ByteArrayOutputStream cloneInputStream(InputStream input) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
return baos;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
字符缓冲流的用法(注意流的刷新及关闭顺序)
package com.iflytek.atp.web.component;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class IOTest {
public static void main(String[] args) throws IOException {
/**
* 新写入一个文件
*/
long beginTime = System.currentTimeMillis();
Writer writer02 = new BufferedWriter( new FileWriter("D:\\test.txt") );
for(int i =0; i < 10000000; i++){
writer02.write(String.valueOf(i));
}
writer02.close();
System.out.println("带缓冲的输出流耗时:"+ (System.currentTimeMillis() - beginTime));
/**
* 复制文件(每Kb大小字符复制内容)
*/
long beginTime1 = System.currentTimeMillis();
File file = new File("D:\\test.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
File file1 = new File("D:\\test1.txt");
FileWriter fr1 = new FileWriter(file1);
BufferedWriter br1 = new BufferedWriter(fr1);
int len;
char [] buffer = new char[1024];
while ((len = br.read(buffer)) > -1) {
br1.write(buffer, 0, len);
}
fr1.flush();
br1.flush();
if(br1 != null){
br1.close();
}
if(fr1 != null){
fr1.close();
}
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
System.out.println("带缓冲批量的输出流耗时:"+ (System.currentTimeMillis() - beginTime1));
/**
* 复制文件(单字符复制内容)
*/
long beginTime2 = System.currentTimeMillis();
File ffile = new File("D:\\test.txt");
FileReader ffr = new FileReader(ffile);
BufferedReader fbr = new BufferedReader(ffr);
File ffile1 = new File("D:\\ftest1.txt");
FileWriter ffr1 = new FileWriter(ffile1);
BufferedWriter fbr1 = new BufferedWriter(ffr1);
int ch = 0;
while ((ch = fbr.read()) > -1) {
fbr1.write(ch);
}
ffr1.flush();
fbr1.flush();
if(null != fbr1){
fbr1.close();
}
if(null != ffr1){
ffr1.close();
}
if(null != fbr){
fbr.close();
}
if(null != ffr){
ffr.close();
}
System.out.println("带缓冲单字节的输出流耗时:"+ (System.currentTimeMillis() - beginTime2));
}
}
字节缓冲流的用法(注意流的刷新及关闭顺序)
package com.iflytek.atp.web.component;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class IOTest {
public static void main(String[] args) throws IOException {
/**
* 新写入一个文件
*/
long beginTime = System.currentTimeMillis();
Writer writer02 = new BufferedWriter( new FileWriter("D:\\test.txt") );
for(int i =0; i < 10000000; i++){
writer02.write(String.valueOf(i));
}
writer02.close();
System.out.println("带缓冲的输出流耗时:"+ (System.currentTimeMillis() - beginTime));
/**
* 复制文件(每Kb字节大小复制内容)
*/
long beginTime1 = System.currentTimeMillis();
File file = new File("D:\\test.txt");
FileInputStream fr = new FileInputStream(file);
BufferedInputStream br = new BufferedInputStream(fr);
File file1 = new File("D:\\test1.txt");
FileOutputStream fr1 = new FileOutputStream(file1);
BufferedOutputStream br1 = new BufferedOutputStream(fr1);
int len;
byte [] buffer = new byte[1024];
while ((len = br.read(buffer)) > -1) {
br1.write(buffer, 0, len);
}
fr1.flush();
br1.flush();
if(br1 != null){
br1.close();
}
if(fr1 != null){
fr1.close();
}
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
System.out.println("带缓冲批量的输出流耗时:"+ (System.currentTimeMillis() - beginTime1));
/**
* 复制文件(单字节复制内容)
*/
long beginTime2 = System.currentTimeMillis();
File ffile = new File("D:\\test.txt");
FileInputStream ffr = new FileInputStream(ffile);
BufferedInputStream fbr = new BufferedInputStream(ffr);
File ffile1 = new File("D:\\ftest1.txt");
FileOutputStream ffr1 = new FileOutputStream(ffile1);
BufferedOutputStream fbr1 = new BufferedOutputStream(ffr1);
int ch = 0;
while ((ch = fbr.read()) > -1) {
fbr1.write(ch);
}
ffr1.flush();
fbr1.flush();
if(null != fbr1){
fbr1.close();
}
if(null != ffr1){
ffr1.close();
}
if(null != fbr){
fbr.close();
}
if(null != ffr){
ffr.close();
}
System.out.println("带缓冲单字节的输出流耗时:"+ (System.currentTimeMillis() - beginTime2));
}
}