Example usage for java.io FileWriter write

List of usage examples for java.io FileWriter write

Introduction

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

Prototype

public void write(char cbuf[], int off, int len) throws IOException 

Source Link

Document

Writes a portion of an array of characters.

Usage

From source file:Main.java

public static void putString(File file, String contents) throws IOException {
    FileWriter output = new FileWriter(file);
    output.write(contents, 0, contents.length());
    output.close();// ww  w.  j  av  a 2  s  . c o  m
}

From source file:Main.java

/**
 * write a string To a File/*from   w ww.  java 2 s  .c  o  m*/
 * 
 * @param context
 * @param file
 * @param string
 * @param isAppend
 * @return
 */
public static boolean writeStringToFile(File file, String string, boolean isAppend) {
    boolean isWriteOk = false;

    if (null == file || null == string) {
        return isWriteOk;
    }

    FileWriter fw = null;
    try {
        fw = new FileWriter(file, isAppend);

        fw.write(string, 0, string.length());
        fw.flush();
        isWriteOk = true;
    } catch (Exception e) {
        isWriteOk = false;
        e.printStackTrace();
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
                isWriteOk = false;
                e.printStackTrace();
            }
        }
    }
    return isWriteOk;
}

From source file:it.geosolutions.geoserver.jms.impl.handlers.DocumentFile.java

/**
 * //from  w  w  w  .j  a v a 2 s . com
 * @param file
 * @param xml
 * @throws JDOMException
 * @throws IOException
 */
protected static void writer(File file, String xml) throws JDOMException, IOException {

    FileWriter writer = null;
    StringReader reader = null;
    try {
        writer = new FileWriter(file);
        reader = new StringReader(xml);

        char[] cbuf = new char[2048];
        int size = 0;
        while (reader.ready() && (size = reader.read(cbuf)) != -1) {
            writer.write(cbuf, 0, size);
        }

    } finally {
        writer.flush();
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(reader);
    }
}

From source file:Main.java

private static void _writeOutput(String outputString, FileWriter fileWriter) throws IOException {
    if (_exportDTD) {
        // XSLT discards the DTD decalration.
        // The following code inserts the specified DTD decalrations.
        // Write the following into the filewriter first:
        // <?xml version="1.0" encoding="UTF-8"?>,
        // which appears just before the comment, <!--
        int positionToInsertDTD = outputString.indexOf("<!--");
        fileWriter.write(outputString, 0, positionToInsertDTD);
        // FIXME: So far, only MoML DTD can be exported. If the support
        // of more DTDs is necessray, modify the main() method and
        // setExportDTD() method to allow configuration of DTD.
        fileWriter.write("\r\n<!DOCTYPE entity PUBLIC \"-//UC Berkeley//DTD MoML 1//EN\" "
                + "\r\n\"http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd\">");
        fileWriter.write(outputString.substring(positionToInsertDTD));
    } else {/*from w  ww. j a v a2 s  .c o  m*/
        fileWriter.write(outputString);
    }
}

From source file:com.moss.appsnap.keeper.freedesktop.FreeDesktopAppHandler.java

public static void writeLaunchScript(boolean isKeeper, String launchCommand, File scriptLocation,
        final PosixFifoSocketAddress fifoSocketServer) throws IOException {
    FileWriter writer = new FileWriter(scriptLocation);
    writer.append("#!" + SHELL + "\n\n");

    if (!isKeeper) {
        {// WRITE THE FIFO SOCKETS FUNCTION
            Reader functionReader = new InputStreamReader(PosixFifoMisc.sendFifoSocketMessageBashFunction());
            char[] b = new char[1024];

            for (int x = functionReader.read(b); x != -1; x = functionReader.read(b)) {
                writer.write(b, 0, x);
            }//from   w w w  .j a v  a2s .  c om
        }

        {
            // WRITE THE POLLING CALL TO THE KEEPER
            final char EOL = '\n';
            writer.append(EOL + "CONTROL=" + fifoSocketServer.controlPipe().getAbsolutePath() + "\n" + "DIR="
                    + fifoSocketServer.socketsDir().getAbsolutePath() + EOL + "" + EOL
                    + "xmessage \"Checking for updates...\" -buttons \"\" -center -title \"Please Wait...\" &"
                    + EOL + "DIALOG=$!" + EOL + "" + EOL
                    + "RESPONSE=`echo -n \"LAUNCH_POLL\" | sendFifoSocketMessage $CONTROL $DIR`" + EOL + ""
                    + EOL + "echo \"RESPONSE: $RESPONSE\"" + EOL + "" + EOL + "kill $DIALOG" + EOL + "" + EOL
                    + "if [ \"$RESPONSE\" != \"OK\" ] " + EOL + "then" + EOL
                    + "   xmessage -center -buttons OK -default OK  \"Polling error: $RESPONSE\"" + EOL
                    + "   exit 1;" + EOL + "fi" + EOL + "" + EOL);
        }
        writer.append(launchCommand);
    } else {
        writer.append("while [ 1 ]; do \n");
        writer.append(launchCommand);
        writer.append('\n');
        writer.append("RESULT=$?\n");
        writer.append("if [ $RESULT != 0 ]; then echo \"Error: keeper quit with return $RESULT\"; exit; fi\n");
        writer.append("done");
    }

    writer.flush();
    writer.close();

}

From source file:com.amazonaws.mturk.cmd.MakeTemplate.java

private void copyTemplateFile(String sourceRoot, String targetRoot, String extension) throws Exception {
    String inputFileName = sourceRoot + extension;
    String outputFileName = targetRoot + extension;

    System.out.println("Copying resource file: " + outputFileName);

    File inputFile = new File(inputFileName);
    if (!inputFile.exists() || !inputFile.canRead()) {
        throw new Exception("Could not read from the file " + inputFileName);
    }/*from   w  w  w .  j a  v a2  s.  co m*/

    File outputFile = new File(outputFileName);
    if (!outputFile.exists()) {
        if (!outputFile.createNewFile() || !outputFile.canWrite())
            throw new Exception("Could not write to the file " + outputFileName);
    }

    // copy file
    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);

    try {
        char[] buffer = new char[1024];
        int nread = 0;
        while ((nread = in.read(buffer)) != -1) {
            out.write(buffer, 0, nread);
        }
    } finally {
        in.close();
        out.close();
    }
}

From source file:com.apkcategorychecker.writer.WriterCSV.java

private void removeBlankLines(String _csvPath) {
    try {/*from  w ww. ja  v  a  2s . c om*/
        FileReader fr = new FileReader(_csvPath);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(_csvPath + ".temp");
        String line;

        while ((line = br.readLine()) != null) {
            //line = line.trim(); // remove leading and trailing whitespace
            if (line.length() != 0) // don't write out blank lines
            {
                fw.write(line, 0, line.length());
                fw.append("\r");
            }
        }
        fr.close();
        fw.close();
        File old = new File(_csvPath);
        old.delete();
        File clean = new File(_csvPath + ".temp");
        clean.renameTo(old);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}