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:com.freedomotic.persistence.FreedomXStream.java

public static boolean toXML(Object object, File file) {
    XStream serializer = getXstream();//from  w  w w. jav  a  2s . c  om
    OutputStream outputStream = null;
    Writer writer = null;

    try {
        outputStream = new FileOutputStream(file);
        writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
        serializer.toXML(object, writer);
    } catch (Exception exp) {
        LOG.error("Error while serializing instance to disk", exp);
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(outputStream);
    }

    return true;
}

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

private static void writeIndentation(Writer out, int indent) throws IOException {
    if (indent >= INDENTS.length) {
        while (indent >= INDENTS.length) {
            indent -= INDENTS.length - 1;
            out.write(INDENTS[INDENTS.length - 1]);
        }/*  w  w w  .jav  a 2  s .c  o m*/
    }
    out.write(INDENTS[indent]);
}

From source file:com.schnobosoft.semeval.cortical.SemEvalTextSimilarity.java

/**
 * Save the values for the metrics using all measures defined in {@link Measure}. All values
 * are scaled to the range [0,5].//from   w w w  .j a  va  2 s  .  com
 *
 * @param metrics   a list of {@link Metric}s
 * @param inputFile the input file, used for specifying the output files
 * @throws IOException
 */
private static void saveScores(Metric[] metrics, File inputFile, Retina retinaName) throws IOException {
    for (Measure measure : Measure.values()) {
        File outputFile = getOutputFile(inputFile, measure, retinaName);
        Writer writer = new BufferedWriter(new FileWriter(outputFile));

        List<Double> scores = Util.scale(Util.getScores(metrics, measure), measure);

        LOG.info("Writing output for '" + inputFile + "'.");
        for (Double score : scores) {
            writer.write(String.valueOf(score) + "\n");
        }
        writer.close();
    }
}

From source file:bs.ws1.dm.itg.App.java

private static void writeToFile(String output, String filename) {
    Writer writer = null;
    try {/*from w w w  . j av a 2  s.c  o  m*/
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + ".csv"), "utf-8"));
        writer.write(output);
    } catch (IOException ex) {
        // report
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}

From source file:com.discovery.darchrow.io.IOWriteUtil.java

/**
 * .//from  www . jav a 2 s  .com
 * 
 * <ul>
 * <li>?,, (?? )</li>
 * <li>,?  {@link FileWriteMode#APPEND}??</li>
 * </ul>
 *
 * @param filePath
 *            
 * @param content
 *            
 * @param encode
 *            ?,isNullOrEmpty, {@link CharsetType#GBK}? {@link CharsetType}
 * @param fileWriteMode
 *            ?
 * @see FileWriteMode
 * @see CharsetType
 * @see java.io.FileOutputStream#FileOutputStream(File, boolean)
 */
