Example usage for java.io OutputStreamWriter write

List of usage examples for java.io OutputStreamWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:Main.java

public static void writeToFile(String path, String data) {
    try {/*from ww  w.j  a  v  a2  s  . com*/
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

From source file:Main.java

public static void writeToPublicDirectory(String filename, String string, String directory,
        String environmentDirectory) throws Exception {
    File publicDirectory = new File(Environment.getExternalStoragePublicDirectory(environmentDirectory),
            directory);/*  w w w  .jav a2 s .c om*/
    boolean result = publicDirectory.mkdirs();
    File file = new File(publicDirectory, filename);
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
    outputStreamWriter.write(string);
    outputStreamWriter.close();
}

From source file:Main.java

public static synchronized void writeToFile(String rootPath, String filename, String data) {

    File file = new File(rootPath);
    if (!file.exists()) {
        file.mkdirs();/*  ww  w. j a  va  2s .c o m*/

    }

    FileOutputStream fOut = null;
    try {

        File savedfile = new File(rootPath + filename);
        savedfile.createNewFile();//create file if not exists
        fOut = new FileOutputStream(savedfile, true); //append content to the end of file
        OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

        outWriter.write(data);

        fOut.flush();
        outWriter.flush();

        fOut.close();
        outWriter.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:Main.java

public static JSONObject updateRequest(String query) {
    HttpURLConnection connection = null;
    try {//www  .j  a  v a  2 s  . c o  m
        connection = (HttpURLConnection) new URL(url + query).openConnection();
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Accept-Charset", charset);

        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("Resource content");
        out.close();

        statusCode = connection.getResponseCode();
        if (statusCode != 200) {
            return null;
        }

        InputStream response = connection.getInputStream();
        BufferedReader bR = new BufferedReader(new InputStreamReader(response));
        String line = "";

        StringBuilder responseStrBuilder = new StringBuilder();
        while ((line = bR.readLine()) != null) {
            responseStrBuilder.append(line);
        }
        response.close();
        return new JSONObject(responseStrBuilder.toString());

    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return new JSONObject();
}

From source file:io.mapzone.controller.vm.http.ProvisionErrorResponse.java

public static void send(HttpServletResponse response, int status, String msg) {
    try {/*from w ww .jav  a2 s .  c  o m*/
        response.setContentType("text/html");
        OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), Charset.forName("UTF-8"));
        out.write("<html>\n");
        out.write("<h1>" + msg + "</h1>\n");
        out.write("</html>");
        out.flush();
        response.setStatus(status);
    } catch (IOException e) {
        log.warn("", e);
    }
}

From source file:Main.java

public static File writeLog(String root, String filename) {
    StringBuilder log = new StringBuilder();
    try {/*from  w  w  w . j  a  v a  2  s. c  om*/
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            log.append(line).append("\n");
        }
    } catch (IOException e) {
        log.append(e.toString());
    }

    File file = new File(root, filename);

    try {
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        // Write the string to the file
        osw.write(log.toString());
        osw.flush();
        osw.close();

    } catch (Exception e) {
        Log.e(TAG, String.format("Failed to write log to [%s]", file), e);
    }
    return file;
}

From source file:Main.java

public static OutputStreamWriter newNovelWriter(String filepath, String encoding) throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filepath), encoding);
    if (encoding.equals("UTF-16LE")) { // inject BOM
        writer.write("\uFEFF");
    }//from   ww  w .  ja  va 2  s  .  co  m

    return writer;
}

From source file:ee.ria.xroad.common.util.PasswordStore.java

private static byte[] charToByte(char[] buffer) throws IOException {
    if (buffer == null) {
        return null;
    }/*from w w  w.j ava2  s . c  o m*/

    ByteArrayOutputStream os = new ByteArrayOutputStream(buffer.length * 2);
    OutputStreamWriter writer = new OutputStreamWriter(os, UTF_8);
    writer.write(buffer);
    writer.close();
    return os.toByteArray();
}

From source file:Main.java

/**
 * Uses http to post an XML String to a URL.
 *
 * @param url/*w w w .ja va  2  s.c o  m*/
 * @param xmlString
 * @param return
 * @throws IOException
 * @throws UnknownHostException
 */
public static HttpURLConnection post(String url, String xmlString) throws IOException, UnknownHostException {

    // open connection
    URL urlObject = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
    connection.setDoOutput(true);

    OutputStreamWriter outStream = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    outStream.write(xmlString);
    outStream.close();

    return connection;
}

From source file:lapispaste.Main.java

private static void paster(Map<String, String> pastemap, StringBuffer pdata) {
    try {//w  w w .  j av a2 s. com
        URL url = new URL("http://paste.linux-sevenler.org/index.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // set connection 'writeable'
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        // construct the data...
        String data;
        String pdataString = pdata.toString();
        data = URLEncoder.encode("language", "ISO-8859-9") + "="
                + URLEncoder.encode(pastemap.get("format"), "ISO-8859-9");
        data += "&" + URLEncoder.encode("source", "ISO-8859-9") + "="
                + URLEncoder.encode(pdataString, "ISO-8859-9");
        data += "&" + URLEncoder.encode("submit", "ISO-8859-9") + "="
                + URLEncoder.encode(" Kaydet ", "ISO-8859-9");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // get new url where the paste is
        conn.getInputStream();
        System.out.println(conn.getURL());
        conn.disconnect();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}