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:de.cgarbs.lib.json.JSONDataModel.java

/**
 * Converts the DataModel to JSON format and writes the JSON data to the
 * specified file./* w w  w  .  j  a v a 2s  . com*/
 *
 * @param model the DataModel to be converted to JSON
 * @param file the File to be written to
 * @throws DataException either IO errors or errors during the JSON conversion
 */
public static void writeToFile(final DataModel model, final File file) throws DataException {
    PrintWriter out = null;
    try {
        out = new PrintWriter(file);
        out.write(convertToJSON(model));
        out.close();
    } catch (FileNotFoundException e) {
        throw wrappedAsDataException(DataException.ERROR.IO_ERROR, e);
    } catch (JSONException e) {
        throw wrappedAsDataException(DataException.ERROR.JSON_CONVERSION_ERROR, e);
    } finally {
        if (out != null) {
            out.close();
        }
    }
    if (out.checkError()) {
        // FIXME: PrintWriter throws no IOExceptions?!  how to find out what happened?
        throw new DataException(DataException.ERROR.IO_ERROR, "error writing to file");
    }
}

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

public static String createObject(MyJSONObject mjo) throws MyRESTException {

    // generate uid for this object
    String uid = UIDGenerator.getUID();
    mjo.setUID(uid);/*from ww w.  j  a  va2 s .  c  o m*/

    // tmp path of data file to store this object. The file will to sent to S3 later.
    String path = "/tmp/" + uid;

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

    // create the new object on 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 create the object " + uid + ".");
    } catch (AmazonClientException ace) {
        throw new MyRESTException("Failed to create the object " + uid + ".");
    }

    return uid;
}

From source file:Main.java

/** 
 * Write the <code>Document</code> in memory out to a standard XML file.
 * @param  doc  the <code>Document</code> object for the XML file to list
 * @param  strFilePath  the XML file to write to
 * @throws custom custom exception if file is not writeable
 * @throws ioe IOException if an error occurs on file creation
 * @throws fnfe FileNotFoundException if PrintWriter constructor fails
 *///  www . j  ava  2  s  .  com
public static void WriteXMLFile2(Document doc, String strFilePath) throws Exception {
    // open file for writing
    File file = new File(strFilePath);

    // ensure that file is writeable
    try {
        file.createNewFile();
        if (!file.canWrite()) {
            throw new Exception("FileNotWriteable");
        }
    } catch (IOException ioe) {
        throw ioe;
    }

    // create a PrintWriter to write the XML file to
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(file);
    } catch (FileNotFoundException fnfe) {
        throw fnfe;
    }

    // write out the serialized form
    pw.print(getXMLListing(doc));
    pw.close();
}

From source file:mp1.include.me.Sabsalon1.java

private static void writeOutput() throws FileNotFoundException, UnsupportedEncodingException {
    System.out.println("\nWriting to file...(outputFile.out)");
    PrintWriter writer = new PrintWriter("outputFile.out", "UTF-8");
    finaloutput.stream().forEach((element) -> {
        writer.println(element);/*from   w w w.j a  va  2 s  .co m*/
    });
    writer.close();
    System.out.println("File write success!");
}

From source file:com.ibm.ids.example.ShowResult.java

public static void writeToVulnerableSink(String str) throws java.io.FileNotFoundException {
    FileOutputStream fos = new FileOutputStream(str);
    PrintWriter writer = new PrintWriter(fos);
    //STATIC SCAN 
    //to remove this vulnerability issue use writer.append rather than writer.write 
    writer.write(str);//  w  ww  .  j av  a 2s.co  m
    //writer.append(str);
    writer.close();
}

From source file:index.IncrementIndex.java

/**
 * id// w  w  w.  j  a v a 2 s .  co  m
 * @param path
 * @param storeId
 * @return 
 */
public static boolean writeStoreId(String path, String storeId) {
    boolean b = false;
    try {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(path);
        PrintWriter out = new PrintWriter(fw);
        out.write(storeId);
        out.close();
        fw.close();
        b = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return b;
}

From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *//*from w  ww . j a  v a2 s .c o m*/
public static boolean sendInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setContentType("text/html");
        response.setHeader("apprisk", reportStatus.name());
        PrintWriter out = response.getWriter();
        out.println(reportText);
        out.flush();
        out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}

From source file:Main.java

public static String toString(Throwable e) {
    StringWriter w = new StringWriter();
    PrintWriter p = new PrintWriter(w);
    p.print(e.getClass().getName() + ": ");
    if (e.getMessage() != null) {
        p.print(e.getMessage() + "\n");
    }/*from  w w  w . ja v a 2  s. c om*/
    p.println();
    try {
        e.printStackTrace(p);
        return w.toString();
    } finally {
        p.close();
    }
}

From source file:axiom.util.Logger.java

public static String getStackTrace(Throwable t) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    t.printStackTrace(writer);//  w  w w  . j a v a 2s  .  com
    writer.close();
    return stringWriter.toString();
}

From source file:com.tamingtext.classifier.bayes.ExtractTrainingData.java

/** Close writers opened by {@link #getWriterForCategory(File, String)} */
protected static void closeWriters() {
    for (PrintWriter p : trainingWriters.values()) {
        p.close();
    }//from  w  w w  .ja va 2s. c om
}