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.twinsoft.convertigo.engine.util.CarUtils.java

private static void exportXMLProject(String fileName, Document document) throws EngineException {
    try {/*from   w ww .j  a  v a  2  s.c  o m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        boolean isCR = FileUtils.isCRLF();

        Writer writer = isCR ? new StringWriter() : new FileWriterWithEncoding(fileName, "UTF-8");
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        if (isCR) {
            String content = FileUtils.CrlfToLf(writer.toString());
            writer = new FileWriterWithEncoding(fileName, "UTF-8");
            writer.write(content);
        }

        writer.close();
    } catch (Exception e) {
        throw new EngineException("(CarUtils) exportProject failed", e);
    }
}

From source file:chibi.gemmaanalysis.cli.deprecated.ProbeMapperCli.java

/**
 * @param bestOutputFileName/*from  w ww. j  a v a 2  s. c om*/
 * @return
 * @throws IOException
 */
private static Writer getWriterForBestResults(String bestOutputFileName) throws IOException {
    Writer w = null;
    File o = new File(bestOutputFileName);
    w = new BufferedWriter(new FileWriter(o));

    try {
        w.write("");
    } catch (IOException e) {
        throw new RuntimeException("Could not write to " + bestOutputFileName);
    }
    return w;
}

From source file:com.cisco.dvbu.ps.common.scriptutil.ScriptUtil.java

/**
 * Create the Composite Studio Enable VCS property file.  The property file name is of the format <composite_user>.<domain>.<composite_server_host>.properties.
 * @param vcsIncludeResourceSecurity - the value is either "true" or "false"
 * @param vcsWorkspacePathOverride - this is a way of overriding the vcs workspace path.  The vcsWorkspacePath is derived from the studio.properties file properties:
 *                                   VCS_WORKSPACE_DIR+"/"+VCS_PROJECT_ROOT.  It will use the substitute drive by default.
 * @param vcsScriptBinFolderOverride - this is the bin folder name only for PDTool Studio.  Since PDTool Studio can now support multiple hosts via multiple /bin folders,
 *                             it is optional to pass in the /bin folder location.  e.g. bin_host1, bin_host2.  The default will be bin if the input is null or blank.                                 
 * @param studioPropertyName - the name of the PDTool configuration property file.  The default is studio.properties
 * @param PDToolHomeDir - the directory of the PDToolStudio home directory
 * @param propertyFilePath - this is the full path to the output file.  For windows 7 the location will be in %USERPROFILE%\.compositesw
 * /*  w  ww. j  av  a  2 s.  c  o  m*/
 * usage createStudioEnableVCSPropertyFile true studio.properties D/:/CompositeSoftware/CIS6.2.0/conf/studio/PDToolStudio62 C:/Users/mike/.compositesw/admin.composite.localhost.properties
 */
public static void createStudioEnableVCSPropertyFile(String vcsIncludeResourceSecurity,
        String vcsWorkspacePathOverride, String vcsScriptBinFolderOverride, String studioPropertyName,
        String PDToolHomeDir, String propertyFilePath) {

    String prefix = "createStudioEnableVCSPropertyFile";
    // Set the global suppress and debug properties used throughout this class
    String validOptions = "true,false";
    // Get the property from the property file
    String debug = CommonUtils.getFileOrSystemPropertyValue(studioPropertyName, "DEBUG1");
    boolean debug1 = false;
    if (debug != null && validOptions.contains(debug)) {
        debug1 = Boolean.valueOf(debug);
    }

    if (!vcsIncludeResourceSecurity.equals("true") && !vcsIncludeResourceSecurity.equals("false")) {
        throw new ApplicationException(
                "The variable vcsIncludeResourceSecurity may only be set to \"true\" or \"false\"");
    }
    if (vcsWorkspacePathOverride != null)
        vcsWorkspacePathOverride = vcsWorkspacePathOverride.trim();

    if (vcsScriptBinFolderOverride != null)
        vcsScriptBinFolderOverride = vcsScriptBinFolderOverride.trim();

    CommonUtils.writeOutput("", prefix, "-debug1", logger, debug1, false, false);
    CommonUtils.writeOutput("--- INPUT PARAMETERS ----", prefix, "-debug1", logger, debug1, false, false);
    CommonUtils.writeOutput("", prefix, "-debug1", logger, debug1, false, false);
    CommonUtils.writeOutput("    vcsIncludeResourceSecurity=" + vcsIncludeResourceSecurity, prefix, "-debug1",
            logger, debug1, false, false);
    CommonUtils.writeOutput("    vcsWorkspacePathOverride=[" + vcsWorkspacePathOverride + "]  len="
            + vcsWorkspacePathOverride.length(), prefix, "-debug1", logger, debug1, false, false);
    CommonUtils.writeOutput("    vcsScriptFolderOverride=[" + vcsScriptBinFolderOverride + "]  len="
            + vcsScriptBinFolderOverride.length(), prefix, "-debug1", logger, debug1, false, false);
    CommonUtils.writeOutput("    studioPropertyName=" + studioPropertyName, prefix, "-debug1", logger, debug1,
            false, false);
    CommonUtils.writeOutput("    PDToolHomeDir=" + PDToolHomeDir, prefix, "-debug1", logger, debug1, false,
            false);
    CommonUtils.writeOutput("    propertyFilePath=" + propertyFilePath, prefix, "-debug1", logger, debug1,
            false, false);
    CommonUtils.writeOutput("", prefix, "-debug1", logger, debug1, false, false);

    StringBuffer sb = new StringBuffer();
    try {
        Format formatter;
        Date date = new Date();
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String dt = formatter.format(date);

        String VCS_WORKSPACE_DIR = CommonUtils.extractVariable("",
                CommonUtils.getFileOrSystemPropertyValue(studioPropertyName, "VCS_WORKSPACE_DIR"),
                "studio.properties", true);
        File absFile = new File(VCS_WORKSPACE_DIR);
        String VCS_WORKSPACE_DIR_ABS = CommonUtils.setCanonicalPath(absFile.getCanonicalPath());

        String VCS_PROJECT_ROOT = CommonUtils.extractVariable("",
                CommonUtils.getFileOrSystemPropertyValue(studioPropertyName, "VCS_PROJECT_ROOT"),
                "studio.properties", true);

        // Set the workspace path to the workspace override
        String vcsWorkspacePath = vcsWorkspacePathOverride;

        // If the vcs workspace path override is null then derive the path from the studio.properties.
        if (vcsWorkspacePath == null || vcsWorkspacePath.length() == 0) {
            vcsWorkspacePath = (VCS_WORKSPACE_DIR_ABS + "/" + VCS_PROJECT_ROOT);
            System.out.println("VCS_WORKSPACE_DIR=" + VCS_WORKSPACE_DIR_ABS);
            System.out.println("VCS_PROJECT_ROOT=" + VCS_PROJECT_ROOT);
            System.out.println("vcsWorkspacePath=[" + vcsWorkspacePath + "]");
        }

        CommonUtils.writeOutput("--- INTERNAL WORKING PARAMETERS ----", prefix, "-debug1", logger, debug1,
                false, false);
        CommonUtils.writeOutput("", prefix, "-debug1", logger, debug1, false, false);
        CommonUtils.writeOutput("VCS_WORKSPACE_DIR=" + VCS_WORKSPACE_DIR_ABS, prefix, "-debug1", logger, debug1,
                false, false);
        CommonUtils.writeOutput("VCS_PROJECT_ROOT=" + VCS_PROJECT_ROOT, prefix, "-debug1", logger, debug1,
                false, false);
        CommonUtils.writeOutput("pre-mod: vcsWorkspacePath=[" + vcsWorkspacePath + "]", prefix, "-debug1",
                logger, debug1, false, false);

        // Change all backslash to forward slashes to normalize the path
        vcsWorkspacePath = vcsWorkspacePath.replaceAll(Matcher.quoteReplacement("\\\\"),
                Matcher.quoteReplacement("/"));
        //         System.out.println("vcsWorkspacePath="+vcsWorkspacePath);

        // Change all backslash to forward slashes to normalize the path
        vcsWorkspacePath = vcsWorkspacePath.replaceAll(Matcher.quoteReplacement("\\"),
                Matcher.quoteReplacement("/"));
        //         System.out.println("vcsWorkspacePath="+vcsWorkspacePath);

        // Create a double backslash for paths
        vcsWorkspacePath = vcsWorkspacePath.replaceAll(Matcher.quoteReplacement("/"),
                Matcher.quoteReplacement("\\\\"));
        //         System.out.println("vcsWorkspacePath="+vcsWorkspacePath);

        // Add \ in front of the C: so it will be C\:
        if (!vcsWorkspacePath.contains("\\:"))
            vcsWorkspacePath = vcsWorkspacePath.replaceAll(Matcher.quoteReplacement(":"),
                    Matcher.quoteReplacement("\\:"));
        //         System.out.println("vcsWorkspacePath="+vcsWorkspacePath);

        // Change C\\: to C\:
        if (vcsWorkspacePath.contains("\\\\:"))
            vcsWorkspacePath = vcsWorkspacePath.replaceAll(Matcher.quoteReplacement("\\\\:"),
                    Matcher.quoteReplacement("\\:"));
        //         System.out.println("vcsWorkspacePath="+vcsWorkspacePath);

        //         System.out.println("vcsWorkspacePath=["+vcsWorkspacePath+"]");
        CommonUtils.writeOutput("post-mod: vcsWorkspacePath=[" + vcsWorkspacePath + "]", prefix, "-debug1",
                logger, debug1, false, false);

        // If the vcs script bin folder name override is null then derive the path from the default /bin location.
        String vcsScriptFolder = (PDToolHomeDir + "/bin");
        if (vcsScriptBinFolderOverride != null && vcsScriptBinFolderOverride.length() > 0)
            vcsScriptFolder = (PDToolHomeDir + "/" + vcsScriptBinFolderOverride);

        CommonUtils.writeOutput("pre-mod: vcsScriptFolder=[" + vcsScriptFolder + "]", prefix, "-debug1", logger,
                debug1, false, false);
        //         System.out.println("vcsScriptFolder="+vcsScriptFolder);

        // Change all backslash to forward slashes to normalize the path
        vcsScriptFolder = vcsScriptFolder.replaceAll(Matcher.quoteReplacement("\\\\"),
                Matcher.quoteReplacement("/"));
        //         System.out.println("vcsScriptFolder="+vcsScriptFolder);

        // Change all backslash to forward slashes to normalize the path
        vcsScriptFolder = vcsScriptFolder.replaceAll(Matcher.quoteReplacement("\\"),
                Matcher.quoteReplacement("/"));
        //         System.out.println("vcsScriptFolder="+vcsScriptFolder);

        // Create a double backslash for paths
        vcsScriptFolder = vcsScriptFolder.replaceAll(Matcher.quoteReplacement("/"),
                Matcher.quoteReplacement("\\\\"));

        // Add \ in front of the C: so it will be C\:
        if (!vcsScriptFolder.contains("\\:"))
            vcsScriptFolder = vcsScriptFolder.replaceAll(Matcher.quoteReplacement(":"),
                    Matcher.quoteReplacement("\\:"));
        //         System.out.println("vcsScriptFolder="+vcsScriptFolder);

        // Change C\\: to C\:
        if (vcsScriptFolder.contains("\\\\:"))
            vcsScriptFolder = vcsScriptFolder.replaceAll(Matcher.quoteReplacement("\\\\:"),
                    Matcher.quoteReplacement("\\:"));
        //         System.out.println("vcsScriptFolder="+vcsScriptFolder);

        //         System.out.println("vcsScriptFolder=["+vcsScriptFolder+"]");
        CommonUtils.writeOutput("post-mod: vcsScriptFolder=[" + vcsScriptFolder + "]", prefix, "-debug1",
                logger, debug1, false, false);

        sb.append("# Generated by ExecutePDToolStudio.bat\n");
        sb.append("# " + dt + "\n");
        sb.append("vcsIncludeResourceSecurity=" + vcsIncludeResourceSecurity + "\n");
        sb.append("vcsScriptFolder=" + vcsScriptFolder + "\n");
        sb.append("vcsWorkspacePath=" + vcsWorkspacePath + "\n");
        sb.append("enableVCS=true" + "\n");
    } catch (Exception e) {
        throw new ValidationException(e.getMessage(), e);
    }

    try {
        Writer out = new OutputStreamWriter(new FileOutputStream(propertyFilePath));
        out.write(sb.toString());
        out.flush();

        String fileContents = CommonUtils.getFileAsString(propertyFilePath);
        System.out.println("");
        System.out.println("");
        System.out.println(
                "-----------------------------------------------------------------------------------------------------");
        System.out.println("Property File Contents For [" + propertyFilePath + "]");
        System.out.println(
                "-----------------------------------------------------------------------------------------------------");
        System.out.println(fileContents);
        System.out.println("");
        CommonUtils.writeOutput("Property File Contents For [" + propertyFilePath + "]", prefix, "-debug1",
                logger, debug1, false, false);
        CommonUtils.writeOutput(fileContents, prefix, "-debug1", logger, debug1, false, false);

    } catch (FileNotFoundException e) {
        logger.error("Could not wirte to property file " + propertyFilePath, e);
        throw new ValidationException(e.getMessage(), e);
    } catch (IOException e) {
        logger.error("Could not wirte to property file " + propertyFilePath, e);
        throw new ValidationException(e.getMessage(), e);
    }
}

From source file:com.oeg.oops.VocabUtils.java

/**
 * Method to save a document on a path//ww  w.  j ava 2s  .  c  om
 * @param path
 * @param textToWrite
 */
public static void saveDocument(String path, String textToWrite) {
    File f = new File(path);
    Writer out = null;
    try {
        f.createNewFile();
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
        out.write(textToWrite);
        out.close();
    } catch (IOException e) {
        System.err.println("Error while creating the file " + e.getMessage() + "\n" + f.getAbsolutePath());
    }

}

From source file:com.chen.emailcommon.internet.Rfc822Output.java

/**
 * Write the body text.// w ww . ja va  2s .co  m
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer
 * @param out the output stream inside the writer (used for byte[] access)
 * @param bodyText Plain text and HTML versions of the original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String[] bodyText)
        throws IOException {
    boolean html = false;
    String text = bodyText[INDEX_BODY_TEXT];
    if (text == null) {
        text = bodyText[INDEX_BODY_HTML];
        html = true;
    }
    if (text == null) {
        writer.write("\r\n"); // a truly empty message
    } else {
        // first multipart element is the body
        String mimeType = "text/" + (html ? "html" : "plain");
        writeHeader(writer, "Content-Type", mimeType + "; charset=utf-8");
        writeHeader(writer, "Content-Transfer-Encoding", "base64");
        writer.write("\r\n");
        byte[] textBytes = text.getBytes("UTF-8");
        writer.flush();
        out.write(Base64.encode(textBytes, Base64.CRLF));
    }
}

From source file:com.xqdev.jam.MLJAM.java

private static void sendStringResponse(HttpServletResponse res, String s) throws IOException {
    res.setContentType("text/plain; charset=UTF-8");
    Writer w = res.getWriter();
    w.write(s);
    w.flush();//  ww w .  j a va 2  s.c o m
}

From source file:Main.java

private static void addChildren(Writer writer, ResultSet rs) throws SQLException, IOException {
    ResultSetMetaData metaData = rs.getMetaData();
    int nbColumns = metaData.getColumnCount();
    StringBuffer buffer = new StringBuffer();
    while (rs.next()) {
        buffer.setLength(0);/*  ww  w.jav a 2s.  c  o  m*/
        buffer.append("<" + metaData.getTableName(1) + ">");
        for (int i = 1; i <= nbColumns; i++) {
            buffer.append("<" + metaData.getColumnName(i) + ">");
            buffer.append(rs.getString(i));
            buffer.append("</" + metaData.getColumnName(i) + ">");
        }
        buffer.append("</" + metaData.getTableName(1) + ">");
        writer.write(buffer.toString());
    }
}

