Java I / O - 增加新目标文件的名称

时间:2013-01-21 14:13:01

标签: java url io

此代码下载4张带照片的链接。 有没有办法用新名称创建新的.png文件? 例如:image1.png,image2.png,image3.png自动

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class AfbeeldingDown {


    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
        URL url1 = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
        URL url2 = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
        URL url3 = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
        InputStream in = url.openStream();

        OutputStream out = new FileOutputStream("image.png");

        int r;

        while ((r = in.read())!= -1) {
            out.write(r);
        }

        in.close();
        out.close();

    }

}

3 个答案:

答案 0 :(得分:1)

你可以这样做

ArrayList<URL> urlList = new ArrayList();
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
int i = 1;
for (URL url : urlList) {
  InputStream in = url.openStream();

  OutputStream out = new FileOutputStream("image" + i + ".png");
  i++;
  int r;
  while ((r = in.read()) != -1) {
   out.write(r);
   }
   in.close();
   out.close();
 }

更简单

ArrayList<String> imageNames = new ArrayList();
imageNames.add("mazda-2013-tokyo-auto-salon.jpg");
imageNames.add("mazda-2013-Volkswagen-CrossBlue-main.jpg");
for ( String imageName : imageNames) {
    URL url = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/"+imageName);
    InputStream in = url.openStream();
    OutputStream out = new FileOutputStream(imageName);
    int r;
    while ((r = in.read()) != -1) {
        out.write(r);
    }
    in.close();
    out.close();
}

答案 1 :(得分:0)

您可以更新代码,以便将您希望它创建的文件名作为参数:

 public static void main(String[] args) throws IOException {
        //same code as before
        OutputStream out = new FileOutputStream(arg[0]);

        int r;

        while ((r = in.read())!= -1) {
            out.write(r);
        }

        in.close();
        out.close();
}

之后,您可以进行各种改进:

  • 验证输入的名称,查看它是否为空(在这种情况下使用默认文件名),以及它是否是当前操作系统支持的有效文件名。
  • 仅传递文件名模板作为参数(例如“image”)并使其在结尾处添加一个增量,以便每次都创建一个新文件(例如:如果存在“image1.png”,则新文件名将为“image2.png”)。您需要首先列出当前目录中以“[template]”开头的所有文件

答案 2 :(得分:0)

使用简单的类,这是可能的:

// Lacks checking, not thread safe etc -- left as an exercise
public final class CountingFileGenerator
{
    private final String prefix, suffix;
    private int count = 1;

    public CountingFileGenerator(final String prefix, final String suffix)
    {
        this.prefix = prefix;
        this.suffix = suffix;
    }

    public OutputStream newFile()
        throws IOException
    {
        final String name = String.format("%s%d%s", prefix, count, suffix);
        final OutputStream ret = new FileOutputStream(name);
        count++;
        return ret;
    }
}

用法:

final CountingFileGenerator generator = new CountingFileGenerator("image", ".png");

final OutputStream o1 = generator.newFile();
final OutputStream o2 = generator.newFile();

等等。

当然,不要忘记关闭返回的流。