0

当我执行以下代码时:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;



import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Transform {
    public static Logger logger = Logger.getLogger(Transform.class);

    public static void main(String[] args){
        //Logger logger = Logger.getLogger(Transform.class);
        try
        {
        PropertyConfigurator.configure("log4j.properties");
        Properties prop = new Properties();
        String mode = null;
        URL url = null;
        //String filePath = null;
        StringBuilder sb = null;
        String csv = null;
        String separator = null;
        URL sourceCSVURL = null;
        URL destinationCSVURL = null;
        String sourceCSVFile = null;
        String destinationCSVFile = null;
        String sourceJson = null;
        String destinationJson = null;
        int flag = 0;
        prop.load(new FileInputStream("jsonGenerator.properties"));


        mode = prop.getProperty("mode");

        separator = prop.getProperty("separator");
        if(!prop.getProperty("sourceCSVURL").trim().equals(""))
        {
        sourceCSVURL = new URL(prop.getProperty("sourceCSVURL").toString());
        }
        if(!prop.getProperty("destinationCSVURL").trim().equals(""))
        destinationCSVURL = new URL(prop.getProperty("destinationCSVURL").toString());
        sourceCSVFile = prop.getProperty("sourceCSVFile");
        destinationCSVFile = prop.getProperty("destinationCSVFile");
        sourceJson = prop.getProperty("sourceJson");
        destinationJson = prop.getProperty("destinationJson");


        for(int a=0;a<2;a++)
        {
        if(mode.trim().toLowerCase().equals("url"))
        {
            if(flag == 0)
            {
                csv=IOUtils.toString(sourceCSVURL);
            }
            else
            {
                csv=IOUtils.toString(destinationCSVURL);
            }
        }
        else if(mode.trim().toLowerCase().equals("file"))
        {
            byte[] bytes=null;
            if(flag == 0)
            {
                bytes = org.apache.commons.io.FileUtils.readFileToByteArray(new File(sourceCSVFile));
                csv = new String(bytes);
            }
            else
            {
                bytes = org.apache.commons.io.FileUtils.readFileToByteArray(new File(destinationCSVFile));
                csv = new String(bytes);
            }
        }




        sb = new StringBuilder("{\n\t\"items\": [\n");
        csv = csv.replace("\r", "");
        String csvValues[] = csv.split("\n");
        String header[]=csvValues[0].split(separator);

        for(int i=1;i<csvValues.length;i++){
            sb.append("\t").append("{").append("\n");
            String tmp[]=csvValues[i].split(separator);
            for(int j=0;j<tmp.length;j++){
                sb.append("\t").append("\t\"").append(header[j]).append("\":\"").append(tmp[j]).append("\"");
                if(j<tmp.length-1){
                    sb.append(",\n");
                }else{
                    sb.append("\n");
                }
            }
            if(i<csvValues.length-1){
                sb.append("\t},\n");
            }
            else{
                sb.append("\t}\n");
            }
        }
        File file =null;
        sb.append("]\n}");
        if(flag==0)
        {
            file = new File(sourceJson);
        }
        else
        {
            file = new File(destinationJson);
        }
        flag++;

        FileUtils.writeStringToFile(file,sb.toString());

        }
        logger.info("Successfully Executed");
        }
        catch(MalformedURLException e)
        {    
             logger.error("Error : " + e.getMessage());
            e.printStackTrace();

        }

        catch (Exception e) {
            logger.error("Error : " + e.getMessage());
            e.printStackTrace();
        }



    }
}

我得到以下异常:

java.net.UnknownHostException: finance.yahoo.com
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:742)
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:727)
    at Transform.main(Transform.java:63)

我给出了这些值:

sourceCSVURL =http://finance.yahoo.com/d/quotes.csv
destinationCSVURL =http://finance.yahoo.com/d/quotes.csv
4

1 回答 1

0

该错误信息java.net.UnknownHostException: finance.yahoo.com意味着您的 java 程序无法解析finance.yahoo.com.

试试看是否可以从将运行该程序的实际机器上下载 CSV 文件。我的猜测是您的 java 程序没有互联网连接来解析 DNSfinance.yahoo.com

于 2013-08-26T05:26:09.997 回答