Example usage for java.io OutputStreamWriter close

List of usage examples for java.io OutputStreamWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:ext.services.xml.XMLUtils.java

/**
 * Writes an object to a file as xml./*from w  w  w . j  a  v a2 s.  co  m*/
 * 
 * @param object Object that should be writen to a xml file
 * @param filename filename
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeObjectToFile(Object object, String filename) throws IOException {
    OutputStreamWriter outputStreamWriter = null;
    try {
        outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8");
        xStream.toXML(object, outputStreamWriter);
    } finally {
        if (outputStreamWriter != null) {
            outputStreamWriter.flush();
            outputStreamWriter.close();
        }
    }
}

From source file:nl.spellenclubeindhoven.dominionshuffle.DataReader.java

public static boolean writeStringToFile(Context context, String filename, String content) {
    OutputStreamWriter out = null;
    try {//  w w  w  .  j  a v a2  s .  co  m
        out = new OutputStreamWriter(context.openFileOutput(filename, Activity.MODE_PRIVATE));
        out.write(content);
    } catch (FileNotFoundException ignore) {
        return false;
    } catch (IOException ignore) {
        return false;
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException ignore) {
            return false;
        }
    }

    return true;
}

From source file:de.domjos.schooltools.helper.Helper.java

public static boolean writeStringToFile(String data, String path, Context context) {
    try {//  w w w .j  a v  a  2 s . c o  m
        path = path.replace(" ", "%20");
        String[] spl = path.split("/");
        String fileName = spl[spl.length - 1];

        File directory = new File(path.replace(fileName, ""));
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                return false;
            }
        }

        File newFile = new File(path);
        if (newFile.exists()) {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(newFile),
                    Charset.defaultCharset());
            outputStreamWriter.write(data);
            outputStreamWriter.close();
            return true;
        } else {
            if (newFile.createNewFile()) {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(newFile),
                        Charset.defaultCharset());
                outputStreamWriter.write(data);
                outputStreamWriter.close();
                return true;
            }
        }
    } catch (Exception ex) {
        Helper.printException(context, ex);
    }
    return false;
}

From source file:com.beyondb.geocoding.BaiduAPI.java

public static Map<String, String> testPost(String x, String y) throws IOException {
    URL url = new URL("http://api.map.baidu.com/geocoder?" + ak + "="
            + "&callback=renderReverse&location=" + x + "," + y + "&output=json");
    URLConnection connection = url.openConnection();
    /**/*  w w  w  . ja v a 2s  . c om*/
     * ??URLConnection?Web
     * URLConnection???Web???
     */
    connection.setDoOutput(true);
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
    //        remember to clean up
    out.flush();
    out.close();
    //        ?????
    String res;
    InputStream l_urlStream;
    l_urlStream = connection.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
    StringBuilder sb = new StringBuilder("");
    while ((res = in.readLine()) != null) {
        sb.append(res.trim());
    }
    String str = sb.toString();
    System.out.println(str);
    Map<String, String> map = null;
    if (StringUtils.isNotEmpty(str)) {
        int addStart = str.indexOf("formatted_address\":");
        int addEnd = str.indexOf("\",\"business");
        if (addStart > 0 && addEnd > 0) {
            String address = str.substring(addStart + 20, addEnd);
            map = new HashMap<String, String>();
            map.put("address", address);
            return map;
        }
    }

    return null;

}

From source file:com.dynamobi.db.conn.couchdb.CouchUdx.java

/**
 * Creates a CouchDB view//ww  w.  j a v a 2s.c om
 */
public static void makeView(String user, String pw, String url, String viewDef) throws SQLException {
    try {
        URL u = new URL(url);
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        uc.setDoOutput(true);
        if (user != null && user.length() > 0) {
            uc.setRequestProperty("Authorization", "Basic " + buildAuthHeader(user, pw));
        }
        uc.setRequestProperty("Content-Type", "application/json");
        uc.setRequestMethod("PUT");
        OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
        wr.write(viewDef);
        wr.close();

        String s = readStringFromConnection(uc);
    } catch (MalformedURLException e) {
        throw new SQLException("Bad URL.");
    } catch (IOException e) {
        throw new SQLException(e.getMessage());
    }
}

