List of usage examples for java.io PrintWriter close
public void close()
From source file:modnlp.capte.AlignerUtils.java
public static void convertLineEndings(String inputfile, String outputfile) { /* This is a quick hackaround to convert the line endings from * Windows files into Unix line endings so that the sentence splitter will work * Seems to work, but hasn't been extensively tested. */// ww w . ja v a 2 s . co m try { File input = new File(inputfile); File output = new File(outputfile); BufferedReader bb = new BufferedReader(new InputStreamReader(new FileInputStream(inputfile), "UTF8")); PrintWriter bv = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputfile), "UTF8")); String l = ""; while (bb.ready()) { l = bb.readLine(); bv.write(l + "\n"); } bb.close(); bv.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.Step4MTurkOutputCollector.java
/** * Creates .success files for updating HITs in order to require more assignments. * * @param assignmentsPerHits actual assignments per HIT * @param hitTypeID type/*from w w w.j av a 2 s .c o m*/ * @param resultFile source MTurk file * @throws IOException IO exception */ static void prepareUpdateHITsFiles(Map<String, Integer> assignmentsPerHits, String hitTypeID, File resultFile) throws IOException { TreeSet<Integer> assignmentNumbers = new TreeSet<>(assignmentsPerHits.values()); System.out.println(assignmentsPerHits); // how many is required to be fully annotated final int fullyAnnotated = 5; assignmentNumbers.remove(fullyAnnotated); for (Integer i : assignmentNumbers) { // output file int annotationsRequired = fullyAnnotated - i; File file = new File(resultFile + "_requires_extra_assignments_" + annotationsRequired + ".success"); PrintWriter pw = new PrintWriter(file, "utf-8"); pw.println("hitid\thittypeid"); for (Map.Entry<String, Integer> entry : assignmentsPerHits.entrySet()) { if (i.equals(entry.getValue())) { pw.println(entry.getKey() + "\t" + hitTypeID); } } pw.close(); System.out.println( "Extra annotations required (" + annotationsRequired + "), saved to " + file.getAbsolutePath()); } }
From source file:com.threecrickets.sincerity.util.IoUtil.java
/** * Writes JVM primitives and collection/map instances to a JSON UTF-8 file. * //from www.j a v a 2 s.co m * @param file * The file * @param object * The object (JVM primitives and collection instances) * @param expand * Whether to expand the JSON with newlines, indents, and spaces * @throws IOException * In case of an I/O error */ public static void writeJson(File file, Object object, boolean expand) throws IOException { String content = Json.to(object, expand).toString(); FileOutputStream stream = new FileOutputStream(file); PrintWriter writer = new PrintWriter( new BufferedWriter(new OutputStreamWriter(stream, StandardCharsets.UTF_8), BUFFER_SIZE)); try { writer.write(content); } finally { writer.close(); } }
From source file:com.nary.Debug.java
public static String getStackTraceAsString(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw);//from ww w .j ava 2s .co m pw.close(); return sw.toString(); }
From source file:PostTest.java
/** * Makes a POST request and returns the server response. * @param urlString the URL to post to/* w w w. j a v a 2 s . c om*/ * @param nameValuePairs a map of name/value pairs to supply in the request. * @return the server reply (either from the input stream or the error stream) */ public static String doPost(String urlString, Map<String, String> nameValuePairs) throws IOException { URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); boolean first = true; for (Map.Entry<String, String> pair : nameValuePairs.entrySet()) { if (first) first = false; else out.print('&'); String name = pair.getKey(); String value = pair.getValue(); out.print(name); out.print('='); out.print(URLEncoder.encode(value, "UTF-8")); } out.close(); Scanner in; StringBuilder response = new StringBuilder(); try { in = new Scanner(connection.getInputStream()); } catch (IOException e) { if (!(connection instanceof HttpURLConnection)) throw e; InputStream err = ((HttpURLConnection) connection).getErrorStream(); if (err == null) throw e; in = new Scanner(err); } while (in.hasNextLine()) { response.append(in.nextLine()); response.append("\n"); } in.close(); return response.toString(); }
From source file:io.github.data4all.util.upload.ChangesetUtil.java
/** * Parses all objects from the database into an xml structure to create a * new Changeset.//ww w.j a v a 2s .c o m * * @param context * The application context for the {@link DataBaseHandler}. * @param changesetId * The Changeset ID from the OSM API. * @return The created Changeset. * @throws OsmException * Indicates an failure in an osm progess. */ public static String getChangesetXml(Context context, int changesetId) throws OsmException { final DataBaseHandler db = new DataBaseHandler(context); final List<DataElement> elems = db.getAllDataElements(); db.close(); final StringBuilder builder = new StringBuilder(); final PrintWriter writer = new PrintWriter(new OutputStream() { @Override public void write(int oneByte) throws IOException { builder.append((char) oneByte); } }); OsmChangeParser.parseElements(elems, changesetId, writer); writer.close(); return builder.toString(); }
From source file:org.fcrepo.test.api.TestHTTPStatusCodes.java
private static void writeStringToFile(String string, File file) throws Exception { FileOutputStream out = new FileOutputStream(file); PrintWriter writer = new PrintWriter(new OutputStreamWriter(out)); try {//from w w w .j ava 2s.co m writer.print(string); } finally { writer.close(); } }
From source file:Main.java
public static String post(String url, Map<String, String> params) { try {/*from w ww. j a va 2s . co m*/ URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); PrintWriter pw = new PrintWriter(connection.getOutputStream()); StringBuilder sbParams = new StringBuilder(); if (params != null) { for (String key : params.keySet()) { sbParams.append(key + "=" + params.get(key) + "&"); } } if (sbParams.length() > 0) { String strParams = sbParams.substring(0, sbParams.length() - 1); Log.e("cat", "strParams:" + strParams); pw.write(strParams); pw.flush(); pw.close(); } BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer response = new StringBuffer(); String readLine = ""; while ((readLine = br.readLine()) != null) { response.append(readLine); } br.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:controller.file.FileUploader.java
public static void fileDownloader(HttpServletRequest request, HttpServletResponse response) { PrintWriter out = null; try {// ww w . j a va 2 s . c o m String filename = "foo.xml"; String filepath = "/tmp/"; out = response.getWriter(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename); int i; while ((i = fileInputStream.read()) != -1) { out.write(i); } fileInputStream.close(); out.close(); } catch (IOException ex) { Logger.getLogger(FileUploader.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } }
From source file:cn.vlabs.umt.ui.servlet.AddClientServlet.java
public static void writeJSONObject(HttpServletResponse response, Object object) { PrintWriter writer = null; try {//from w w w . jav a2 s .co m //IE???text/html? response.setContentType("text/html"); writer = response.getWriter(); writer.write(object.toString()); } catch (IOException e) { } finally { if (writer != null) { writer.flush(); writer.close(); } } }