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

📄 filecopy.java

📁 这是一个你能自己设置缓冲区大小的程序
💻 JAVA
字号:
import java.io.*;
import java.util.StringTokenizer;
public class FileCopy
{
  public FileCopy()
  {
  }
  public static void main(String [] args)
  {
    String inputFileName = "";
    String outputFileName = "";
    try
    {
      if (args.length > 2)
      {
        inputFileName = args[0];
        outputFileName = args[1];
      }
      else
      {
        System.out.println(
            "you can run this program as : FileCopy sorceFileName targetFileName");
        BufferedReader consoleInput = new BufferedReader(new InputStreamReader(
            System.in));
	while(inputFileName.equals(""))
        {
          System.out.println("\nplease input the source file name:");
          inputFileName = consoleInput.readLine();
          if(inputFileName.equals(""))
            System.out.println("the source file name cann't be null");
        }
        while(outputFileName.equals(""))
        {
          System.out.println("\nplease input the target file name:");
          outputFileName = consoleInput.readLine();
          if(outputFileName.equals(""))
            System.out.println("the target file name cann't be null");
        }
      }
      //去除文件名的引号(如果有的话)
      StringTokenizer st = new StringTokenizer(inputFileName, "\"");
      inputFileName = st.nextToken();
      st = new StringTokenizer(outputFileName, "\"");
      outputFileName = st.nextToken();
      File sourceFile = new File(inputFileName);
      File targetFile = new File(outputFileName);
      totalBytes = sourceFile.length();

      DataInputStream in = new DataInputStream(new FileInputStream(sourceFile));
      DataOutputStream out = new DataOutputStream(new FileOutputStream(targetFile));
      long startInitTime = System.currentTimeMillis();
      long determineSize = totalBytes > 10485760 ? 10485760 : totalBytes;
      determineSize = determineSize < 524288 ? 524288 : determineSize;
      byte[] buffer = new byte[(int)determineSize];
      System.out.println("buffer size is:\t" + buffer.length + ", cost initTime: "
	+ (System.currentTimeMillis() - startInitTime) + " ms\n");
      int lenthToRead = 0;
      new Thread()
      {
        public void run()
        {
          try
          {
	    int seconds = 0;
            while (true)
            {
              Thread.sleep(2000);
              seconds += 2;
              System.out.println((totalBytesReaded / 1024) + "\t\tKBytes has been copyed!\tspeed is "
                  + (totalBytesReaded / 1024 / 1024 / seconds) + "MB/s" + "\t" + (totalBytesReaded * 100 / totalBytes) + "% completed!");
            }
          }
          catch (InterruptedException e)
          {
            e.printStackTrace();
          }
        }
      }.start();
      long starTime = System.currentTimeMillis();
      while ((lenthToRead = in.read(buffer)) != -1)
      {
        out.write(buffer, 0, lenthToRead);
        totalBytesReaded += lenthToRead;
      }
      in.close();
      out.close();
      System.out.println("\ncopy sussessfully! total time is: " +
                         (System.currentTimeMillis() - starTime)
                         + "ms.\ttotal size is:" + totalBytes);
      System.out.println("copy source file:");
      System.out.println(sourceFile.getAbsolutePath());
      System.out.println("to the target file:");
      System.out.println(targetFile.getAbsolutePath() + "\n\n");
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    System.exit(0);
  }
  private static long totalBytesReaded = 0;
  private static long totalBytes = 0;
}

⌨️ 快捷键说明

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