From source file:Main.java

private static void saveStr(String path, String xml, boolean isAppend) {
    OutputStream out = null;//from   ww w  .  j a  v  a2s .  c o m
    OutputStreamWriter outwriter = null;
    try {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        out = new FileOutputStream(path, isAppend);
        outwriter = new OutputStreamWriter(out, "UTF-8");
        outwriter.write(xml);
        outwriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outwriter != null) {
                outwriter.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.example.cmput301.model.WebService.java

/**
 * Sets up http connection to the web server and returns the connection.
 * @param conn URLConnection/*from   ww w  .j ava 2s  .c o  m*/
 * @param data string to send to web service.
 * @return String response from web service.
 * @throws IOException
 */
private static String getHttpResponse(URLConnection conn, String data) throws IOException {
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    wr.close();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line, httpResponse = "";
    while ((line = rd.readLine()) != null) {
        // Process line...
        httpResponse += line;
    }
    rd.close();
    return httpResponse;
}

From source file:Main.java

public static File writeToSDFromInput(String path, String fileName, String data) {

    File file = null;// w w w  . j av  a 2 s .c  om
    OutputStreamWriter outputWriter = null;
    OutputStream outputStream = null;
    try {
        creatSDDir(path);
        file = createFileInSDCard(fileName, path);
        outputStream = new FileOutputStream(file, false);
        outputWriter = new OutputStreamWriter(outputStream);
        outputWriter.write(data);
        outputWriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return file;
}

From source file:Main.java

public static Uri getAllContacts(ContentResolver cr, Uri internal, Context context, String timeStamp) {

    String[] contactsArray = new String[2];
    Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    Cursor cur = cr.query(contactsUri, null, null, null, null);

    FileOutputStream fOut = null;
    try {/*from w  ww.ja v a 2  s .c o m*/
        fOut = context.openFileOutput("contacts_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        contactsArray[0] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                .toString();
        contactsArray[1] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

        writeToOutputStreamArray(contactsArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:com.beyondb.geocoding.BaiduAPI.java

public static String getPointByAddress(String address, String city) throws IOException {
    String resultPoint = "";
    try {//  w  ww  .  java2  s  .  com
        URL url = new URL("http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&output=xml&address=" + address
                + "&" + city);

        URLConnection connection = url.openConnection();
        /**
         * ??URLConnection?Web
         * URLConnection???Web???
         */
        connection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        //        remember to clean up
        out.flush();
        out.close();
        //        ?????

        String res;
        InputStream l_urlStream;
        l_urlStream = connection.getInputStream();
        if (l_urlStream != null) {
            BufferedReader in = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();
            System.out.println(str);
            Document doc = DocumentHelper.parseText(str); // XML
            Element rootElt = doc.getRootElement(); // ?
            //            System.out.println("" + rootElt.getName()); // ??
            Element resultElem = rootElt.element("result");
            if (resultElem.hasContent()) {
                Element locationElem = resultElem.element("location");
                Element latElem = locationElem.element("lat");
                //            System.out.print("lat:"+latElem.getTextTrim()+",");
                Element lngElem = locationElem.element("lng");
                //            System.out.println("lng:"+lngElem.getTextTrim());
                resultPoint = lngElem.getTextTrim() + "," + latElem.getTextTrim();
            } else {
                System.out.println("can't compute the coor");
                resultPoint = " , ";
            }
        } else {
            resultPoint = " , ";
        }

    } catch (DocumentException ex) {
        Logger.getLogger(BaiduAPI.class.getName()).log(Level.SEVERE, null, ex);
    }
    return resultPoint;
}