我無意看到
DataOutputStream
的源碼.結果發現這段.
/** * Writes out the string to the underlying output stream as a * sequence of bytes. Each character in the string is written out, in * sequence, by discarding its high eight bits. If no exception is * thrown, the counter written is incremented by the * length of s. * * @param s a string of bytes to be written. * @exception IOException if an I/O error occurs. * @see java.io.FilterOutputStream#out */ public final void writeBytes(String s) throws IOException {int len = s.length();for (int i = 0 ; i out.writer((byte)s.charAt(i));//這個因為英文一個位元組就夠了,可中文是兩個位元組,結果我們的中文就在這裡被扔掉一半.於是就再輸出就亂碼了.相比之下: /** * Writes a string to the underlying output stream as a sequence of * characters. Each character is written to the data output stream as * if by the writeChar method. If no exception is * thrown, the counter written is incremented by twice * the length of s. * * @param s a String value to be written. * @exception IOException if an I/O error occurs. * @see java.io.DataOutputStream#writeChar(int) * @see java.io.FilterOutputStream#out */ public final void writeChars(String s) throws IOException { int len = s.length(); for (int i = 0 ; i >> 8) & 0xFF); out.write((v >>> 0) & 0xFF); } incCount(len * 2); }
這個要好一點了,中文就可以真確的被寫入了.