Java架构师之旅(三十二 基础IO流)

夜光序言:

 不积跬步无以至千里不积小流无以成江海~~,但是还是要夯实基础,做项目你就会知道基础不稳的后果了,调试会很烦~


正文:

package com.Genius.byteIO;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

public class CopyFile {

 

public static void main(String[] args) throws FileNotFoundException,IOException {

    /*1、夜光:我们肯定需要先建立联系嗯

     * 源:存在且为文件+目的地(文件可以不存在)

     *

     * */

    File src = new File("E:/Genius/parent/test/software.txt");

    File dest = new File("E:/Genius/parent/test/copysoftware.txt");

    

    //2、我们选择流

    

    InputStream is = new FileInputStream(src);

    OutputStream os = new FileOutputStream(dest);

    

    //3、文件拷贝+读取+写出+循环

    //夜光:下面这个是数组嗯~~

    byte[] flush = new byte[1024];

    int len = 0;

    while(-1 != (len=is.read(flush))){

     //下面,我们写出

     os.write(flush, 0, len);

    }

    os.flush(); //强制退出一下~~

    

    //关闭,先打开的先关闭

    os.close();

    is.close();

    

    

}

 

}

 

package com.Genius.byteIO;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

public class CopyFile {

 

public static void main(String[] args){

       String src = "E:/Genius/parent/test/software.txt";

       String dest = "E:/Genius/parent/test/copysoftware1.txt";

       try {

copyFile1(src, dest);

} catch (FileNotFoundException e) {

e.printStackTrace();

System.out.println("文件不存在~~");

} catch (IOException e) {

e.printStackTrace();

System.out.println("拷贝文件失败~~");

}

}

 

 

 

 

private static void copyFile1(String srcPath,String destPath) throws FileNotFoundException,IOException{

/*1、夜光:我们肯定需要先建立联系嗯

     * 源:存在且为文件+目的地(文件可以不存在)

     *

     * */

    File src = new File(srcPath);

    File dest = new File(destPath);

    

    //2、我们选择流

    

    InputStream is = new FileInputStream(src);

    OutputStream os = new FileOutputStream(dest);

    

    //3、文件拷贝+读取+写出+循环

    //夜光:下面这个是数组嗯~~

    byte[] flush = new byte[1024];

    int len = 0;

    while(-1 != (len=is.read(flush))){

     //下面,我们写出

     os.write(flush, 0, len);

    }

    os.flush(); //强制退出一下~~

    

    //关闭,先打开的先关闭

    os.close();

    is.close();

 

}

 

 

 

 

public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {

 

}

 

}

 

猜你喜欢

转载自blog.csdn.net/weixin_41987706/article/details/86534541