⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copypastefile.java

📁 java实现文件复制粘贴文件的源程序
💻 JAVA
字号:
import java.io.*;
class Progress extends Thread
{
	private CopyPaste copypaste = null;
	private boolean   bfinished = false;
	private String    sPercent = "Process: 0%";
	public Progress(CopyPaste cp)
	{
		copypaste = cp;
	}
	public synchronized void run()
	{
		if(copypaste == null)
		{
			System.out.println("Error: Get progress failed!");
			return;
		}
		while(bfinished == false)
		{
			try 
			{
				wait();
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			System.out.println(sPercent);
		}
	}
	public synchronized void setFinished(boolean finish)
	{
		bfinished = finish;
		notify();
	}
	public synchronized void setPercent(String percent)
	{
		sPercent = percent;
		notify();
	}
}

class CopyPaste extends Thread
{
	private String sourcePath = "";
	private String destPath = "";
	private FileInputStream fis  = null;
	private FileOutputStream fos = null;
	private long fileLen = 0;
	public CopyPaste(String sPath, String dPath)
	{
		sourcePath = sPath;
		destPath = dPath;
	}
	public void run()
	{
		File sFile = new File(sourcePath);
		File dFile = new File(destPath);
		String smd5 = "";
		String dmd5 = "";
		if(!sFile.exists())
		{
			System.out.println("Error: The source file is not exist!");
			return;
		}
		try
		{
			smd5 = MD5Util.getFileMD5String(sFile);
			System.out.println("Source File MD5: " + smd5);
		} 
		catch (IOException e) 
		{
			System.out.println(e.getMessage());
			return;
		}
		if(!dFile.exists())
		{
			try
			{
				int idx = destPath.lastIndexOf('/');
				if(idx > 0)
				{
					String path = destPath.substring(0, idx);
					File pathFile = new File(path);
					if(!pathFile.isDirectory())
					{
						pathFile.mkdirs();
					}
				}
				if(!dFile.createNewFile())
				{
					System.out.println("Error: The dest file created failed!");
					return;
				}
			}
			catch (IOException e) 
			{
				System.out.println(e.getMessage());
				return;
			}
		}
		fileLen = sFile.length();
		byte[] buf = new byte[8092];
		try 
		{
			fis = new FileInputStream(sFile);
			fos = new FileOutputStream(dFile);
		}
		catch (FileNotFoundException e) 
		{	
			System.out.println(e.getMessage());
			return;
		}
		int ilen = 0;
		long isumlen = 0;
	    try 
	    {
	    	long oldper = 0;
			Progress prg = new Progress(this);
			prg.start();
			while((ilen=fis.read(buf))!=-1) 
			{
				fos.write(buf, 0, ilen);
				isumlen += ilen;
				long per = (long)(100*isumlen/fileLen);
				
				if(oldper == per)
					continue;
				else
				{
					prg.setPercent("Process: "+ Long.toString(per)+"%");
					oldper = per;
				}
			}
			try 
			{
				sleep(100);
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			try
			{
				dmd5 = MD5Util.getFileMD5String(dFile);
				System.out.println("Dest File MD5: "+ dmd5);
				if(dmd5.equals(smd5))
				{
					System.out.println("Copying and Pasting Completed!");
				}
				else
				{
					System.out.println("Copying and Pasting Failed!");
				}
			} 
			catch (IOException e) 
			{
				System.out.println(e.getMessage());
				return;
			}
			prg.setFinished(true);
		}
	    catch (IOException e)
		{
	    	System.out.println(e.getMessage());
			return;
		} 
		return;
	}
}

public class CopyPasteFile 
{
	public static void main(String[] args) 
	{
		CopyPaste cp = new CopyPaste(args[0], args[1]);
		cp.start();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -