Java io流完成復制粘貼功能


JAVA 中io字節輸入輸出流 完成復制粘貼功能:

    public static void main(String[] args) throws Exception{
        // 創建輸入流要讀取的文件
        FileInputStream  fis = new FileInputStream("f:yxx.txt");
        //創建要輸出的文件路徑
        FileOutputStream fos = new FileOutputStream("f:Yao/y.txt");
        //創建變量  用於接收 字節流讀取返回字節
        int len = 0;
        while((len = fis.read())!=-1){
            fos.write(len);
        }
        //關閉數據流
        fis.close();
        fos.close();
    }

使用IO字節數組輸入輸出流  完成復制粘貼功能: 這個方法比較快

    // 創建輸入流要讀取的文件
        FileInputStream  fis = new FileInputStream("f:s.exe");
        //創建要輸出的文件路徑
        FileOutputStream fos = new FileOutputStream("f:Yao/s.exe");
        //創建變量  用於接收 字節流讀取返回字節
        int len = 0;
        byte[] by = new byte[1024];
        while((len = fis.read(by))!=-1){
            fos.write(by,0,len);
        }
        //關閉數據流
        fis.close();
        fos.close();
    }

使用io流 字符流來復制  注意只能復制文本!!

 

/*
* 字符流的復制功能 只能復制 文本文件
* 循環一定要刷新緩沖區
* */
public static void main(String[] args)throws IOException {
FileReader fr = new FileReader("E:gu.txt");
FileWriter fw = new FileWriter("e:gubin/1.txt");

char[] cbuff = new char[1024];
int len = 0;
while ((len = fr.read(cbuff))!=-1) {
fw.write(cbuff);
fw.flush();
}
fr.close();
fw.close();
}

 

 

=====================


/*
* 文件復制方式,字節流,一共4個方式
* 1. 字節流讀寫單個字節 125250 毫秒
* 2. 字節流讀寫字節數組 193 毫秒 OK
* 3. 字節流緩沖區流讀寫單個字節 1210 毫秒
* 4. 字節流緩沖區流讀寫字節數組 73 毫秒 OK
*/
public class Copy {
public static void main(String[] args)throws IOException {
long s = System.currentTimeMillis();
copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));
long e = System.currentTimeMillis();
System.out.println(e-s);
}
/*
* 方法,實現文件復制
* 4. 字節流緩沖區流讀寫字節數組
*/
public static void copy_4(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
/*
* 方法,實現文件復制
* 3. 字節流緩沖區流讀寫單個字節
*/
public static void copy_3(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
}

/*
* 方法,實現文件復制
* 2. 字節流讀寫字節數組
*/
public static void copy_2(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}

/*
* 方法,實現文件復制
* 1. 字節流讀寫單個字節
*/
public static void copy_1(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM