- 抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
InputStream FileInputStream BufferedFileInputStream
OutputStream FileOutputStream BufferedFileOutputStream
/*
测试FileInputStream和FileOutputStream的使用
结论:
1,对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
2,对于非文本文件(.jpj,.mp3,.mp4,.doc,.ppt…..),使用字节流处理
*
package com.atguigu.java1;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 测试FileInputStream和FileOutputStream的使用
*
* 结论:
* 1,对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
* 2,对于非文本文件(.jpj,.mp3,.mp4,.doc,.ppt.....),使用字节流处理
*
*
* @author Dxkstart
* @create 2021-05-30 12:41
*/
public class File_Input_OutputStream_Test {
//使用字节流FileInputStream处理文本文件,可能出现乱码。
@Test
public void test_FileInputStream(){
FileInputStream fis = null;
try {
//1.造文件
File file = new File("Hello1.txt");
//2.造流
fis = new FileInputStream(file);
//3.读数据
byte[] buffer = new byte[5];
int len;//记录每次读取的字节的个数
while((len = fis.read(buffer)) != -1) {
String str = new String(buffer, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//4.关闭流资源
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
实现对图片的复制操作
*/
@Test
public void test_FileInput_OutputStream(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File file1 = new File("C:\\Users\\Administrator\\Desktop\\IO\\图片\\爱丽丝.jpg");
File file2 = new File("爱丽丝1.jpg");
//
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//复制的过程
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//指定路径下文件的复制
@Test
public void copyFile(String srcPath,String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File file1 = new File(srcPath);
File file2 = new File(destPath);
//
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//复制的过程
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Administrator\\Desktop\\IO\\图片\\螺旋丸.jpg";
String destPath = "C:\\Users\\Administrator\\Desktop\\IO\\图片\\螺旋丸2.jpg";
copyFile(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作花费的时间为:" + (end - start));
}
}
package com.atguigu.java1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Dxkstart
* @create 2021-05-30 14:13
*/
public class testCopy {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Administrator\\Desktop\\IO\\视频\\01.mp4";
String destPath = "C:\\Users\\Administrator\\Desktop\\IO\\视频\\02.mp4";
new copy().copyFile(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作花费的时间为:" + (end - start)); //380ms
}
}
class copy{
public void copyFile(String srcPath,String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File file1 = new File(srcPath);
File file2 = new File(destPath);
//
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//复制的过程
byte[] buffer = new byte[102400];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}