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:Main.java

private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException {
    if (node == null) {
        return false;
    }//from   w ww. ja  v a2s  .  co  m
    short type = node.getNodeType();
    boolean result = true;
    switch (type) {
    case Node.ATTRIBUTE_NODE: {
        String text = ((Attr) node).getValue();
        writer.write(text);
        break;
    }
    case Node.TEXT_NODE: {
        String text = ((Text) node).getData();
        writer.write(text);
        break;
    }
    case Node.ELEMENT_NODE: {
        Element element = (Element) node;
        if (includeNode) {
            serializeXML(element, writer, false);
        } else {
            Node child = element.getFirstChild();
            while (child != null) {
                serializeXmlNode(child, writer, true);
                child = child.getNextSibling();
            }
        }
        break;
    }
    case Node.DOCUMENT_NODE: {
        Document doc = (Document) node;
        serializeXmlNode(doc.getDocumentElement(), writer, includeNode);
        break;
    }
    default:
        result = false;
        break;
    }
    return result;
}

From source file:com.feilong.commons.core.io.IOWriteUtil.java

/**
 * .//from  w  w w  . ja va 2  s . c om
 * 
 * <ul>
 * <li>?,, (?? )</li>
 * <li>,?  {@link FileWriteMode#APPEND}??</li>
 * </ul>
 *
 * @param filePath
 *            
 * @param content
 *            
 * @param encode
 *            ?,isNullOrEmpty, {@link CharsetType#GBK}? {@link CharsetType}
 * @param fileWriteMode
 *            ?
 * @throws UncheckedIOException
 *             the unchecked io exception
 * @throws IllegalArgumentException
 *             <ul>
 *             <li>filePath,isDirectory</li>
 *             <li>filePath,!canWrite</li>
 *             </ul>
 * @see FileWriteMode
 * @see CharsetType
 * @see com.feilong.commons.core.io.FileUtil#cascadeMkdirs(String)
 * @see java.io.FileOutputStream#FileOutputStream(File, boolean)
 */
public static void write(String filePath, String content, String encode, FileWriteMode fileWriteMode)
        throws UncheckedIOException, IllegalArgumentException {

    if (Validator.isNullOrEmpty(encode)) {
        encode = CharsetType.GBK;
    }

    // **************************************************************************8
    File file = FileUtil.cascadeMkdirs(filePath);

    boolean append = (fileWriteMode == FileWriteMode.APPEND);

    try {
        OutputStream outputStream = new FileOutputStream(file, append);
        Writer outputStreamWriter = new OutputStreamWriter(outputStream, encode);

        Writer writer = new PrintWriter(outputStreamWriter);
        writer.write(content);
        writer.close();

        if (log.isInfoEnabled()) {
            log.info("fileWriteMode:[{}],contentLength:[{}],encode:[{}],fileSize:[{}],absolutePath:[{}]",
                    fileWriteMode, content.length(), encode, FileUtil.getFileFormatSize(file),
                    file.getAbsolutePath());
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:Main.java

/**
 * Appends str to <code>out</code> while performing HTML attribute escaping.
 * /*ww w  .  j av a2  s  .  c  o m*/
 * @param out
 * @param str
 * @throws IOException
 */
public static void escapeHTMLAttribute(Writer out, String str) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz;
    sz = str.length();
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);

        switch (ch) {
        case '&':
            out.write("&amp;");
            break;
        case '"':
            out.write("&quot;");
            break;
        case '\t':
            out.write("&#9;");
            break;
        case '\n':
            out.write("&#10;");
            break;
        case '\r':
            out.write("&#13;");
            break;
        case '<':
            out.write("&lt;");
            break;
        case '>':
            out.write("&gt;");
            break;
        default:
            out.write(ch);
            break;
        }

    }
}

From source file:Main.java

/**
 * Writes {@link String} {@code s} to the {@link Writer} {@code out} replacing special XML tokens by their respective XML entities.
 * /*  ww w . ja v a2  s .  c  o  m*/
 * @param out
 *            The {@link Writer} to write to.
 * @param s
 *            The raw {@link String}.
 * 
 * @throws IOException
 */
public static void writeXmlEntityEncodedString(Writer out, String s) throws IOException {
    if (s == null || s.length() == 0) {
        return;
    }

    int pos;
    int start = 0;

    // scan for special characters
    while ((pos = minIndexOfOneOf(s, start, XML_SPECIAL_CHARS)) >= 0) {
        // write everything up to the special character
        out.write(s.substring(start, pos));

        // write the XML entity
        switch (s.charAt(pos)) {
        case '"':
            out.write(XML_ENTITY_QUOT);
            break;
        case '&':
            out.write(XML_ENTITY_AMP);
            break;
        case '\'':
            out.write(XML_ENTITY_APOS);
            break;
        case '<':
            out.write(XML_ENTITY_LT);
            break;
        case '>':
            out.write(XML_ENTITY_GT);
            break;
        }
        // skip the special character
        start = pos + 1;
    }
    // write everything that's left
    out.write(s.substring(start));
}