From source file:com.xqdev.jam.MLJAM.java

private static void sendClientProblemResponse(HttpServletResponse res, String s) throws IOException {
    // Commenting out the status code because we want the client to eval the error() call
    //res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    if (s != null && s.length() > 4096) { // Cap super long errors
        s = s.substring(0, 2048) + " ...[trimmed]... " + s.substring(s.length() - 2048);
    }//from   w  w w  .j a v  a  2 s. com
    res.setContentType("x-marklogic/xquery; charset=UTF-8");
    Writer writer = res.getWriter();
    writer.write("error('" + escapeSingleQuotes(s) + "')");
    writer.flush();
}

From source file:com.xqdev.jam.MLJAM.java

private static void sendServerProblemResponse(HttpServletResponse res, String s) throws IOException {
    // Commenting out the status code because we want the client to eval the error() call
    //res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    res.setContentType("x-marklogic/xquery; charset=UTF-8");
    if (s != null && s.length() > 4096) { // Cap super long errors
        s = s.substring(0, 2048) + " ...[trimmed]... " + s.substring(s.length() - 2048);
    }/*  w w  w .j av  a 2s  .co m*/
    Writer writer = res.getWriter();
    writer.write("error('" + escapeSingleQuotes(s) + "')");
    writer.flush();
}

