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

/**
 * This method get string and create local file with that string
 * @param st//from   w w  w. ja va 2s.c  om
 * @param fileName
 */
private static void writeFile(String st, String fileName) {
    FileOutputStream fos = null;
    PrintWriter pw = null;

    try {
        File rootDir = new File("/storage/emulated/0/dbSuperZol/");

        fos = new FileOutputStream(new File(rootDir, fileName));
        pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos)));

        pw.println(st);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:MainClass.java

private static void parse(URL url, String encoding) throws IOException {
    ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();
    InputStream in = url.openStream();
    InputStreamReader r = new InputStreamReader(in, encoding);
    HTMLEditorKit.ParserCallback callback = new Outliner(new OutputStreamWriter(System.out));
    parser.parse(r, callback, true);//  ww w . j av  a2 s .  c  o m
}

From source file:Main.java

public static void execute(final String templateDirName, final String templateFileName, final Map param,
        final OutputStream out) throws IOException, TemplateException {

    // create configuration
    Configuration cfg = new Configuration();
    cfg.setDirectoryForTemplateLoading(new File(templateDirName));

    // get template
    Template temp = cfg.getTemplate(templateFileName);

    // execute/*  w  ww . j ava  2  s  .co  m*/
    temp.process(param, new OutputStreamWriter(out));
    // temp.process(new SampleBean(), new OutputStreamWriter(out));

    // flush out
    out.flush();
}

From source file:Main.java

/** Write an XML DOM tree to a file
 *
 * @param doc the document to write/*  w  w  w. j  av a  2  s.  c o m*/
 * @param file the file to write to
 * @throws IOException if a file I/O error occurs
 */
public static void write(Document doc, File file) throws IOException {
    try {
        DOMSource domSource = new DOMSource(doc);
        FileOutputStream stream = new FileOutputStream(file);
        // OutputStreamWriter works around indent bug 6296446 in JDK
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
        StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream));
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(2));
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * @param node//from  w  w  w .j  a v a 2s.c om
 * @throws IOException
 */
public static void serializeNode(Node node) throws IOException {
    if (writer == null)
        writer = new BufferedWriter(new OutputStreamWriter(System.out));

    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        Document doc = (Document) node;
        writer.write("<?xml version=\"");
        writer.write(doc.getXmlVersion());
        writer.write("\" encoding=\"UTF-8\" standalone=\"");
        if (doc.getXmlStandalone())
            writer.write("yes");
        else
            writer.write("no");
        writer.write("\"?>\n");

        NodeList nodes = node.getChildNodes();
        if (nodes != null)
            for (int i = 0; i < nodes.getLength(); i++)
                serializeNode(nodes.item(i));
        break;
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        writer.write("<" + name);
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            writer.write(" " + current.getNodeName() + "=\"");
            print(current.getNodeValue());
            writer.write("\"");
        }
        writer.write(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
            //  writer.write("\n");

            for (int i = 0; i < children.getLength(); i++)
                serializeNode(children.item(i));
            if ((children.item(0) != null)
                    && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
                writer.write("");
        }

        writer.write("</" + name + ">");
        break;
    case Node.TEXT_NODE:
        print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.write("CDATA");
        print(node.getNodeValue());
        writer.write("");
        break;
    case Node.COMMENT_NODE:
        writer.write("<!-- " + node.getNodeValue() + " -->\n");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.write("&" + node.getNodeName() + ";");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        String publicId = docType.getPublicId();
        String systemId = docType.getSystemId();
        String internalSubset = docType.getInternalSubset();
        writer.write("<!DOCTYPE " + docType.getName());
        if (publicId != null)
            writer.write(" PUBLIC \"" + publicId + "\" ");
        else
            writer.write(" SYSTEM ");
        writer.write("\"" + systemId + "\"");
        if (internalSubset != null)
            writer.write(" [" + internalSubset + "]");
        writer.write(">\n");
        break;
    }
    writer.flush();
}

From source file:Main.java

public static void writeUserInfo(String[] info, Context c) throws IOException {
    File f = c.getFileStreamPath(userInfoPath);
    if (!f.exists()) {
        f.createNewFile();/*from   ww w.  ja v  a2s  .com*/
    }

    String output = "";
    for (int n = 0; n < info.length; n++) {
        output += info[n] + eol + separator + eol;
    }
    BufferedWriter out = null;
    out = new BufferedWriter(new OutputStreamWriter(c.openFileOutput(userInfoPath, Context.MODE_PRIVATE)));

    out.write(output);
    out.flush();
    out.close();
    userInfo = info;
}

From source file:Main.java

public synchronized static String writeXmlToString(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (doc != null) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(output);
        writeXml(doc, writer, null);//from   w ww  .j av  a  2s.  co m
        return output.toString();
    }
    return null;
}

From source file:Main.java

public static JSONObject updateRequest(String query) {
    HttpURLConnection connection = null;
    try {//from w  w w.  j a  v  a2  s.c om
        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:edu.usc.qspr.Main.java

/**
 * The main method./* ww  w  .ja  va2s  .  co m*/
 *
 * @param args the arguments
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    double result;
    long start = System.currentTimeMillis();
    //TODO: to be commented
    //      args = new String("-i sample_inputs/5-1-3.qasm -f sample_inputs/fabric.ql -o output -p baseline -s 21 -v").split(" ");
    //      args = new String("-i ../sample_inputs/5-1-3.qasm -f ../sample_inputs/fabric.ql -p mvfb -s 2 -v").split(" ");

    parseInputs(args);

    if (RuntimeConfig.OUTPUT_TO_FILE) {
        outputFile = new PrintWriter(new BufferedWriter(new FileWriter(outputFileAddr, false)), true);
    } else { //writing to stdout
        outputFile = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
    }

    //Prints the current working directory
    if (RuntimeConfig.DEBUG)
        System.out.println("Current directory: " + System.getProperty("user.dir"));

    layout = LayoutParser.LayoutParser(fabricFileAddr);

    qasm = QASMParser.QASMParser(qasmFileAddr);

    eds = new EventDrivenSimulator(layout, qasm.getCommandsList(), outputFile);

    if (placementMethod.compareTo("center") == 0) {
        result = center();
    } else if (placementMethod.compareTo("baseline") == 0) {
        result = baseLine();
    } else if (placementMethod.compareTo("mc") == 0) {
        result = mc(m, false);
    } else { //default is mvfb
        result = mvfb(m);
    }
    outputFile.println("------------------------------------");
    outputFile.println("Execution latency: " + result + " us");
    long end = System.currentTimeMillis();
    outputFile.println("QSPR runtime " + (end - start) + " ms");

    if (RuntimeConfig.OUTPUT_TO_FILE) {
        outputFile.close();
    } else {
        outputFile.flush();
    }

    if (RuntimeConfig.VERBOSE) {
        System.out.println("Done.");
    }
}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    FileOutputStream fOut = null;

    try {//from  www  .j  a va2 s.  c o m
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

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

    return internal;
}