From source file:com.hbs.common.josn.JSONUtil.java

/**
 * Serializes an object into JSON to the given writer, excluding any properties matching
 * any of the regular expressions in the given collection.
 * @param writer Writer to serialize the object to
 * @param object object to be serialized
 * @param excludeProperties Patterns matching properties to ignore
 * @throws IOException//w  w w . j ava2s.  c om
 * @throws JSONException
 */
public static void serialize(Writer writer, Object object, Collection<Pattern> excludeProperties,
        Collection<Pattern> includeProperties) throws IOException, JSONException {
    writer.write(serialize(object, excludeProperties, includeProperties, true));
}

From source file:com.netspective.sparx.util.HttpUtils.java

public static void includeUrlContent(String spec, Writer writer) throws IOException {
    URL url = new URL(spec);
    URLConnection urlConn = url.openConnection();
    InputStream urlIn = urlConn.getInputStream();
    int iRead = urlIn.read();
    while (iRead != -1) {
        writer.write((char) iRead);
        iRead = urlIn.read();/*from   w  ww. ja  va2s .co m*/
    }
}

From source file:de.uzk.hki.da.sb.Utilities.java

/**
 * Writes the given text string into a file
 * // ww w  . java  2 s .  c  om
 * @param output File The file to write into
 * @param text The text to write into the file
 * @throws Exception
 */
public static void writeFile(File outputFile, String text) throws Exception {

    Writer writer;
    try {
        writer = new FileWriter(outputFile);
    } catch (IOException e) {
        throw new Exception("Couldn't create writer", e);
    }

    try {
        writer.write(text);
        writer.close();

    } catch (IOException e) {
        throw new Exception("Couldn't write to file " + outputFile.getAbsolutePath(), e);
    }
}

From source file:org.jboss.as.test.integration.security.loginmodules.common.Utils.java

public static File createTempPropFile(Map<String, String> props) {
    try {/*w  ww. j  a  v a  2s .  c  om*/
        File propsFile = File.createTempFile("props", ".properties");
        propsFile.deleteOnExit();
        Writer writer = new FileWriter(propsFile);
        for (Map.Entry<String, String> entry : props.entrySet()) {
            writer.write(entry.getKey() + "=" + entry.getValue() + "\n");
        }
        writer.close();
        return propsFile;
    } catch (IOException e) {
        throw new RuntimeException("Temporary file could not be created or writen to!", e);
    }
}

From source file:org.jboss.as.test.integration.security.loginmodules.common.Utils.java

public static void setPropertiesFile(Map<String, String> props, URL propFile) {
    File userPropsFile = new File(propFile.getFile());
    try {/*from w w w.java  2  s .  c  o  m*/
        Writer writer = new FileWriter(userPropsFile);
        for (Map.Entry<String, String> entry : props.entrySet()) {
            writer.write(entry.getKey() + "=" + entry.getValue() + "\n");
        }
        writer.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.bstek.dorado.view.output.OutputUtils.java

/**
 * Java?JavaScript//from www .ja va 2  s  . c om
 * 
 * @param writer
 *            Writer
 * @param owner
 *            JavaScript?
 * @param object
 *            Java
 * @param property
 *            ???
 * @param escapeValue
 *            Java?
 * @throws Exception
 * @see #DEFAULT_VALUE
 */
public static void outputProperty(Writer writer, String owner, Object object, String property,
        Object escapeValue) throws Exception {
    Object value = PropertyUtils.getProperty(object, property);
    if (value == escapeValue || (escapeValue != null && escapeValue.equals(value))) {
        return;
    }

    writer.write(owner);
    writer.write('.');
    writer.write(property);
    writer.write('=');

    if (value == null) {
        writer.write("null");
    } else if (value instanceof String) {
        writer.write("\"");
        writer.write((String) value);
        writer.write("\"");
    } else if (value instanceof Number || value instanceof Boolean) {
        writer.write(value.toString());
    } else if (value instanceof Date) {
        writer.write("new Date(");
        writer.write(String.valueOf(((Date) value).getTime()));
        writer.write(")");
    } else {
        writer.write("\"");
        writer.write(value.toString());
        writer.write("\"");
    }
    writer.write(";\n");
}