public static void write(String filePath, String content, String encode, FileWriteMode fileWriteMode) {

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

    // **************************************************************************8
    File file = new File(filePath);

    FileUtil.createDirectoryByFilePath(filePath);

    OutputStream outputStream = null;
    try {
        outputStream = FileUtil.getFileOutputStream(filePath, fileWriteMode);
        //TODO ? write(inputStream, outputStream);

        Writer outputStreamWriter = new OutputStreamWriter(outputStream, encode);

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

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

From source file:com.flexive.faces.javascript.menu.DojoMenuWriter.java

/**
 * Render the JSON representation for the given menu.
 *
 * @param menuName          name of the JS variable where the menu widget is stored
 * @param menuClass         widget class of the menu widget, e.g. TreeContextMenuV3
 * @param itemClass         widget class of the menu items, e.g. TreeMenuItemV3
 * @param container         the menu container
 * @param contextMenuTarget target element if this menu should be attached as a context menu
 * @param showHandler       an optional javascript handler to be called when the menu is opened
 * @throws IOException if an output error occured @param writer    the output writer @param widgetId  the widget ID (set to null to use an auto-generated ID)
 *//*w  w w .j a va 2  s .  c  o m*/
public static void writeMenu(Writer writer, String widgetId, String menuName, String menuClass,
        String itemClass, MenuItemContainer<DojoMenuItemData> container, RelativeUriMapper uriMapper,
        String contextMenuTarget, String showHandler) throws IOException {
    writer.write("var " + menuName + " = flexive.dojo.makeMenu("
            + (widgetId != null ? "'" + FxFormatUtils.escapeForJavaScript(widgetId) + "'" : "null") + ", '"
            + menuClass + "', '" + itemClass + "', ");
    // render menu-items in method call
    DojoMenuWriter menuWriter = new DojoMenuWriter(writer, widgetId, uriMapper);
    for (DojoMenuItemData item : container.getMenuItems()) {
        menuWriter.writeItem(item);
    }
    menuWriter.finishResponse();
    writer.write(", false, "
            + (StringUtils.isNotBlank(contextMenuTarget) ? "'" + contextMenuTarget + "'" : "null") + ");\n");
    // add event subscriptions
    menuWriter.writeEventSubscriptions(writer);
    if (StringUtils.isNotBlank(showHandler)) {
        writer.write(menuName + ".show_ = " + menuName + ".show;\n");
        writer.write(menuName + ".show = " + showHandler + ";\n");
    }
}

From source file:juicebox.tools.utils.juicer.apa.APAUtils.java

public static void saveListText(String filename, List<Double> array) {
    Writer writer = null;
    try {/* www . j  a  va2s  .c o m*/
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));
        for (double val : array) {
            writer.write(val + " ");
        }
        writer.write("\n");
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:edu.ku.brc.specify.tools.FixMetaTags.java

/**
* Change the contents of text file in its entirety, overwriting any
* existing text./*from   w  w  w  .  j a  v  a2s .co m*/
*
* This style of implementation throws all exceptions to the caller.
*
* @param aFile is an existing file which can be written to.
* @throws IllegalArgumentException if param does not comply.
* @throws FileNotFoundException if the file does not exist.
* @throws IOException if problem encountered during write.
*/
static public void setContents(File aFile, String aContents) throws FileNotFoundException, IOException {
    if (aFile == null) {
        throw new IllegalArgumentException("File should not be null.");
    }
    if (!aFile.exists()) {
        throw new FileNotFoundException("File does not exist: " + aFile);
    }
    if (!aFile.isFile()) {
        throw new IllegalArgumentException("Should not be a directory: " + aFile);
    }
    if (!aFile.canWrite()) {
        throw new IllegalArgumentException("File cannot be written: " + aFile);
    }

    //declared here only to make visible to finally clause; generic reference
    Writer output = null;
    try {
        //use buffering
        //FileWriter always assumes default encoding is OK!
        output = new BufferedWriter(new FileWriter(aFile));
        output.write(aContents);
    } finally {
        //flush and close both "output" and its underlying FileWriter
        if (output != null)
            output.close();
    }
}

From source file:com.technofovea.packbsp.packaging.BspZipController.java

public static void writePackingList(Map<String, File> packItems, Writer dest) throws IOException {
    final String newline = "\r\n";
    List<String> keys = new ArrayList<String>();
    keys.addAll(packItems.keySet());//from  w w  w .  ja  v  a  2s.  c o  m
    Collections.sort(keys);
    for (String inner : keys) {
        File outer = packItems.get(inner);

        dest.write(inner);
        dest.write(newline);
        dest.write(outer.getAbsolutePath());
        dest.write(newline);

    }
}

From source file:com.dm.estore.common.utils.FileUtils.java

/**
 * Save text file from classpath to file.
 *
 * @param resource Classpath text resource
 * @param outputFile Output file//from  w w  w . j  a  va  2 s. c o m
 * @param processor Optional line processor
 * @param encoding Text encoding
 * @throws IOException Exception
 */
public static void saveTextFileFromClassPath(String resource, String outputFile, String encoding,
        TextStreamProcessor processor) throws IOException {

    final InputStream inputStream = FileUtils.class.getResourceAsStream(resource);
    final Scanner scanner = new Scanner(inputStream, encoding);
    final Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), encoding);
    try {

        while (scanner.hasNextLine()) {
            final String nextLine = scanner.nextLine();
            out.write(processor != null ? processor.process(nextLine) : nextLine);
            out.write(NEW_LINE_SEP);
        }

    } finally {
        out.close();
        scanner.close();
    }

}