Example usage for java.io BufferedReader close

List of usage examples for java.io BufferedReader close

Introduction

In this page you can find the example usage for java.io BufferedReader close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:uk.ac.ebi.eva.test.utils.JobTestUtils.java

public static void restoreMongoDbFromDump(String dumpLocation) throws IOException, InterruptedException {
    logger.info("restoring DB from " + dumpLocation);

    Process exec = Runtime.getRuntime().exec("mongorestore " + dumpLocation);
    exec.waitFor();//  w ww  . j a va  2s . c  om
    String line;
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    while ((line = bufferedReader.readLine()) != null) {
        logger.info("mongorestore output:" + line);
    }
    bufferedReader.close();
    bufferedReader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
    while ((line = bufferedReader.readLine()) != null) {
        logger.info("mongorestore errorOutput:" + line);
    }
    bufferedReader.close();

    logger.info("mongorestore exit value: " + exec.exitValue());
}

From source file:Main.java

/**
 * @param context//from w ww. ja v  a2  s. co m
 * @param resId
 * @return
 */
public static List<String> geFileToListFromRaw(Context context, int resId) {
    if (context == null) {
        return null;
    }

    List<String> fileContent = new ArrayList<String>();
    BufferedReader reader = null;
    try {
        InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
        reader = new BufferedReader(in);
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent.add(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * same to {@link W_ResourceUtil#geFileFromRaw(Context, int)}, but return type is List<String>
 *
 * @param context//  w w  w.  j  ava 2 s .  co  m
 * @param resId
 * @return
 */
public static List<String> geFileToListFromRaw(Context context, int resId) {
    if (context == null) {
        return null;
    }

    List<String> fileContent = new ArrayList<>();
    BufferedReader reader = null;
    try {
        InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
        reader = new BufferedReader(in);
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent.add(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:eu.hansolo.fx.weatherfx.GeoCode.java

/**
 * Returns name of City for given latitude and longitude
 * @param LATITUDE/*  w  w w .  j  av a 2 s.c o m*/
 * @param LONGITUDE
 * @return name of City for given latitude and longitude
 */
public static String inverseGeoCode(final double LATITUDE, final double LONGITUDE) {
    String URL_STRING = new StringBuilder(INVERSE_GEO_CODE_URL).append(LATITUDE).append(",").append(LONGITUDE)
            .append("&outFormat=json&thumbMaps=false").toString();

    StringBuilder response = new StringBuilder();
    try {
        final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection();
        final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
        String inputLine;
        while ((inputLine = IN.readLine()) != null) {
            response.append(inputLine).append("\n");
        }
        IN.close();

        Object obj = JSONValue.parse(response.toString());
        JSONObject jsonObj = (JSONObject) obj;

        JSONArray results = (JSONArray) jsonObj.get("results");
        JSONObject firstResult = (JSONObject) results.get(0);
        JSONArray locations = (JSONArray) firstResult.get("locations");
        JSONObject firstLocation = (JSONObject) locations.get(0);
        return firstLocation.get("adminArea5").toString();
    } catch (IOException ex) {
        System.out.println(ex);
        return "";
    }
}

From source file:ReadTemp.java

/** Executes the given applescript code and returns the first line of the 
 output as a string *///  w  ww  .ja v a 2s .  co  m
static String doApplescript(String script) {
    String line;
    try {
        // Start applescript 
        Process p = Runtime.getRuntime().exec("/usr/bin/osascript -s o -");

        // Send applescript via stdin
        OutputStream stdin = p.getOutputStream();
        stdin.write(script.getBytes());
        stdin.flush();
        stdin.close();

        // get first line of output
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        line = input.readLine();
        input.close();

        // If we get an exit code, print it out
        if (p.waitFor() != 0) {
            System.err.println("exit value = " + p.exitValue());
            System.err.println(line);
        }
        return line;
    } catch (Exception e) {
        System.err.println(e);
    }

    return "";
}

From source file:com.othermedia.exampleasyncimage.AsyncImageDemo.java

public static String stringFromURL(String address) throws Exception {

    StringBuilder result = new StringBuilder();
    URL url = new URL(address);
    HttpGet httpRequest = new HttpGet(url.toURI());
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
    InputStream input = bufHttpEntity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

    String line;//from w  w  w  .j  a  va 2 s  . c om
    while ((line = reader.readLine()) != null) {
        result.append(line);
    }
    reader.close();

    return result.toString();
}

From source file:eu.hansolo.fx.weatherfx.GeoCode.java

/**
 * Returns a JavaFX Point2D object that contains latitude (y) and longitude(x) of the given
 * Address./*from  w w  w. jav a  2  s. c  o m*/
 * Example format for STREET_CITY_COUNTRY: "1060 W. Addison St., Chicago IL, 60613"
 * @param STREET_CITY_COUNTRY
 * @return a JavaFX Point2D object that contains latitude(y) and longitude(x) of given address
 */
public static Point2D geoCode(final String STREET_CITY_COUNTRY) throws UnsupportedEncodingException {
    String URL_STRING = new StringBuilder(GEO_CODE_URL).append(URLEncoder.encode(STREET_CITY_COUNTRY, "UTF-8"))
            .append("&thumbMaps=false").toString();

    StringBuilder response = new StringBuilder();
    try {
        final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection();
        final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
        String inputLine;
        while ((inputLine = IN.readLine()) != null) {
            response.append(inputLine).append("\n");
        }
        IN.close();

        Object obj = JSONValue.parse(response.toString());
        JSONObject jsonObj = (JSONObject) obj;

        JSONArray results = (JSONArray) jsonObj.get("results");
        JSONObject firstResult = (JSONObject) results.get(0);
        JSONArray locations = (JSONArray) firstResult.get("locations");
        JSONObject firstLocation = (JSONObject) locations.get(0);
        JSONObject latLng = (JSONObject) firstLocation.get("latLng");
        return new Point2D(Double.parseDouble(latLng.get("lng").toString()),
                Double.parseDouble(latLng.get("lat").toString()));
    } catch (IOException ex) {
        System.out.println(ex);
        return new Point2D(0, 0);
    }
}

From source file:it.eng.spagobi.studio.core.util.JSONReader.java

public static JSONObject createJSONObject(IFile objSel) {
    logger.debug("IN");
    JSONObject o = null;// w ww .ja  v a2 s.com
    try {
        InputStream inputStream = objSel.getContents();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);

        String line = null;
        StringBuffer stringBuffer = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }

        inputStreamReader.close();
        reader.close();
        inputStream.close();

        String contentString = stringBuffer.toString();

        o = new JSONObject(contentString);

    } catch (Exception e) {
        logger.error("error in reading JSON object", e);
        return null;
    } catch (Throwable e) {
        logger.error("error in reading JSON object", e);
        return null;
    }

    logger.debug("OUT");
    return o;
}

From source file:Main.java

public static String getLocalDns(String dns) {
    Process process = null;//from   w  ww.  j av a2  s .co m
    String str = "";
    BufferedReader reader = null;
    try {
        process = Runtime.getRuntime().exec("getprop net." + dns);
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            str += line;
        }
        reader.close();
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
            process.destroy();
        } catch (Exception e) {
        }
    }
    return str.trim();
}

From source file:Main.java

public static String post(String url, Map<String, String> params) {
    try {/*from  w  ww  .ja  v  a  2  s .c o  m*/
        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        PrintWriter pw = new PrintWriter(connection.getOutputStream());
        StringBuilder sbParams = new StringBuilder();
        if (params != null) {
            for (String key : params.keySet()) {
                sbParams.append(key + "=" + params.get(key) + "&");
            }
        }
        if (sbParams.length() > 0) {
            String strParams = sbParams.substring(0, sbParams.length() - 1);
            Log.e("cat", "strParams:" + strParams);
            pw.write(strParams);
            pw.flush();
            pw.close();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuffer response = new StringBuffer();
        String readLine = "";
        while ((readLine = br.readLine()) != null) {
            response.append(readLine);
        }
        br.close();
        return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}