Question

I have this method that downloads .csv files from yahoo finance and saves them locally. It is accessed during a loop so it is downloading many files from a list. However sometimes a symbol is entered incorrectly, no longer exists, or the connection times out. How can I amend this method so that connection time outs are retried and incorrect symbols (meaning the url does not work) are just skipped over without ending the program?

public static void get_file(String symbol){

    OutputStream outStream = null;
    URLConnection  uCon = null;
    InputStream is = null;

    String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
    String destination = "C:/"+symbol+"_table.csv";

    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(finance_url);

        outStream = new BufferedOutputStream(new FileOutputStream(destination));

        uCon = Url.openConnection();
        is = uCon.getInputStream();         
        buf = new byte[size];

        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }

    }catch (Exception e) {
        System.out.println("Error while downloading "+symbol);
        e.printStackTrace();
    }finally {
        try {
            is.close();
            outStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Was it helpful?

Solution

Why not call the method again when an exception is thrown. You can narrow down the exception type to indicate when a retry should be initiated.

public static void get_file(String symbol){

    OutputStream outStream = null;
    URLConnection  uCon = null;
    InputStream is = null;

    String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
    String destination = "C:/"+symbol+"_table.csv";

    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(finance_url);

        outStream = new BufferedOutputStream(new FileOutputStream(destination));

        uCon = Url.openConnection();
        is = uCon.getInputStream();         
        buf = new byte[size];

        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }

    }catch (Exception e) {
      getFile(symbol);
    }finally {
        try {
            is.close();
            outStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top