如何正确使用FileUtils IO? [英] How to use FileUtils IO correctly?

查看:164
本文介绍了如何正确使用FileUtils IO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用方法(信用证 Shenyuan Lu ):

I tried to use method (credits to Shenyuan Lu ):

org.apache.commons.io.FileUtils.copyURLToFile(URL,File)

准确地说:

org.apache.commons .io.FileUtils.copyURLToFile(driver.getCurrentUrl(),C:\\Users\\ myDocs \\myfolder\myFile.pdf);

然而, Eclipse 正在强调 copyURLToFile 并建议使用的CopyFile 。当我切换到 copyFile 时,它建议再次使用 copyURLToFile ???

However,Eclipse is underlining the copyURLToFile and suggesting to use copyFile. When I switch to copyFile, it suggests to use copyURLToFile again???

推荐答案

根据 FileUtils #copyURLToFile()API ...

第一个参数需要是 URL 对象,第二个参数需要是文件宾语。你正在传递Strings作为两个args。尝试...

The first parameter needs to be a URL object, and the second parameter needs to be a File Object. You are passing Strings as both args. Try...

org.apache.commons.io.FileUtils.copyURLToFile(new URL(driver.getCurrentUrl()), new File("C:\\Users\\myDocs\\myfolder\\myFile.pdf"));

此外,由于此方法抛出异常,您需要使方法抛出异常,或者在try {} catch {}块中包含该语句。

Also, since this method throws an exception, you either need to make your method throw an exception, or surround the statement in a try {} catch{} block.

例如

try {
    org.apache.commons.io.FileUtils.copyURLToFile(new URL(driver.getCurrentUrl()), new File("C:\\Users\\myDocs\\myfolder\\myFile.pdf"));
} catch (Exception x) { x.printStackTrace(); }

这篇关于如何正确使用FileUtils IO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