其他分享
首页 > 其他分享> > 字节流

字节流

作者:互联网

字节流   
输入流FileInputStream,读文件
输出流FileOutputStream,写文件

           FileInputStream inputStream=new FileInputStream(srcFile);
           FileOutputStream outStream=new FileOutputStream(destFile);


字节缓冲输入流:BufferedInputStream bit=new BufferedInputStream(new FileInputStream(srcFile));

字节缓冲输出流:BufferedOutputStream bot=new BufferedOutputStream(new FileOutputStream(destFile));
package task;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TaskIo {
public  static void  copyFile(File srcFile) throws IOException {
    File destFile=new File("e:\\b",srcFile.getName());//此处修改目标文件夹
    FileInputStream inputStream=new FileInputStream(srcFile);
    FileOutputStream outStream=new FileOutputStream(destFile);
    byte[] bytes=new byte[1024];
    int len=0;
    while((len=inputStream.read(bytes))!=-1) {
        outStream.write(bytes,0,len);
    }
    inputStream.close();
    outStream.close();
}
public static void copyFolder(File srcFile) throws IOException {
    
    File[] files=srcFile.listFiles();
    
    for(File f:files) {
        if(f.isDirectory()) {
            copyFolder(f);
        }else {
            copyFile(f);
        }
    }
    
    
}

public static void deleteFile(File srcFile) {
    File[] files=srcFile.listFiles();
    for(File f:files) {
        if(f.isDirectory()) {
            deleteFile(f);
        }else {
            f.delete();
        }
    }
    
    
}
public static void deleteFile(String srcPath) {
    File src=new File(srcPath);
    File[] files=src.listFiles();
    for(File f:files) {
        if(f.isDirectory()) {
            deleteFile(f);
        }else {
            f.delete();
        }src.delete();//复制到新文件夹后,把原来路径总文件夹也删了
    }
    
    
}


    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
//把e盘a文件夹下的tiku.txt文件,复制到b文件夹里,复制一个文件
//        FileInputStream stream=new FileInputStream("E:\\a\\tiku.txt");
//        FileOutputStream outStream=new FileOutputStream("E:\\b\\tiku.txt");
//        byte[] bytes=new byte[1024];
//        int len=0;
//        while((len=stream.read(bytes))!=-1) {
//            outStream.write(bytes,0,len);
//        }
//        stream.close();
//        outStream.close();
//    把e盘a文件夹下的所有文件,复制到b文件夹里,b文件夹必须存在
//        File srcFile=new File("e:\\a");
//        copyFolder(srcFile);
//    把e盘a文件夹下的所有文件,剪切到b文件夹里,b文件夹必须存在
        
//        File srcFile=new File("e:\\a");
//        copyFolder(srcFile);
//        deleteFile(srcFile);
    
        
//        复制指定文件夹到指定目录,指定目录可能不存在
//        String srcPath="e:\\b";
//        String destPath="e:\\a";
//        copyDir1(srcPath,destPath);
//        deleteFile(srcPath);
        
//        复制的文件内容很大,分别用读取一组字节,字节缓冲流读取一组字节,看哪个快
        
//        String srcPath="e:\\java\\视频";
//        String destPath="e:\\b";
//        long a=System.currentTimeMillis();
//        copyDir1(srcPath,destPath);
//        long b=System.currentTimeMillis();
//        System.out.println(b-a);
//        74965
//        String srcPath="e:\\java\\视频";
//        String destPath="e:\\c";
//        long a=System.currentTimeMillis();
//        copyDir2(srcPath,destPath);
//        long b=System.currentTimeMillis();
//        System.out.println(b-a);
//        52310
        
        
    }
public static void copyDir1(String srcPath,String destPath) throws IOException {
    
    File src=new File(srcPath);
    
    File dest=new File(destPath);
    
    File[] srcFiles=src.listFiles();
    if(!dest.exists()) {
        dest.mkdirs();
    }
    for(File srcFile:srcFiles) {
        if(srcFile.isDirectory()) {
            String dirName=srcFile.getName();
            File newDestDir=new File(dest,dirName);
            //递归,用来复制源文件的所有文件夹(不含文件)
            copyDir1(srcFile.getPath(),newDestDir.getPath());
            
        }else {
            String destName=srcFile.getName();
            File destFile=new File(dest,destName);
            copyFile1(srcFile,destFile);
        }
    }
    }
public static void copyDir2(String srcPath,String destPath) throws IOException {
    
    File src=new File(srcPath);
    
    File dest=new File(destPath);
    
    File[] srcFiles=src.listFiles();
    if(!dest.exists()) {
        dest.mkdirs();
    }
    for(File srcFile:srcFiles) {
        if(srcFile.isDirectory()) {
            String dirName=srcFile.getName();
            File newDestDir=new File(dest,dirName);
            //递归,用来复制源文件的所有文件夹(不含文件)
            copyDir2(srcFile.getPath(),newDestDir.getPath());
            
        }else {
            String destName=srcFile.getName();
            File destFile=new File(dest,destName);
            copyFile2(srcFile,destFile);
        }
    }
    }
        
    public static void copyFile1(File srcFile,File destFile) throws IOException {
        
        FileInputStream inputStream=new FileInputStream(srcFile);
        FileOutputStream outStream=new FileOutputStream(destFile);
        byte[] bytes=new byte[1024];
        int len=0;
        while((len=inputStream.read(bytes))!=-1) {
            outStream.write(bytes,0,len);
        }
        inputStream.close();
        outStream.close();
        
        
    }    
public static void copyFile2(File srcFile,File destFile) throws IOException {
        
        BufferedInputStream bit=new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bot=new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] bytes=new byte[1024];
        int len=0;
        while((len=bit.read(bytes))!=-1) {
            bot.write(bytes,0,len);
        }
        bit.close();
        bot.close();
        
        
    }        
    
    }
    
    
    
    
    
    
    
    
    

 



标签:String,srcFile,文件夹,File,srcPath,new,字节
来源: https://www.cnblogs.com/j99426/p/15078231.html