From source file:com.android.emailcommon.internet.Rfc822Output.java

/**
 * Write the body text. If only one version of the body is specified (either plain text
 * or HTML), the text is written directly. Otherwise, the plain text and HTML bodies
 * are both written with the appropriate headers.
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer//w w w  .  j a  v  a  2s  .c o  m
 * @param out the output stream inside the writer (used for byte[] access)
 * @param bodyText Plain text and HTML versions of the original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String[] bodyText)
        throws IOException {
    String text = bodyText[INDEX_BODY_TEXT];
    String html = bodyText[INDEX_BODY_HTML];

    if (text == null) {
        writer.write("\r\n"); // a truly empty message
    } else {
        String multipartBoundary = null;
        boolean multipart = html != null;

        // Simplified case for no multipart - just emit text and be done.
        if (multipart) {
            // continue with multipart headers, then into multipart body
            multipartBoundary = getNextBoundary();

            writeHeader(writer, "Content-Type",
                    "multipart/alternative; boundary=\"" + multipartBoundary + "\"");
            // Finish headers and prepare for body section(s)
            writer.write("\r\n");
            writeBoundary(writer, multipartBoundary, false);
        }

        // first multipart element is the body
        writeHeader(writer, "Content-Type", "text/plain; charset=utf-8");
        writeHeader(writer, "Content-Transfer-Encoding", "base64");
        writer.write("\r\n");
        byte[] textBytes = text.getBytes("UTF-8");
        writer.flush();
        out.write(Base64.encode(textBytes, Base64.CRLF));

        if (multipart) {
            // next multipart section
            writeBoundary(writer, multipartBoundary, false);

            writeHeader(writer, "Content-Type", "text/html; charset=utf-8");
            writeHeader(writer, "Content-Transfer-Encoding", "base64");
            writer.write("\r\n");
            byte[] htmlBytes = html.getBytes("UTF-8");
            writer.flush();
            out.write(Base64.encode(htmlBytes, Base64.CRLF));

            // end of multipart section
            writeBoundary(writer, multipartBoundary, true);
        }
    }
}