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:org.esigate.HttpErrorPage.java

private static HttpEntity toMemoryEntity(Exception exception) {
    StringBuilderWriter out = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE);
    PrintWriter pw = new PrintWriter(out);
    exception.printStackTrace(pw);//from   w  w w . j av a  2s .  c  o  m
    String content = out.toString();
    try {
        return toMemoryEntity(content);
    } finally {
        pw.close();
    }
}

From source file:Main.java

/**
 * Saves an index (Hashtable) to a file.
 * /*from   w ww.  j a v  a  2s .  co  m*/
 * @param root_
 *            The file where the index is stored in.
 * @param index
 *            The indextable.
 * @exception IOException
 *                If an internal error prevents the file from being written.
 */
public static void saveIndex(File root_, String name, Hashtable index) throws IOException {
    File indexfile = new File(root_, name);
    PrintWriter out = new PrintWriter(new FileWriter(indexfile));
    Enumeration keys = index.keys();
    String key = null;
    while (keys.hasMoreElements()) {
        key = (String) keys.nextElement();
        out.println(key);
        out.println((Long) index.get(key));
    }
    out.close();
}

From source file:forge.util.FileUtil.java

public static void writeFile(File file, String text) {
    try {//from ww w  .ja  v  a 2s. co  m
        PrintWriter p = new PrintWriter(file);
        p.print(text);
        p.close();
    } catch (final Exception ex) {
        throw new RuntimeException("FileUtil : writeFile() error, problem writing file - " + file + " : " + ex);
    }
}

From source file:com.quanticate.opensource.pdftkbox.PDFtkBox.java

protected static void doExport(String pdf, String bookmarks, String[] args) throws IOException {
    Bookmarks bm = new Bookmarks(new File(pdf));

    File bmf = null;/*w  ww.  ja va2s  . c o m*/
    if (bookmarks != null) {
        bmf = new File(bookmarks);
    } else if (args.length > 0) {
        bmf = new File(args[0]);
    }

    PrintWriter output;
    if (bmf == null) {
        output = new PrintWriter(System.out);
    } else {
        output = new PrintWriter(bmf, "UTF-8");
    }

    bm.exportBookmarks(output);
    output.close();
    bm.close();
}

From source file:com.ikanow.aleph2.data_model.utils.ProcessUtils.java

/**
 * Writes pid=date out to /app/aleph2/pid_manager/bucket._id/application_name
 * // ww  w  .  j  a va 2  s  . com
 * @param application_name
 * @param bucket
 * @param aleph_root_path
 * @param pid
 * @param date
 * @throws IOException 
 */
private static void storePid(final String application_name, final DataBucketBean bucket,
        final String aleph_root_path, final String pid, final long date) throws IOException {
    final File file = new File(
            aleph_root_path + PID_MANAGER_DIR_NAME + bucket._id() + File.separator + application_name);
    file.getParentFile().mkdirs();
    if (file.exists())
        file.delete();
    file.createNewFile();
    final PrintWriter pw = new PrintWriter(file);
    pw.print(pid + "=" + date);
    pw.close();
}

From source file:Main.java

public static void saveToFile(File folder, String name, String data) {

    File file = new File(folder, name);

    PrintWriter pw = null;
    try {//from  ww w.j av a 2  s .com
        Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        pw = new PrintWriter(w);
        pw.write(data);

    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
}

From source file:de.wdilab.coma.gui.extensions.RemoteRepo.java

public static boolean getRemoteConnection(String s, UUID uid) {

    UUID uuid;//from   www . j a  v a  2s.co m
    String xslt_text;
    boolean is_stored = false;

    xslt_text = s.replaceAll("\"", "\\\"");
    xslt_text = xslt_text.replaceAll("\'", "\\\'");
    uuid = uid;

    try {
        File f = new File("helpingFile.csv");
        PrintWriter output = new PrintWriter(new FileWriter(f));
        output.println("uuid,data");
        xslt_text.trim();
        xslt_text = xslt_text.replace("\n", "").replace("\r", "");
        output.println(uuid + "," + xslt_text);
        output.close();

        HttpClient client = new DefaultHttpClient();
        FileEntity entity = new FileEntity(f, ContentType.create("text/plain", "UTF-8"));

        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        HttpResponse response = client.execute(httppost);
        //            System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200)
            is_stored = true;
        f.delete();
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    //        System.out.println("xslt stored   "+is_stored);

    return is_stored;

}

From source file:jeplus.util.LineEnds.java

/**
 * Replace line ends in the (text) file with the given string. This function creates a temporary file then delete and rename.
 * @param file//  ww  w .  jav a  2s  . c o  m
 * @param newLN 
 */
protected static void convertFile(File file, String newLN) {
    try {
        BufferedReader fr = new BufferedReader(new FileReader(file));
        File tempfile = new File(file.getAbsolutePath() + ".temp");
        PrintWriter fw = new PrintWriter(new FileWriter(tempfile));
        String line = fr.readLine();
        while (line != null) {
            fw.print(line);
            fw.print(newLN);
            line = fr.readLine();
        }
        fr.close();
        fw.close();
        file.delete();
        if (!tempfile.renameTo(file)) {
            throw new Exception("Cannot rename " + tempfile.getName() + " to " + file.getName());
        }
    } catch (Exception ex) {
        logger.error("Error converting file " + file.getAbsolutePath(), ex);
    }
}

From source file:com.sunway.cbm.util.ServletUtils.java

/**
 * ?xml?//from   www  .  j av  a 2  s  .c om
 * 
 * @param response
 * @param object
 */
public static void responseXml(HttpServletResponse response, String xml) {
    try {
        //         response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        logger.debug(xml);
        //         response.getWriter().write(xml);
        //         response.getWriter().flush();
        //         response.getWriter().close();
        PrintWriter out = response.getWriter();
        out.print(xml);
        out.close();
    } catch (IOException e) {
        logger.debug(e.toString());
    }
}

From source file:edu.brown.benchmark.voteresper.EPRuntimeUtil.java

public static void writeToFile(String toWrite) {
    try {// w  w  w . jav a  2 s.  com
        if (!VoterConstants.PRINT_TO_CONSOLE) {
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new FileWriter(VoterConstants.OUT_FILE, true)));
            out.println(toWrite);
            out.flush();
            out.close();
        } else {
            System.out.println(toWrite);
        }
    }

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}