Example usage for java.io Writer write

List of usage examples for java.io Writer write

Introduction

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

Prototype

public void write(String str) throws IOException 

Source Link

Document

Writes a string.

Usage

From source file:ilearnrw.utils.ServerHelperClass.java

private static void writeToFile(String outfilename, String data) throws Exception {
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfilename), "UTF-8"));
    try {//from ww  w .ja v a  2 s  .c om
        out.write(data);
    } finally {
        out.close();
    }
}

From source file:Main.java

public static void write(byte[] data, Writer output, String encoding) throws IOException {
    if (data != null)
        output.write(new String(data, encoding));
}

From source file:Dengue.CDengueManager.java

private static void sendInfoToCPU(String pStrJSON, int pIntPort) {

    new Thread(() -> {
        try (Socket client = new Socket(InetAddress.getLocalHost(), pIntPort)) {

            Writer objWriter = Channels.newWriter(Channels.newChannel(client.getOutputStream()),
                    StandardCharsets.US_ASCII.name());
            objWriter.write(pStrJSON);
            objWriter.flush();/*from   w w w.j  a  v a  2s.c  o  m*/

            client.shutdownOutput();

            try (Reader objReader = Channels.newReader(Channels.newChannel(client.getInputStream()),
                    StandardCharsets.US_ASCII.name());
                    BufferedReader objOutReader = new BufferedReader(objReader)) {
                System.out.println((char) objOutReader.read());

            }

        } catch (IOException e) {
            System.out.println(e);
        }
    }).start();
}

From source file:Base64.java

private static void writeChars(File file, char[] data) {
    try {/* w w  w  . ja v a  2s.  c om*/
        Writer fos = new FileWriter(file);
        Writer os = new BufferedWriter(fos);
        os.write(data);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.solr.client.solrj.util.ClientUtils.java

public static void writeXML(SolrInputDocument doc, Writer writer) throws IOException {
    writer.write("<doc boost=\"" + doc.getDocumentBoost() + "\">");

    for (SolrInputField field : doc) {
        float boost = field.getBoost();
        String name = field.getName();
        for (Object v : field) {
            if (v instanceof Date) {
                v = DateUtil.getThreadLocalDateFormat().format((Date) v);
            } else if (v instanceof byte[]) {
                byte[] bytes = (byte[]) v;
                v = Base64.byteArrayToBase64(bytes, 0, bytes.length);
            } else if (v instanceof ByteBuffer) {
                ByteBuffer bytes = (ByteBuffer) v;
                v = Base64.byteArrayToBase64(bytes.array(), bytes.position(), bytes.limit() - bytes.position());
            }/*from   w w  w.  ja  v a 2s.  c  o m*/

            if (boost != 1.0f) {
                XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost);
            } else if (v != null) {
                XML.writeXML(writer, "field", v.toString(), "name", name);
            }

            // only write the boost for the first multi-valued field
            // otherwise, the used boost is the product of all the boost values
            boost = 1.0f;
        }
    }
    writer.write("</doc>");
}

From source file:com.redhat.rhn.common.util.FileUtils.java

/**
 * Save a String to a file on disk using specified path.
 *
 * WARNING:  This deletes the original file before it writes.
 *
 * @param contents to save to file on disk
 * @param path to save file to./* w w w  .j ava  2s.c o m*/
 */
public static void writeStringToFile(String contents, String path) {
    try {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        Writer output = new BufferedWriter(new FileWriter(file));
        try {
            output.write(contents);
        } finally {
            output.close();
        }
    } catch (Exception e) {
        log.error("Error trying to write file to disk: [" + path + "]", e);
        throw new RuntimeException(e);
    }
}

From source file:me.timothy.ddd.DDDUtils.java

private static void write(Writer out, Object o, int indentation) throws IOException {
    if (o == null) {
        out.write("null");
    } else if (o instanceof Map<?, ?>)
        writeJSONPretty(out, (Map<?, ?>) o, indentation);
    else if (o instanceof Collection<?>)
        writeJSONPretty(out, (Collection<?>) o, indentation);
    else if (o instanceof Number) {
        out.write(o.toString());//from w w  w .  ja v a 2 s.  c  o m
    } else if (o instanceof String) {
        out.write("\"");
        out.write(JSONValue.escape((String) o));
        out.write("\"");
    } else if (o instanceof Boolean) {
        out.write(o == Boolean.TRUE ? "true" : "false");
    } else {
        throw new IllegalArgumentException("How do I write " + o.getClass().getName() + "?");
    }
}

From source file:Main.java

/**
 * Writes a string XML encoded to a writer.
 *//*  w ww.  j av a 2s  .co m*/
public static void writeEncoded(Writer out, String str) throws IOException {
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        switch (ch) {
        case '<':
            out.write("&lt;");
            break;
        case '>':
            out.write("&gt;");
            break;
        case '&':
            out.write("&amp;");
            break;
        case '"':
            out.write("&quot;");
            break;
        case '\'':
            out.write("&apos;");
            break;
        case '\r':
        case '\n':
            out.write(ch);
            break;
        default:
            if (((int) ch < 32) || ((int) ch > 126)) {
                out.write("&#x");
                out.write(Integer.toString((int) ch, 16));
                out.write(';');
            } else {
                out.write(ch);
            }
        }
    }
}

From source file:FileCopyUtils.java

/**
 * Copy the contents of the given String to the given output Writer.
 * Closes the write when done./*  ww w. ja  va 2 s .c  om*/
 * @param in the String to copy from
 * @param out the Writer to copy to
 * @throws IOException in case of I/O errors
 */
public static void copy(String in, Writer out) throws IOException {

    try {
        out.write(in);
    } finally {
        try {
            out.close();
        } catch (IOException ex) {
            System.out.println("Could not close Writer" + ex);
        }
    }
}

From source file:com.nextep.designer.beng.model.impl.FileUtils.java

/**
 * Writes the specified contents to a file at the given file location.
 * // w  w  w.j a v a2  s . c o  m
 * @param fileName name of the file to create (will be replaced if exists)
 * @param contents contents to write to the file.
 */
public static void writeToFile(String fileName, String contents) {
    // Retrieves the encoding specified in the preferences
    String encoding = SQLGenUtil.getPreference(PreferenceConstants.SQL_SCRIPT_ENCODING);
    final boolean convert = SQLGenUtil.getPreferenceBool(PreferenceConstants.SQL_SCRIPT_NEWLINE_CONVERT);
    if (convert) {
        String newline = CorePlugin.getService(IGenerationService.class).getNewLine();
        // Converting everything to \n then \n to expected new line
        contents = contents.replace("\r\n", "\n");
        contents = contents.replace("\r", "\n");
        contents = contents.replace("\n", newline);
    }
    Writer w = null;
    try {
        w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), encoding));
        w.write(contents);
    } catch (FileNotFoundException e) {
        throw new ErrorException(e);
    } catch (IOException e) {
        throw new ErrorException(e);
    } finally {
        if (w != null) {
            try {
                w.close();
            } catch (IOException e) {
                throw new ErrorException(e);
            }
        }
    }
}