List of usage examples for java.io BufferedWriter flush
public void flush() throws IOException
From source file:com.linkedin.pinot.controller.helix.ControllerTest.java
public static String sendPostRequest(String urlString, String payload) throws UnsupportedEncodingException, IOException, JSONException { LOGGER.info("Sending POST to " + urlString + " with payload " + payload); final long start = System.currentTimeMillis(); final URL url = new URL(urlString); final URLConnection conn = url.openConnection(); conn.setDoOutput(true);/*w w w .ja va 2 s. c o m*/ final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); writer.write(payload, 0, payload.length()); writer.flush(); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); final StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } final long stop = System.currentTimeMillis(); LOGGER.info(" Time take for Request : " + payload + " in ms:" + (stop - start)); return sb.toString(); }
From source file:com.example.makerecg.NetworkUtilities.java
/** * Perform 2-way sync with the server-side ADSampleFrames. We send a request that * includes all the locally-dirty contacts so that the server can process * those changes, and we receive (and return) a list of contacts that were * updated on the server-side that need to be updated locally. * * @param account The account being synced * @param authtoken The authtoken stored in the AccountManager for this * account//from w w w .j av a 2 s . c o m * @param serverSyncState A token returned from the server on the last sync * @param dirtyFrames A list of the frames to send to the server * @return A list of frames that we need to update locally */ public static List<ADSampleFrame> syncSampleFrames(Account account, String authtoken, long serverSyncState, List<ADSampleFrame> dirtyFrames) throws JSONException, ParseException, IOException, AuthenticationException { List<JSONObject> jsonFrames = new ArrayList<JSONObject>(); for (ADSampleFrame frame : dirtyFrames) { jsonFrames.add(frame.toJSONObject()); } JSONArray buffer = new JSONArray(jsonFrames); JSONObject top = new JSONObject(); top.put("data", buffer); // Create an array that will hold the server-side ADSampleFrame // that have been changed (returned by the server). final ArrayList<ADSampleFrame> serverDirtyList = new ArrayList<ADSampleFrame>(); // Send the updated frames data to the server //Log.i(TAG, "Syncing to: " + SYNC_URI); URL urlToRequest = new URL(SYNC_URI); HttpURLConnection conn = (HttpURLConnection) urlToRequest.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "Bearer " + authtoken); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(top.toString(1)); writer.flush(); writer.close(); os.close(); //Log.i(TAG, "body="+top.toString(1)); int responseCode = conn.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { // Our request to the server was successful - so we assume // that they accepted all the changes we sent up, and // that the response includes the contacts that we need // to update on our side... // TODO: Only uploading data for now ... String response = ""; String line; BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = br.readLine()) != null) { response += line; } br.close(); //Log.i(TAG, "response="+response); /* final JSONArray serverContacts = new JSONArray(response); Log.d(TAG, response); for (int i = 0; i < serverContacts.length(); i++) { ADSampleFrame rawContact = ADSampleFrame.valueOf(serverContacts.getJSONObject(i)); if (rawContact != null) { serverDirtyList.add(rawContact); } } */ } else { if (responseCode == HttpsURLConnection.HTTP_UNAUTHORIZED) { Log.e(TAG, "Authentication exception in while uploading data"); throw new AuthenticationException(); } else { Log.e(TAG, "Server error in sending sample frames: " + responseCode); throw new IOException(); } } return null; }
From source file:es.sotileza.plugin.utils.Utilidades.java
public static void paintFile(String file, List<String> lineas) throws IOException { FileWriter output = new FileWriter(file); BufferedWriter writer = new BufferedWriter(output); for (String i : lineas) writer.write(i + "\n"); System.out.println("Generado el fichero: " + file); writer.flush(); }
From source file:fr.ece.epp.tools.Utils.java
public static void writeScript(String path, String version) { String system = System.getProperty("os.name"); try {/*from w ww . j a v a 2 s . c o m*/ if (system.startsWith("Windows")) { File writename = new File(path); writename.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("%~d0\n\r"); out.write("cd %~dp0\n\r"); out.write("mvn install -P base," + version); out.flush(); // out.close(); // ? } else if (system.startsWith("Linux") || system.startsWith("Mac")) { File writename = new File(path); writename.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("#!/bin/sh\n\n"); out.write("BASEDIR=$(dirname $0)\n"); out.write("cd $BASEDIR\n"); out.write("mvn install -P base," + version); out.flush(); // out.close(); // ? } } catch (IOException ex) { ex.printStackTrace(); } }
From source file:edgeserver.Publicador.java
private static void armazenaFila(ArrayList filaPublicacoes) throws IOException { new PrintWriter("fila.txt").close(); filaPublicacoes.stream().forEach((publicacao) -> { BufferedWriter bw = null; try {//from ww w. ja v a 2 s . co m // APPEND MODE SET HERE bw = new BufferedWriter(new FileWriter("fila.txt", true)); bw.write(publicacao.toString()); bw.newLine(); bw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { // always close the file if (bw != null) try { bw.close(); } catch (IOException ioe2) { // just ignore it } } }); }
From source file:com.dc.util.file.FileSupport.java
public static void appendToFile(File file, String data) throws IOException { FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try {//from ww w . ja v a 2 s. c om fileWriter = new FileWriter(file, true); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(data); bufferedWriter.flush(); } finally { if (fileWriter != null) { fileWriter.close(); } if (bufferedWriter != null) { bufferedWriter.close(); } } }
From source file:com.dc.util.file.FileSupport.java
public static void writeTextFile(String folderPath, String fileName, String data, boolean isExecutable) throws IOException { File dir = new File(folderPath); if (!dir.exists()) { dir.mkdirs();//from w w w.j av a2s .co m } File file = new File(dir, fileName); FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileWriter = new FileWriter(file); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(data); bufferedWriter.flush(); if (isExecutable) { file.setExecutable(true); } } finally { if (fileWriter != null) { fileWriter.close(); } if (bufferedWriter != null) { bufferedWriter.close(); } } }
From source file:com.packtpub.mahout.cookbook.chapter01.App.java
private static void CreateCsvRatingsFile() throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader(inputFile)); BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile)); String line = null;// w ww .j av a2 s.co m String line2write = null; String[] temp; int i = 0; while ((line = br.readLine()) != null && i < 1000) { i++; temp = line.split("::"); line2write = temp[0] + "," + temp[1]; bw.write(line2write); bw.newLine(); bw.flush(); } br.close(); bw.close(); }
From source file:uk.ac.bbsrc.tgac.miso.integration.util.IntegrationUtils.java
/** * Sends a String message to a given host socket * /*ww w . j a v a2 s. c o m*/ * @param socket * of type Socket * @param query * of type String * @return String * @throws IntegrationException * when the socket couldn't be created */ public static String sendMessage(Socket socket, String query) throws IntegrationException { BufferedWriter wr = null; BufferedReader rd = null; try { wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")); // Send data wr.write(query + "\r\n"); wr.flush(); // Get response rd = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; StringBuilder sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line); } wr.close(); rd.close(); String dirty = sb.toString(); StringBuilder response = new StringBuilder(); int codePoint; int i = 0; while (i < dirty.length()) { codePoint = dirty.codePointAt(i); if ((codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) { response.append(Character.toChars(codePoint)); } i += Character.charCount(codePoint); } return response.toString().replace("\\\n", "").replace("\\\t", ""); } catch (UnknownHostException e) { log.error("Cannot resolve host: " + socket.getInetAddress(), e); throw new IntegrationException(e.getMessage()); } catch (IOException e) { log.error("Couldn't get I/O for the connection to: " + socket.getInetAddress(), e); throw new IntegrationException(e.getMessage()); } finally { try { if (wr != null) { wr.close(); } if (rd != null) { rd.close(); } } catch (Throwable t) { log.error("close socket", t); } } }
From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
/** * Method appends a String to File/* ww w. ja v a 2 s .co m*/ * @param fileName * @param contentString * @return */ public static String appendStringToResultFile(String fileName, String contentString) { FileWriter fw = null; try { inputFile = new File(Configuration.getResultDirPath() + fileName); fw = new FileWriter(inputFile, true); log.info(Configuration.getResultDirPath()); BufferedWriter bw = new BufferedWriter(fw); bw.append(contentString); bw.flush(); } catch (IOException ioExc) { log.error(ioExc); } finally { if (fw != null) { try { fw.close(); } catch (IOException ioExc) { log.error(ioExc); } } } log.info("File-Size, Ergebnis: " + inputFile.length()); return inputFile.getName(); }