Example usage for java.io PrintWriter close

List of usage examples for java.io PrintWriter close

Introduction

In this page you can find the example usage for java.io PrintWriter close.

Prototype

public void close() 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:com.jredrain.base.utils.CommandUtils.java

public static File createShellFile(String command, String shellFileName) {
    String dirPath = IOUtils.getTempFolderPath();
    File dir = new File(dirPath);
    if (!dir.exists())
        dir.mkdirs();/*w  w  w .j  ava  2s  .  co m*/

    String tempShellFilePath = dirPath + File.separator + shellFileName + ".sh";
    File shellFile = new File(tempShellFilePath);
    try {
        if (!shellFile.exists()) {
            PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tempShellFilePath)));
            out.write("#!/bin/bash\n" + command);
            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return shellFile;
    }
}

From source file:Main.java

public static void putString2XmlFile(String xml, String fileName) {
    PrintWriter out = null;
    try {/* ww  w .j  a  va  2s .c om*/
        out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
        out.write(xml, 0, xml.length());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.freebase.cleanDL.java

private static void outputMentions() throws FileNotFoundException {
    PrintWriter w = new PrintWriter("/Users/Shyam/mention.eval.dl.out");
    for (String doc : mentions.keySet()) {
        for (DocMention tmp : mentions.get(doc))
            w.println(doc + "\t" + tmp.start + "\t" + tmp.end + "\t" + tmp.mention);
    }/*from ww w  . j  a  va  2s .c  o  m*/
    w.close();
}

From source file:io.proleap.cobol.TestGenerator.java

public static void generateTreeFile(final File cobolInputFile, final File outputDirectory) throws IOException {
    final File outputFile = new File(outputDirectory + "/" + cobolInputFile.getName() + TREE_EXTENSION);

    final boolean createdNewFile = outputFile.createNewFile();

    if (createdNewFile) {
        LOG.info("Creating tree file {}.", outputFile);

        final File parentDirectory = cobolInputFile.getParentFile();
        final CobolSourceFormat format = getCobolSourceFormat(parentDirectory);

        final String preProcessedInput = CobolGrammarContext.getInstance().getCobolPreprocessor()
                .process(cobolInputFile, parentDirectory, format);

        final Cobol85Lexer lexer = new Cobol85Lexer(new ANTLRInputStream(preProcessedInput));
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final Cobol85Parser parser = new Cobol85Parser(tokens);
        final StartRuleContext startRule = parser.startRule();
        final String inputFileTree = TreeUtils.toStringTree(startRule, parser);

        final PrintWriter pWriter = new PrintWriter(new FileWriter(outputFile));

        pWriter.write(inputFileTree);//  w ww.java 2s .  c o m
        pWriter.flush();
        pWriter.close();
    }
}

From source file:Main.java

public static void appendOneline(String fileName, String oneline) {
    PrintWriter out = null;
    try {// ww w.  j a v a 2 s  .  c om
        out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)));
        out.print(oneline + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            out.close();
        }
    }

}

From source file:gov.nist.appvet.tool.sigverifier.util.FileUtil.java

public static boolean saveReport(String reportContent, String reportFilePath) {
    PrintWriter out;
    try {/*from   ww w .jav  a  2s.c  o m*/
        out = new PrintWriter(reportFilePath);
        out.println(reportContent);
        out.flush();
        out.close();
        log.debug("Saved " + reportFilePath);
        return true;
    } catch (FileNotFoundException e) {
        log.error(e.toString());
        return false;
    }
}

From source file:org.eclipse.jgit.lfs.server.fs.FileLfsServlet.java

/**
 * Send an error response./*from  ww  w  . j a  v a  2s.  com*/
 *
 * @param rsp
 *            the servlet response
 * @param status
 *            HTTP status code
 * @param message
 *            error message
 * @throws IOException
 *             on failure to send the response
 * @since 4.6
 */
protected static void sendError(HttpServletResponse rsp, int status, String message) throws IOException {
    rsp.setStatus(status);
    PrintWriter writer = rsp.getWriter();
    gson.toJson(new Error(message), writer);
    writer.flush();
    writer.close();
    rsp.flushBuffer();
}

From source file:org.shareok.data.plosdata.PlosUtil.java

public static void createContentFile(String fileName, String content) {
    try {// w w w . j a  va  2  s.  c o  m
        PrintWriter writer = new PrintWriter(fileName, "UTF-8");
        writer.println(content);
        writer.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.dongli.model.MyJSONData.java

public static void updateObject(MyJSONObject mjo) throws MyRESTException {

    String uid = mjo.getUID();/*from   w  w w  . j  a  v  a 2  s .  c o m*/
    MyJSONObject myJSONObject = queryObject(uid);

    String path = "/tmp/" + uid;

    // write the JSON object to a tmp file
    try {
        FileWriter fw = new FileWriter(path, false);
        PrintWriter pw = new PrintWriter(fw);
        pw.println(mjo.toString());
        pw.close();
        fw.close();
    } catch (IOException e) {
        // failed to create the new object
        File nf = new File(path);
        nf.delete();
        throw new MyRESTException("Failed to update the object.");
    }

    // update the new object on Amazon AWS S3 
    try {
        File uploadFile = new File(path);
        MyAWSStorage.getInstance().s3client
                .putObject(new PutObjectRequest(MyConfiguration.getInstance().bucket, uid, uploadFile));
    } catch (AmazonServiceException ase) {
        throw new MyRESTException("Failed to update the object " + uid + ".");
    } catch (AmazonClientException ace) {
        throw new MyRESTException("Failed to qpdate the object " + uid + ".");
    }
}

From source file:com.unresyst.DealRecommender.java

private static String runCommand(String... commands) throws IOException, InterruptedException {
    // generate a script file containg the command to run
    final File scriptFile = new File("/tmp/runcommand.sh");
    PrintWriter w = new PrintWriter(scriptFile);
    w.println("#!/bin/sh");
    for (String command : commands) {
        w.println(command);/*from  w  w w  . j  a  v a 2s.co  m*/
    }
    w.close();

    // make the script executable
    //System.out.println("absolute path: " + scriptFile.getAbsolutePath());
    Process p = Runtime.getRuntime().exec("chmod +x " + scriptFile.getAbsolutePath());
    p.waitFor();

    // execute the script
    p = Runtime.getRuntime().exec(scriptFile.getAbsolutePath());
    p.waitFor();
    BufferedReader stdin = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String toReturn = "";
    String line = "";
    while ((line = stdin.readLine()) != null) {
        toReturn += line + "\n";
    }
    while ((line = stderr.readLine()) != null) {
        toReturn += "err: " + line + "\n";
    }

    scriptFile.delete();
    return toReturn;
}