Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

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

Prototype

public OutputStreamWriter(OutputStream out) 

Source Link

Document

Creates an OutputStreamWriter that uses the default character encoding.

Usage

From source file:Main.java

/**
 * Print a string representation of the current stack state of all the active threads.
 *///w  ww .  ja v  a2  s  .c om
public static void printStackTraces() {
    printStackTraces(new PrintWriter(new OutputStreamWriter(System.err)));
}

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);/*from ww w  . j  a va 2s  .  com*/
    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 void write(Element root, OutputStream os) throws Exception {
    Writer writer = new OutputStreamWriter(os);
    write(root, writer);/*from www  .ja v a  2 s . c  om*/
}

From source file:Main.java

public static boolean write(String filename, Document document, boolean addDocType) {
    try {//w w w .  j a  va 2s.co  m
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        if (addDocType) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                    "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN");
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                    "http://java.sun.com/dtd/facelet-taglib_1_0.dtd");
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source,
                new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)))));
        return true;
    } catch (Exception e) {
        return false;
    }

}

From source file:boxConnection.SerBoxTelnet.java

private static void createTelnetSession(String command) throws IOException, InterruptedException {
    telnet.connect(ControlMain.getActiveBox().getDboxIp());
    OutputStream ostream = telnet.getOutputStream();
    Writer writer = new OutputStreamWriter(ostream);
    writer.write(ControlMain.getActiveBox().getLogin() + "\n");
    writer.flush();/*from   w w  w.j av a  2  s . c o  m*/
    Thread.sleep(1000);
    writer.write(ControlMain.getActiveBox().getPassword() + "\n");
    writer.flush();
    Thread.sleep(1000);
    writer.write(command + "\n");
    writer.flush();
    closeTelnetSession();
}

From source file:Main.java

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

    File file = null;/*from   w  ww.j ava  2s.c  o m*/
    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:LoginClient.java

public LoginClient() {
    try {// ww  w .ja va2  s.c o  m
        SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", 7070);
        PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        String userName = "MyName";
        output.println(userName);
        String password = "MyPass";
        output.println(password);
        output.flush();
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String response = input.readLine();
        System.out.println(response);

        output.close();
        input.close();
        socket.close();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        System.exit(0);
    }
}

From source file:cc.vileda.sipgatesync.api.SipgateApi.java

public static String getToken(final String username, final String password) {
    try {/*from w ww .  j  a va 2s  . c o  m*/
        final HttpURLConnection urlConnection = getConnection("/authorization/token");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        JSONObject request = new JSONObject();
        request.put("username", username);
        request.put("password", password);
        OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
        Log.d("SipgateApi", request.getString("username"));
        wr.write(request.toString());
        wr.flush();
        StringBuilder sb = new StringBuilder();
        int HttpResult = urlConnection.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            br.close();
            Log.d("SipgateApi", "" + sb.toString());
            final JSONObject response = new JSONObject(sb.toString());
            return response.getString("token");
        } else {
            System.out.println(urlConnection.getResponseMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.siemens.sw360.exporter.CSVExport.java

@NotNull
private static ByteArrayOutputStream getCSVOutputStream(Iterable<String> csvHeaderIterable,
        Iterable<Iterable<String>> inputIterable) throws IOException {
    final ByteArrayOutputStream outB = new ByteArrayOutputStream();
    try (Writer out = new BufferedWriter(new OutputStreamWriter(outB));) {
        CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat);
        csvPrinter.printRecord(csvHeaderIterable);
        csvPrinter.printRecords(inputIterable);
        csvPrinter.flush();/*w w w.j  a va 2 s. c  o  m*/
        csvPrinter.close();
    } catch (Exception e) {
        outB.close();
        throw e;
    }

    return outB;

}

From source file:cz.pichlik.goodsentiment.MockDataGenerator.java

private static void generateFile(File file, LocalDate date) throws IOException {
    CSVFormat format = CSVFormat/*from  ww  w  . ja va2  s. c  o  m*/
            .newFormat(',').withRecordSeparator("\r\n").withQuote('"').withHeader("id", "sentimentCode",
                    "orgUnit", "latitude", "longitude", "city", "gender", "yearsInCompany", "timestamp")
            .withNullString("");

    CSVPrinter printer = null;
    try (OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file))) {
        printer = new CSVPrinter(output, format);
        for (int i = 0; i < 150 + rg().nextInt(100); i++) {
            long id = sequence++;
            int sentimentCode = generateSentiment();
            String orgUnit = generateOrgUnit();
            Object geo[] = generateGeo();
            Object latitude = geo[0];
            Object longitude = geo[1];
            Object city = geo[2];
            String gender = generateGender();
            int daysInCompany = generateYearsInCompany();
            LocalDateTime timestamp = generateTimestamp(date);
            printer.printRecord(id, sentimentCode, orgUnit, latitude, longitude, city, gender, daysInCompany,
                    timestamp);
        }
    } finally {
        printer.close();
    }
}