@Override public PrintWriter getWriter() { return new PrintWriter( new OutputStreamWriter( output, StandardCharsets.UTF_8 ) ); } }
try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("filename.txt"), "utf-8"))) { writer.write("something"); }
/** * Returns a buffered writer that writes to a file using the given character set. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#newBufferedWriter(java.nio.file.Path, Charset, * java.nio.file.OpenOption...)}. * * @param file the file to write to * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for * helpful predefined constants * @return the buffered writer */ public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException { checkNotNull(file); checkNotNull(charset); return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)); }
public void initialize(OutputStream processIn, InputStream processOut) { try { this.processIn = new BufferedWriter(new OutputStreamWriter(processIn, DEFAULT_CHARSET)); this.processOut = new BufferedReader(new InputStreamReader(processOut, DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
public void biList2File(String output) throws IOException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF-8")); for (ArrayList<String> list : clusterResult) { for (String s : list) { bw.write(s + " "); } bw.write("\n"); } bw.close(); }
private OutputStreamWriter getFileWriter() throws FileNotFoundException { return new OutputStreamWriter( new FileOutputStream(new File(baseDir, filePattern.print(currentDay)), true), StandardCharsets.UTF_8 ); }
public class ProcessWriter { public static void main(String[] args) throws Exception { ProcessBuilder builder = new ProcessBuilder("java", "Test"); builder.directory(new File("C:\\Users\\sotirios.delimanolis\\Downloads")); Process process = builder.start(); OutputStream stdin = process.getOutputStream(); // <- Eh? InputStream stdout = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin)); writer.write("Sup buddy"); writer.flush(); writer.close(); Scanner scanner = new Scanner(stdout); while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } } }
private void save() { try { PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(path)), "UTF-8")); urlTokens.forEach(printWriter::println); printWriter.close(); } catch (IOException e) { logger.error("write file error", e); } }
private Session(Socket socket) throws IOException { client = socket; in = new BufferedReader(new InputStreamReader(client.getInputStream(), charset)); out = new PrintWriter(new OutputStreamWriter(client.getOutputStream(), charset)); start(); }
File textFile = new File("/path/to/file.txt"); File binaryFile = new File("/path/to/file.bin"); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); ) {
public static void writePluginServices(Iterable<String> plugins, File root) throws IOException { Path path = root.toPath().resolve(SERVICES_FILE); createDirectories(path.getParent()); try (Writer out = new OutputStreamWriter(new FileOutputStream(path.toFile()), UTF_8)) { for (String plugin : plugins) { out.write(plugin + "\n"); } } }
@BeforeClass public static void setup() throws IOException { TEST_DIR = tempDir.newFolder(); for (int i = 0; i < 100; i++) { try ( CountingOutputStream cos = new CountingOutputStream( Files.newOutputStream(new File(TEST_DIR, "test_" + i).toPath()) ); Writer writer = new BufferedWriter(new OutputStreamWriter(cos, StandardCharsets.UTF_8)) ) { for (int j = 0; j < 100; j++) { final String a = StringUtils.format("%d,%03d,%03d\n", (20171220 + i), i, j); writer.write(a); } writer.flush(); // Every file size must be same if (FILE_SIZE == -1) { FILE_SIZE = cos.getCount(); } else { Assert.assertEquals(FILE_SIZE, cos.getCount()); } } } }
public static PrintWriter getPrintWriter(File textFile, String encoding) throws IOException { File f = textFile.getAbsoluteFile(); if (encoding == null) { encoding = defaultEncoding; } return new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding)), true); }
public StreamConsumer( InputStream in, OutputStream out, boolean quiet, String prefix, StreamExceptionHandler failureHandler ) { this.quiet = quiet; this.prefix = prefix; this.failureHandler = failureHandler; this.in = new BufferedReader( new InputStreamReader( in ) ); this.out = new OutputStreamWriter( out ); this.stackTraceOfOrigin = new Exception( "Stack trace of thread that created this StreamConsumer" ); }
public OneFilePipeline(String path) throws FileNotFoundException, UnsupportedEncodingException { setPath(path); printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getFile(path)), "UTF-8")); }
DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8")); writer.write(content); writer.close(); wr.close();
@SuppressWarnings("resource") // 'in' is deliberately not closed @Test public void testCopy_readerToWriter() throws Exception { InputStream in = new ByteArrayInputStream(inData); in = new YellOnCloseInputStream(in); final Reader reader = new InputStreamReader(in, "US-ASCII"); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true); final Writer writer = new OutputStreamWriter(baout, "US-ASCII"); final int count = IOUtils.copy(reader, writer); out.off(); writer.flush(); assertEquals("The number of characters returned by copy is wrong", inData.length, count); assertEquals("Sizes differ", inData.length, baout.size()); assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); }
private static boolean writeValue(Socket client, String value) { boolean result; BufferedWriter out = null; try { OutputStream clientStream = client.getOutputStream(); out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024); out.write(value); out.write("\n"); out.flush(); result = true; } catch (Exception e) { result = false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { result = false; } } } return result; }
/** * Copy the contents of the given String to the given output OutputStream. * Leaves the stream open when done. * @param in the String to copy from * @param charset the Charset * @param out the OutputStream to copy to * @throws IOException in case of I/O errors */ public static void copy(String in, Charset charset, OutputStream out) throws IOException { Assert.notNull(in, "No input String specified"); Assert.notNull(charset, "No charset specified"); Assert.notNull(out, "No OutputStream specified"); Writer writer = new OutputStreamWriter(out, charset); writer.write(in); writer.flush(); }
public static PrintWriter getPrintWriter(String filename, String encoding) throws IOException { OutputStream out = getFileOutputStream(filename); if (encoding == null) { encoding = defaultEncoding; } return new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, encoding)), true); }