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.jaeksoft.searchlib.util.Timer.java

final public void setError(Exception exception) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    exception.printStackTrace(pw);/*  w w  w  .j av a2  s  .  com*/
    pw.close();
    try {
        sw.close();
    } catch (IOException e) {
        Logging.warn(e.getMessage(), e);
    }
    this.error = sw.toString();
}

From source file:com.netscape.cmstools.user.UserCertShowCLI.java

public void execute(String[] args) throws Exception {
    // Always check for "--help" prior to parsing
    if (Arrays.asList(args).contains("--help")) {
        printHelp();/*from  w  w  w .j av  a2s . c o  m*/
        return;
    }

    CommandLine cmd = parser.parse(options, args);

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length != 2) {
        throw new Exception("Incorrect number of arguments specified.");
    }

    boolean showPrettyPrint = cmd.hasOption("pretty");
    boolean showEncoded = cmd.hasOption("encoded");

    String userID = cmdArgs[0];
    String certID = cmdArgs[1];
    String file = cmd.getOptionValue("output");

    UserClient userClient = userCertCLI.getUserClient();
    UserCertData userCertData = userClient.getUserCert(userID, URLEncoder.encode(certID, "UTF-8"));

    String encoded = userCertData.getEncoded();
    if (encoded != null && file != null) {
        // store cert to file
        PrintWriter out = new PrintWriter(new FileWriter(file));
        out.print(encoded);
        out.close();
    }

    MainCLI.printMessage("Certificate \"" + userCertData.getID() + "\"");

    UserCertCLI.printCert(userCertData, showPrettyPrint, showEncoded);
}

From source file:eu.artist.methodology.mpt.cheatsheet.actions.DownloadMATReportAction.java

