Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Streams (<code>InputStream</code> and <code>OutputStream</code>) transfer <em>binary</em> data. If you want to write a string to a stream, you must first convert it to bytes, or in other words <em>encode</em> it. You can do that manually (as you suggest) using the <code>String.getBytes(Charset)</code> method, but you should avoid the <code>String.getBytes()</code> method, because that uses the default encoding of the JVM, which can't be reliably predicted in a portable way.</p> <p>The usual way to write character data to a stream, though, is to wrap the stream in a <a href="http://download.oracle.com/javase/8/docs/api/java/io/Writer.html" rel="nofollow noreferrer"><code>Writer</code></a>, (often a <a href="http://download.oracle.com/javase/8/docs/api/java/io/PrintWriter.html" rel="nofollow noreferrer"><code>PrintWriter</code></a>), that does the conversion for you when you call its <a href="http://download.oracle.com/javase/8/docs/api/java/io/Writer.html#write(java.lang.String)" rel="nofollow noreferrer"><code>write(String)</code></a> (or <a href="http://download.oracle.com/javase/8/docs/api/java/io/PrintWriter.html#print(java.lang.String)" rel="nofollow noreferrer"><code>print(String)</code></a>) method. The corresponding wrapper for InputStreams is a <a href="http://download.oracle.com/javase/8/docs/api/java/io/Reader.html" rel="nofollow noreferrer">Reader</a>.</p> <p><code>PrintStream</code> is a special <code>OutputStream</code> implementation in the sense that it also contain methods that automatically encode strings (it uses a writer internally). But it is still a stream. You can safely wrap your stream with a writer no matter if it is a <code>PrintStream</code> or some other stream implementation. There is no danger of double encoding.</p> <p>Example of PrintWriter with OutputStream:</p> <pre><code>try (PrintWriter p = new PrintWriter(new FileOutputStream("output-text.txt", true))) { p.println("Hello"); } catch (FileNotFoundException e1) { e1.printStackTrace(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload