safecopy.java

来自「Java程序设计技巧与开发实例附书源代码。」· Java 代码 · 共 63 行

JAVA
63
字号

import java.io.*;

public class SafeCopy
{
  public static void copyFile(DataInputStream in, DataOutputStream out) throws
      IOException
  {
    try
    {
      while (true)
      {
        out.writeByte(in.readByte());
      }
    }
    catch (EOFException eof)
    {
      return;
    }
  }

  public static void main(String args[])
  {
    if (args.length != 2)
    {
      System.out.println("Usage: java Copy sourceFile targetFile");
    }
    else
    {
      String inFileName = args[0], outFileName = args[1];
      File inFile = new File(inFileName);
      File outFile = new File(outFileName);
      if (!inFile.exists())
      {
        System.out.println(inFileName + " does not exist.");
      }
      else if (outFile.exists())
      {
        System.out.println(outFileName + " already exists.");
      }
      else
      {
        try
        {
          DataInputStream in = new DataInputStream(
              new BufferedInputStream(
              new FileInputStream(inFileName)));
          DataOutputStream out = new DataOutputStream(
              new BufferedOutputStream(
              new FileOutputStream(outFileName)));
          copyFile(in, out);
          in.close();
          out.close();
        }
        catch (IOException ioe)
        {
          System.out.println("Unknown error: " + ioe);
        }
      }
    }
  }
}

⌨️ 快捷键说明

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