@Override
public void run(String[] params, ICheatSheetManager arg1) {

    //String url = "http://54.196.142.179:8080/ArtistEva/webresources/question/getmpt";
    InputStream in = null;//  w  w  w .  j  a va  2 s . c  om

    try {

        URL url = new URL(
                "http://54.196.142.179:8080/ArtistEva/webresources/question/getmpt?login=yosu&params={%22ts%22:1412763746529}&hash=e4ecd6a297c01e9cca5a0da0e018a5de9f9ddfa6bd3e13b727df85ba542d11af74c32326320c65ca2de3441b488a09c9f6634ca1f83575be015504dd4168eac0");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");

        connection.setRequestProperty("Accept", "application/xml");

        String output;

        String strXml;

        StringBuilder builder = new StringBuilder();

        BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

        while ((output = br.readLine()) != null) {

            builder.append(output);

        }

        strXml = builder.toString();

        System.out.println("MAT report:" + strXml);

        PrintWriter out = new PrintWriter("MAT_report.txt");

        out.println(strXml);

        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:gwtupload.server.UploadServlet.java

/**
 * Writes a response to the client./*from   w w  w  .ja v  a2 s.c o  m*/
 */
protected static void renderMessage(HttpServletResponse response, String message, String contentType)
        throws IOException {
    response.addHeader("Cache-Control", "no-cache");
    response.setContentType(contentType + "; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(message);
    out.flush();
    out.close();
}

From source file:net.bashtech.geobot.BotManager.java

public static String putRemoteData(String urlString, String postData) throws IOException {

    URL url;//from  w  w w . j  a  va  2  s . c o m
    HttpURLConnection conn;

    try {
        url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("PUT");

        conn.setFixedLengthStreamingMode(postData.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/vnd.twitchtv.v2+json");
        conn.setRequestProperty("Authorization", "OAuth " + BotManager.getInstance().krakenOAuthToken);
        conn.setRequestProperty("Client-ID", BotManager.getInstance().krakenClientID);
        // conn.setConnectTimeout(5 * 1000);
        // conn.setReadTimeout(5 * 1000);

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(postData);
        out.close();

        String response = "";

        Scanner inStream = new Scanner(conn.getInputStream());

        while (inStream.hasNextLine())
            response += (inStream.nextLine());

        return response;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:edu.vt.vbi.patric.portlets.CircosGenomeViewerPortlet.java

@SuppressWarnings("unchecked")
public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {

    String imageId = request.getParameter("imageId");
    String trackList = request.getParameter("trackList");
    JSONObject res = new JSONObject();
    res.put("success", true);
    res.put("imageId", imageId);
    res.put("trackList", trackList);

    PrintWriter writer = response.getWriter();
    res.writeJSONString(writer);//w  w w . j a v a 2s  .  c o  m
    writer.close();
}

From source file:gov.nih.nci.caarray.application.translation.geosoft.GeoSoftFileWriterUtilTest.java

@Test
public void testWriteSoftFile() throws Exception {
    Experiment experiment = helper.makeGoodExperiment();
    String permaLinkUrl = "http://example.com/my-experiment";
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    GeoSoftFileWriterUtil.writeSoftFile(experiment, permaLinkUrl, out);
    out.close();
    @SuppressWarnings("unchecked")
    List<String> lines = IOUtils.readLines(new StringReader(sw.toString()));

    final String[] expected = { "^SAMPLE=test-hyb", "!Sample_title=test-hyb",
            "!Sample_description=test-sample1, test-sample2", "!Sample_supplementary_file=raw_file.data",
            "!Sample_table=derived_file.data", "!Sample_source_name=test-source",
            "!Sample_organism=test Organizm 1", "!Sample_organism=test Organizm 2",
            "!Sample_treatment_protocol=\"some treatment:treatment desc\"; \"another treatment:another treatment desc\"",
            "!Sample_growth_protocol=\"some growth:growth desc\"",
            "!Sample_extract_protocol=\"some extract:extract desc\"",
            "!Sample_label_protocol=\"some label:labeling desc\"",
            "!Sample_hyb_protocol=\"some hybridization:hybridization desc\"",
            "!Sample_scan_protocol=\"some scan:scan desc\"",
            "!Sample_data_processing=\"data processing:data proc desc\"", "!Sample_label=label",
            "!Sample_biomaterial_provider=Affymetrix", "!Sample_platform_id=test-ga",
            "!Sample_characteristics=test-factor:test-value growth",
            "!Sample_characteristics=OrganismPart:some tissue site",
            "!Sample_characteristics=DiseaseState:some disease state",
            "!Sample_characteristics=ExternalId:test external id",
            "!Sample_characteristics=test-cat:test-val growth",
            "!Sample_characteristics=CellType:some cell type", "!Sample_molecule=other", "^SERIES=test-exp-id",
            "!Series_title=test-title", "!Series_summary=test-title",
            "!Series_overall_design=\"test-design-type1 (MO)\"; \"test-design-type2 (MO)\"",
            "!Series_pubmed_id=test-pub", "!Series_contributor=fff,mmm,lll",
            "!Series_web_link=http://example.com/my-experiment", "!Series_sample_id=test-hyb" };

    for (int i = 0; i < expected.length; i++) {
        String result = lines.get(i);
        assertEquals("line " + (i + 1), expected[i], result);
    }/*from  w  w  w.j  a v  a 2 s  .c o m*/

}

From source file:cn.vlabs.umt.ui.servlet.AppProxyServlet.java

private void sendRandom(HttpServletRequest request, HttpServletResponse response) throws IOException {
    RandomUtil ru = new RandomUtil();
    String random = ru.getRandom(20);
    request.getSession().setAttribute("proxy_random", random);
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(random);//from   w w w .ja  v a2  s  . c  o  m
    out.flush();
    out.close();
}

From source file:net.bashtech.geobot.BotManager.java

public static String postRemoteDataTwitch(String urlString, String postData, int krakenVersion) {
    URL url;//from   w  w  w.ja  v a2  s  . c o  m
    HttpURLConnection conn;

    try {
        url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        conn.setFixedLengthStreamingMode(postData.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Accept", "application/vnd.twitchtv.v" + krakenVersion + "+json");
        conn.setRequestProperty("Authorization", "OAuth " + BotManager.getInstance().krakenOAuthToken);
        conn.setRequestProperty("Client-ID", BotManager.getInstance().krakenClientID);
        // conn.setConnectTimeout(5 * 1000);
        // conn.setReadTimeout(5 * 1000);

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(postData);
        out.close();

        String response = "";

        Scanner inStream = new Scanner(conn.getInputStream());

        while (inStream.hasNextLine())
            response += (inStream.nextLine());

        inStream.close();
        return response;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:is.brendan.WarpMarkers.WarpMarkersTimerTask.java

public void run() {
    JSONArray jsonList = plugin.getJSON();
    JSONArray jsonListUpdates = plugin.getUpdatesJSON();

    if (jsonList != null) {
        try {/* w  w w.j  av  a 2  s .com*/
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
            writer.print(jsonList);
            writer.close();
        } catch (java.io.IOException e) {
            plugin.log(Level.SEVERE, "Unable to write to " + outputFile + ": " + e.getMessage(), e);
            e.printStackTrace();
        }
    }
    if (jsonListUpdates != null) {
        try {
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(updateFile)));
            writer.print(jsonListUpdates);
            writer.close();
        } catch (java.io.IOException e) {
            plugin.log(Level.SEVERE, "Unable to write to " + updateFile + ": " + e.getMessage(), e);
            e.printStackTrace();
        }
    }
}