0

我正在使用 Commons IO 从 Internet 下载文件。

这是我正在使用的方法:

public void getFile(String url){

File f = new File("C:/Users/Matthew/Desktop/hello.txt");
    PrintWriter pw = new PrintWriter(f);
    pw.close();
    URL url1;
    try {
        url1 = new URL(url);
        FileUtils.copyURLToFile(url1, f);
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }catch (IOException e1){
        e1.printStackTrace();
    }
}

有没有办法我可以使用这种方法下载多个文件并将它们全部保存到 hello.txt 文件中?使用上述方法,所有内容都会被覆盖,最后下载的文件将是添加到 hello.txt 文件中的文件。

基本上,有没有一种方法可以将多个文件下载存储在一个文件中。

谢谢。

4

1 回答 1

0

没有办法使用FileUtils. 但是,如果您想使用 Apache Commons,我建议您执行以下操作:

File f = new File("C:/Users/Matthew/Desktop/hello.txt");
URL url1;
try {
    url1 = new URL(url);
    IOUtils.copy(url1.openStream(), new FileOutputStream(f, true));
} catch (MalformedURLException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

它或多或少做同样的事情,但在FileOutputStream.

于 2015-03-14T20:26:37.893 回答