0

Commons IO を使用して、インターネットからファイルをダウンロードしています。

これは私が使用している方法です:

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 ファイルに追加されます。

基本的に、複数のファイルのダウンロードを 1 つのファイルに保存する方法はありますか。

ありがとう。

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 に答える