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, CharsetEncoder enc) 

Source Link

Document

Creates an OutputStreamWriter that uses the given charset encoder.

Usage

From source file:Main.java

public static void writeXML(Document d, OutputStream os, String sysID) throws IOException {
    /**/*  ww  w .  j a v  a  2 s.  c  o  m*/
     * To support i18n, we have to specify the encoding of
     * output writer to UTF-8 when we writing the XML.
     */
    // PrintWriter out=new PrintWriter(os);
    PrintWriter out = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.println();
    if (sysID != null) {
        out.println("<!DOCTYPE " + d.getDoctype().getName() + " SYSTEM \"" + sysID + "\">");
        out.println();
    }
    //d.getDocumentElement().normalize();
    writeXMLwalkTree(d.getDocumentElement(), 0, out);
    out.flush();
}

From source file:Main.java

public static void writeFile(String content, String path) {
    FileOutputStream fos = null;//from  w  w w  .  jav a2 s . co  m
    BufferedWriter bw = null;
    try {
        File file = new File(path);
        fos = new FileOutputStream(file);
        bw = new BufferedWriter(new OutputStreamWriter(fos, "GB2312"));
        bw.write(content);
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (fos != null)
                fos.close();
        } catch (IOException ie) {
        }
    }
}

From source file:Main.java

public static void writeStr(OutputStream out, String str, String charset) throws IOException {
    if (TextUtils.isEmpty(charset))
        charset = "UTF-8";

    Writer writer = new OutputStreamWriter(out, charset);
    writer.write(str);//from w w w.j a v  a  2  s  .  c o m
    writer.flush();
}

From source file:Main.java

public static void writeFile(String path, String content, boolean append) throws FileNotFoundException {
    //File file = new File(path);
    FileOutputStream writerStream = new FileOutputStream(path, append);
    BufferedWriter writer = null;
    try {//from  w  w  w .  j  av a  2  s . c o  m
        writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
        writer.write(content);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
            }
        }
    }
}

From source file:Main.java

public static void toStream(final String str, final OutputStream stream, final Charset charset)
        throws IOException {
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset));
    try {//w  w w  . j  a va2s  .com
        writer.write(str);
    } finally {
        writer.flush();
        writer.close();
    }
}

From source file:Main.java

private static void saveStr(String path, String xml, boolean isAppend) {
    OutputStream out = null;//from   w ww  . j ava 2  s  .  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:Main.java

public static void documentToFile(Document doc, File file, Charset charset) throws IOException {
    StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), charset));
    DOMSource source = new DOMSource(doc);
    try {//from ww  w .  j  a  va2s  .  com
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void Converter(String input, String output) {
    try {/*w w  w . j av  a 2  s  .  c o m*/
        FileInputStream fis = new FileInputStream(new File(input));
        BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

        FileOutputStream fos = new FileOutputStream(new File(output));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

        int len = 80;
        char buf[] = new char[len];

        int numRead;
        while ((numRead = in.read(buf, 0, len)) != -1)
            out.write(buf, 0, numRead);

        out.close();
        in.close();
    } catch (IOException e) {
        System.out.println("An I/O Exception Occurred: " + e);
    }
}

From source file:Main.java

public static void print(Document doc) {
    try {// w  w w .  ja v  a  2s  . c om
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "utf-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.bstek.dorado.idesupport.StandaloneRuleSetExporter.java

public static void main(String[] args) throws Exception {
    String ruleSetFile = null;/*from  ww  w.j  a v  a2s .  c om*/
    String doradoHome = null;
    if (args.length >= 2) {
        ruleSetFile = args[0];
        doradoHome = args[1];
    } else {
        throw new IllegalArgumentException();
    }

    if (StringUtils.isEmpty(doradoHome)) {
        doradoHome = System.getenv("DORADO_HOME");
    }

    StandaloneRuleSetExporter instance = new StandaloneRuleSetExporter(doradoHome);

    FileOutputStream fos = new FileOutputStream(ruleSetFile);
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, Constants.DEFAULT_CHARSET));
    try {
        instance.exportRuleSet(writer);
    } finally {
        writer.flush();
        writer.close();
        fos.close();
    }
}