// 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();
@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()); } }
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();
InputStream input = null; String logTag = null; final File f = new File(templateName); try { if (f.isFile()) { 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);
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; } }
while ((socket = serverSocket.accept()) != null) { InputStream inputStream = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String req = reader.readLine().split(" ")[1]; int i = inputStream.available(); socket.shutdownInput(); if (req.endsWith("MultiEnt")) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); outputStreamWriter.append("HTTP/1.0 200 OK\n" + "Connection: close\n" + "Content-Type: text/plain; charset=iso-8859-1\n" + "Content-Length: 2\n" + "Content-Length: 1\n" + "\n0\n"); outputStreamWriter.flush(); socket.shutdownOutput(); } else if (req.endsWith("MultiOther")) { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); outputStreamWriter.append("HTTP/1.0 200 OK\n" + "Connection: close\n" + "Content-Type: text/plain; charset=iso-8859-1\n" + "Content-Length: 1\n" + "X-Forwarded-For: abc\n" + "X-Forwarded-For: def\n" + "\n0\n"); outputStreamWriter.flush(); socket.shutdownOutput();
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");
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(); } } }
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 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(); } }
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(); }
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);
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(); } }
public void handleTokenize(String arg, OutputStream outStream) throws IOException { if (arg == null) { return; } List<? extends HasWord> tokens = parser.tokenize(arg); OutputStreamWriter osw = new OutputStreamWriter(outStream, "utf-8"); for (int i = 0; i < tokens.size(); ++i) { HasWord word = tokens.get(i); if (i > 0) { osw.write(" "); } osw.write(word.toString()); } osw.write("\n"); osw.flush(); }
/** * Serialize chars from a <code>Reader</code> to bytes on an * <code>OutputStream</code>, and flush the <code>OutputStream</code>. * @param input the <code>Reader</code> to read from * @param output the <code>OutputStream</code> to write to * @param encoding The name of a supported character encoding. See the * <a href="http://www.iana.org/assignments/character-sets">IANA * Charset Registry</a> for a list of valid encoding types. * @throws IOException In case of an I/O problem * @since 2.5 */ public static void copy( final Reader input, final OutputStream output, final String encoding) throws IOException { final OutputStreamWriter out = new OutputStreamWriter(output, encoding); copy(input, out); // XXX Unless anyone is planning on rewriting OutputStreamWriter, we // have to flush here. out.flush(); }