chinabugotech/hutool · error · IORuntimeException

File not exist: {}

Error message

File not exist: {}

What it means

FileCopier.copy() is the entry point that copies src to dest. As a precondition it asserts the source exists; if this.src does not exist it throws IORuntimeException("File not exist: {}") before doing any work.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/io/file/FileCopier.java:175

	 * 2、源为文件,目标为不存在路径,则目标以文件对待(自动创建父级目录)比如:/dest/aaa,如果aaa不存在,则aaa被当作文件名
	 * 3、源为文件,目标是一个已存在的文件,则当{@link #setOverride(boolean)}设为true时会被覆盖,默认不覆盖
	 * 4、源为目录,目标为已存在目录,当{@link #setCopyContentIfDir(boolean)}为true时,只拷贝目录中的内容到目标目录中,否则整个源目录连同其目录拷贝到目标目录中
	 * 5、源为目录,目标为不存在路径,则自动创建目标为新目录,然后按照规则4复制
	 * 6、源为目录,目标为文件,抛出IO异常
	 * 7、源路径和目标路径相同时,抛出IO异常
	 * </pre>
	 *
	 * @return 拷贝后目标的文件或目录
	 * @throws IORuntimeException IO异常
	 */
	@Override
	public File copy() throws IORuntimeException{
		final File src = this.src;
		File dest = this.dest;
		// check
		Assert.notNull(src, "Source File is null !");
		if (false == src.exists()) {
			throw new IORuntimeException("File not exist: " + src);
		}
		Assert.notNull(dest, "Destination File or directiory is null !");
		if (FileUtil.equals(src, dest)) {
			throw new IORuntimeException("Files '{}' and '{}' are equal", src, dest);
		}

		if (src.isDirectory()) {// 复制目录
			if(dest.exists() && false == dest.isDirectory()) {
				//源为目录,目标为文件,抛出IO异常
				throw new IORuntimeException("Src is a directory but dest is a file!");
			}
			if(FileUtil.isSub(src, dest)) {
				throw new IORuntimeException("Dest is a sub directory of src !");
			}

			final File subTarget = isCopyContentIfDir ? dest : FileUtil.mkdir(FileUtil.file(dest, src.getName()));
			internalCopyDirContent(src, subTarget);
		} else {// 复制文件

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Verify src.exists() before building or calling copy().
  2. Construct the FileCopier only after confirming the source is present.
  3. Surface the absolute src path in your own error for quicker diagnosis.

Example fix

// before
File result = FileCopier.create(src, dest).copy(); // throws if src missing

// after
if (!src.exists()) {
    throw new FileNotFoundException("source not found: " + src.getAbsolutePath());
}
File result = FileCopier.create(src, dest).copy();
Defensive patterns

Strategy: validation

Validate before calling

if (src != null && src.exists()) {
    return FileCopier.create(src, dest).copy();
}
throw new java.io.FileNotFoundException("source not found: " + src);

Type guard

static boolean isCopyableSource(File f) {
    return f != null && f.exists();
}

Try / catch

try {
    return FileCopier.create(src, dest).copy();
} catch (IORuntimeException e) {
    if (e.getMessage().startsWith("File not exist")) {
        // source missing: regenerate or report
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling copy() on a FileCopier built with a source File that does not exist on disk.

Common situations: Source path misconfigured or stale; the file deleted before the copy runs; FileCopier constructed with a path for a file that has not been generated yet.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/161378abfe1e3638. Report an issue: GitHub.