// get external storage file reference FileWriter writer = new FileWriter(getExternalStorageDirectory()); // Writes the content to the file writer.write("This\n is\n an\n example\n"); writer.flush(); writer.close();
String urlParameters = "param1=a¶m2=b¶m3=c"; URL url = new URL("http://example.com/index.php"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(urlParameters); writer.flush(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) != null) { System.out.println(line); } writer.close(); reader.close();
try { url = new URL(serverUrl); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setConnectTimeout(1000 * 5); wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); responseBuilder = new StringBuilder(); String line = null; wr.close(); } catch (IOException e) { LOG.error("close error", e);
protected InputStream postMessage(Map<String, String>content, URL destination, String encodeCharset) throws IOException { HttpURLConnection connection = (HttpURLConnection) destination.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); osw = new OutputStreamWriter(connection.getOutputStream()); boolean isFirst = true; for (String key : content.keySet()) { if (!isFirst) { osw.write("&"); osw.write(URLEncoder.encode(key, encodeCharset)); osw.write("="); osw.write(URLEncoder.encode(value, encodeCharset)); osw.flush(); osw.close(); } catch (IOException e) { } finally { if (osw != null) { try { osw.close(); } catch (Throwable e) {}
try { OutputStreamWriter out = new OutputStreamWriter(connSock.getOutputStream()); out.write(msg); out.flush(); try { InputStreamReader in = new InputStreamReader(connSock.getInputStream()); n = in.read(); return n;
private boolean sendCommand(String cmd, @NotNull Process pythonProcess) { try { OutputStreamWriter writer = new OutputStreamWriter(pythonProcess.getOutputStream()); writer.write(cmd); writer.write("\n"); writer.flush(); return true; } catch (Exception e) { _.msg("\nFailed to send command to interpreter: " + cmd); return false; } }
try (InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8); OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { if (!velocityEngine.evaluate(context, writer, logTag, reader)) { throw new ReportException("Failed to convert the template into html."); writer.flush(); } catch (UnsupportedEncodingException ex) { throw new ReportException("Unable to generate the report using UTF-8", ex);
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); try { OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET); serializer.write(value, osw); osw.flush(); } catch (RuntimeException | IOException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
public void generateNoteOnSD(Context context, String sFileName, String sBody) { try { File root = new File(Environment.getExternalStorageDirectory(), "Notes"); if (!root.exists()) { root.mkdirs(); } File gpxfile = new File(root, sFileName); FileWriter writer = new FileWriter(gpxfile); writer.append(sBody); writer.flush(); writer.close(); Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } }
interface Printer { void print(String msg); } class ConsolePrinter implements Printer { public void print(String msg) { System.out.print(msg); } } class FilePrinter implements Printer { private final FileWriter fileWriter; public FilePrinter(File file) { this.fileWriter = new FileWriter(file); //here you should do some exceptions handling } public void print(String msg) { fileWriter.write(msg); //here you should also care about exceptions fileWriter.flush(); } } Printer chosenPrinter = printOnFile ? new FilePrinter(file) : new ConsolePrinter(); chosenPrinter.print("Hello");
URL url = new URL(urlString); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setRequestProperty ("Authorization", encodedCredentials); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(data); writer.flush(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) != null) { System.out.println(line); } writer.close(); reader.close();
public void Request(StringBuffer urlFormat ) throws IOException { URL u = new URL("http://mon.alibaba-inc.com/noticenter/send.do"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(),"UTF-8"); osw.write(urlFormat.toString()); osw.flush(); osw.close(); InputStreamReader urlStream = new InputStreamReader(con.getInputStream(), "utf-8"); BufferedReader in = new BufferedReader(urlStream); int bufferlen = 1; char[] buffer = new char[bufferlen]; int readBytes = 0; while (true) { readBytes = in.read(buffer, 0, bufferlen); if (readBytes < 0) { break; } } in.close(); urlStream.close(); con.disconnect(); }
public void mCreateAndSaveFile(String params, String mJsonResponse) { try { FileWriter file = new FileWriter("/data/data/" + getApplicationContext().getPackageName() + "/" + params); file.write(mJsonResponse); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } }
@Override void run(String[] args) { try { CommandLine cli = parseOptions(args); JCEKSKeystoreCredentialStore credStore = loadKeystore(cli.getOptionValue(KEYSTORE_LOCATION.getOpt())); Map<Integer, String> base64Keys = new HashMap<>(); Map<String, byte[]> keys = credStore.getAllEncodedKeys(); for (Map.Entry<String, byte[]> e: keys.entrySet()) { base64Keys.put(Integer.valueOf(e.getKey()), DatatypeConverter.printBase64Binary(e.getValue())); } OutputStreamWriter fOs = new OutputStreamWriter( new FileOutputStream(new File(cli.getOptionValue(OUTPUT_LOCATION.getOpt()))), StandardCharsets.UTF_8); Gson gson = new GsonBuilder().disableHtmlEscaping().create(); fOs.write(gson.toJson(base64Keys)); fOs.flush(); fOs.close(); } catch (ParseException e) { printUsage(); } catch (IOException e) { throw new RuntimeException(e); } } }
private static void copyStreamAndInjectWorksheet(InputStream in, OutputStream out, InputStream worksheetData) throws IOException { InputStreamReader inReader = new InputStreamReader(in, StandardCharsets.UTF_8); OutputStreamWriter outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8); boolean needsStartTag = true; int c; if (c == -1) outWriter.write(s); break; outWriter.write(s); outWriter.write(c); s = "</sheetData>"; n = s.length(); outWriter.flush(); if (needsStartTag) outWriter.write("<sheetData>\n"); outWriter.flush(); outWriter.write("</sheetData>"); outWriter.flush(); outWriter.write(c); outWriter.flush();
public static void writeStringToFile(@NonNull File file, @NonNull String content) throws IOException { final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), ACRAConstants.UTF8); try { writer.write(content); writer.flush(); } finally { safeClose(writer); } }
/** * Serialize chars from a <code>Reader</code> to bytes on an * <code>OutputStream</code>, and flush the <code>OutputStream</code>. * Uses the default platform encoding. * @param input the <code>Reader</code> to read from * @param output the <code>OutputStream</code> to write to * @throws IOException In case of an I/O problem * @deprecated 2.5 use {@link #copy(Reader, OutputStream, String)} instead */ @Deprecated public static void copy( final Reader input, final OutputStream output) throws IOException { // make explicit the dependency on the default encoding final OutputStreamWriter out = new OutputStreamWriter(output, Charset.defaultCharset()); copy(input, out); // XXX Unless anyone is planning on rewriting OutputStreamWriter, we // have to flush here. out.flush(); }
import java.io.FileWriter; import java.io.IOException; import User; public class GenerateCsv { private static void generateCsvFile(String fileName, ArrayList<User> users) { try { FileWriter writer = new FileWriter(fileName); writer.append("Email"); writer.append(','); writer.append("Name"); writer.append('\n'); for (User user in users) { writer.append(user.getEmail()); writer.append(','); writer.append(user.getName()); writer.append('\n'); } writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } } }