使用多线程实现文件复制

    xiaoxiao2021-12-12  8

    package com.zhi.lianxi1; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Calendar; import java.util.Scanner; //使用多线程实现文件的复制功能, //并在控制台显示复制的进度,进度以百分比表示。 //例如:把文件A复制到E盘某文件夹下, //在控制台上显示“文件已复制10%”,“文件已复制20%”……“文件已复制100%”,“复制完成!” public class Lianxi extends Thread{ public void run(){ Scanner sc =new Scanner(System.in); System.out.println("请输入文件源目录:"); String str=sc.next(); FileReader f = null; try { f = new FileReader(str); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("请输入拷贝目录的路径:"); String str1=sc.next(); File newfile = new File(str1); File oldfile = new File(str); String name = newfile+"\\"+oldfile.getName(); FileWriter w = null; try { w = new FileWriter(name); } catch (IOException e) { e.printStackTrace(); } int i; String s=""; try { while((i=f.read())!=-1){ try { w.write(i); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e1) { e1.printStackTrace(); } try { w.close(); } catch (IOException e) { e.printStackTrace(); } try { f.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"已复制成功"); }

    }

    ————————————————————————————————————————————————————————————————————————

    package com.zhi.lianxi1; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class Lianxi2 extends Thread{ public void run() { Scanner sc =new Scanner(System.in); System.out.println("请输入文件源目录:"); String str=sc.next(); try { FileReader f = new FileReader(str); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("请输入拷贝目录的路径:"); String str1=sc.next(); } }

    ——————————————————————————————————————————————————————————————————————————

    package com.zhi.lianxi1; public class Text { public static void main(String[] args) { Lianxi li = new Lianxi(); li.start(); } }

    转载请注明原文地址: https://ju.6miu.com/read-900022.html

    最新回复(0)