原创

I/O流之缓冲流的文件复制代码及节点流处理数据加密

缓冲流的文件复制代码

@Test
public void bufferedStream() {
long start = System.currentTimeMillis();
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
File file = new File("D:\\视频\\1.mp4");
File file2 = new File("D:\\视频\\2.mp4");

FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(file2);

bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedOutputStream != null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

long end = System.currentTimeMillis();
System.out.println(end - start);
}



节点流的处理数据加密代码

@Test
public void testFileReaderFileWriter() throws IOException {
File inputFile = new File("4.jpg");
File outputFile = new File("5.jpg");

FileInputStream fileInputStream = new FileInputStream(inputFile);
FileOutputStream fileOutputStream = new FileOutputStream(outputFile,true);

int data = 0;
while((data = fileInputStream.read()) != -1){
fileOutputStream.write(data ^ 5);
}

fileInputStream.close();
fileOutputStream.close();
}


正文到此结束
本文目录