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:duty_scheduler.Scheduler.java

/**
 * Prints the results of the algorithm to a file
 * //from   w  w  w.  j  a  v a2  s .c om
 * @param best The best Schedule found during the run
 * @param runTimeReport A String describing the runtime of the algorithm
 */
private static void printResults(Schedule best, String runTimeReport) {
    String resultsFile = "schedule_" + (new SimpleDateFormat("MM-dd-yyyy-hh:mm")).format(new Date()) + ".sched";
    PrintWriter dataOut = null;
    try {
        dataOut = new PrintWriter(resultsFile);
        dataOut.println(runTimeReport);
        dataOut.println("Duty Assignments:\n\n");
        dataOut.println(best.toString());
    } catch (IOException e) {
        ErrorChecker.printExceptionToLog(e);
    } finally {
        dataOut.close();
    }
}

From source file:IO.java

public static void writeData(int[][] data, int nData, int nDim, String filepath) {
    File file = new File(filepath);
    PrintWriter writer;

    try {/*  w w w  .j  a v  a  2 s  .c om*/

        writer = new PrintWriter(file);
        for (int i = 0; i < nData; i++) {
            for (int j = 0; j < nDim; j++) {
                if (j == nDim - 1) {
                    writer.print(data[i][j]);
                } else {
                    writer.print(data[i][j] + ",");
                }
            }
            writer.println();
        }
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:IO.java

public static void writeData(double[][] data, int nData, int nDim, String filepath) {
    File file = new File(filepath);
    PrintWriter writer;

    try {//from www  . j  a  v  a  2 s  .c  om

        writer = new PrintWriter(file);
        for (int i = 0; i < nData; i++) {
            for (int j = 0; j < nDim; j++) {
                if (j == nDim - 1) {
                    writer.print(data[i][j]);
                } else {
                    writer.print(data[i][j] + ",");
                }

            }
            writer.println();
        }
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:edu.ucsb.nceas.metacat.util.RequestUtil.java

public static String get(String urlString, Hashtable<String, String[]> params) throws MetacatUtilException {
    try {/*from  w  ww .  j av  a 2 s .c om*/
        URL url = new URL(urlString);
        URLConnection urlConn = url.openConnection();

        urlConn.setDoOutput(true);

        PrintWriter pw = new PrintWriter(urlConn.getOutputStream());
        String queryString = paramsToQuery(params);
        logMetacat.debug("Sending get request: " + urlString + "?" + queryString);
        pw.print(queryString);
        pw.close();

        // get the input from the request
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();

        return sb.toString();
    } catch (MalformedURLException mue) {
        throw new MetacatUtilException("URL error when contacting: " + urlString + " : " + mue.getMessage());
    } catch (IOException ioe) {
        throw new MetacatUtilException("I/O error when contacting: " + urlString + " : " + ioe.getMessage());
    }
}

From source file:com.google.api.GoogleAPI.java

/**
 * Forms an HTTP request, sends it using POST method and returns the result of the request as a JSONObject.
 * //from  w  w w .  j  a va 2s  .  c  o m
 * @param url The URL to query for a JSONObject.
 * @param parameters Additional POST parameters
 * @return The translated String.
 * @throws Exception on error.
 */
protected static JSONObject retrieveJSON(final URL url, final String parameters) throws Exception {
    try {
        final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setRequestProperty("referer", referrer);
        uc.setRequestMethod("POST");
        uc.setDoOutput(true);

        final PrintWriter pw = new PrintWriter(uc.getOutputStream());
        pw.write(parameters);
        pw.flush();

        try {
            final String result = inputStreamToString(uc.getInputStream());

            return new JSONObject(result);
        } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
            uc.getInputStream().close();
            if (uc.getErrorStream() != null) {
                uc.getErrorStream().close();
            }
            pw.close();
        }
    } catch (Exception ex) {
        throw new Exception("[google-api-translate-java] Error retrieving translation.", ex);
    }
}

From source file:at.wada811.dayscounter.CrashExceptionHandler.java

public static void makeReportFile(Context context, Throwable throwable) {
    PrintWriter writer = null;
    FileOutputStream outputStream;
    try {//ww  w.j  a  v a2  s .  c  o  m
        outputStream = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
        writer = new PrintWriter(outputStream);
        String report = CrashExceptionHandler.makeReport(context, throwable);
        writer.print(report);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:barrysw19.calculon.icc.ICCInterface.java

private static void saveChat(String s) {
    try {/*from w ww.  ja v a  2 s.co  m*/
        PrintWriter pw = new PrintWriter(new FileWriter("c:/Development/chatlog.txt", true));
        pw.println(s);
        pw.close();
    } catch (IOException e) {
        LOG.error("Error writing chatlog", e);
    }
}

From source file:com.glaf.core.util.IOUtils.java

/**
 * write lines.// w ww .  j  av a 2  s .com
 * 
 * @param os
 *            output stream.
 * @param lines
 *            lines.
 * @throws IOException
 */
public static void writeLines(OutputStream os, String[] lines) throws IOException {
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
    try {
        for (String line : lines)
            writer.println(line);
        writer.flush();
    } finally {
        writer.close();
    }
}

From source file:com.example.admin.processingboilerplate.JsonIO.java

public static JSONObject pushJson(String requestURL, String jsonDataName, JSONObject json) {
    try {//from w w  w.  jav a  2  s. c  o  m
        String boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        //HttpURLConnection con = (HttpURLConnection) url.openConnection();
        URLConnection uc = (url).openConnection();
        HttpURLConnection con = requestURL.startsWith("https") ? (HttpsURLConnection) uc
                : (HttpURLConnection) uc;
        con.setUseCaches(false);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        con.setRequestProperty("User-Agent", USER_AGENT);
        OutputStream outputStream = con.getOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);

        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"" + "data" + "\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF);
        writer.append(json.toString()).append(CRLF);
        writer.flush();

        writer.append(CRLF).flush();
        writer.append("--" + boundary + "--").append(CRLF);
        writer.close();
        int status = con.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            StringBuilder sb = load(con);
            try {
                json = new JSONObject(sb.toString());
                return json;
            } catch (JSONException e) {
                Log.e("loadJson", e.getMessage(), e);
                e.printStackTrace();
                return new JSONObject();
            }
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new JSONObject();
}

From source file:com.tamingtext.tagging.LuceneTagExtractor.java

public static void emitTextForTags(File file, File output) throws IOException {
    String field = "tag";

    Directory dir = FSDirectory.open(file);
    IndexReader reader = IndexReader.open(dir, true);
    TermEnum te = reader.terms(new Term(field, ""));
    StringBuilder buf = new StringBuilder();
    do {//from ww w  .ja v  a 2s  .c om
        Term term = te.term();
        if (term == null || term.field().equals(field) == false) {
            break;
        }

        if (te.docFreq() > 30) {
            File f = new File(output, term.text() + ".txt");
            PrintWriter pw = new PrintWriter(new FileWriter(f));
            System.err.printf("%s %d\n", term.text(), te.docFreq());

            TermDocs td = reader.termDocs(term);
            while (td.next()) {
                int doc = td.doc();
                buf.setLength(0);
                appendVectorTerms(buf, reader.getTermFreqVector(doc, "description-clustering"));
                appendVectorTerms(buf, reader.getTermFreqVector(doc, "extended-clustering"));
                emitTagDoc(term, pw, buf);
            }

            pw.close();
        }
    } while (te.next());
    te.close();
}