When i executes the below code:
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();
}
}
}
i am getting below exception:
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)
i am giving these values:
sourceCSVURL =http://finance.yahoo.com/d/quotes.csv
destinationCSVURL =http://finance.yahoo.com/d/quotes.csv
The error message
java.net.UnknownHostException: finance.yahoo.com
means that your java program is unable to resolve the IP address offinance.yahoo.com
.Try and see if you can download the CSV file from the actual machine that will be running this program. My guess is that your java program doesn't have internet connectivity to resolve DNS to
finance.yahoo.com