日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

print打印字符串之谜

發布時間:2025/3/20 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 print打印字符串之谜 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

print打印字符串之謎 博客分類: java

string 中維護了一個char數組的value值,打印的時候也就是打印的這個數組的值。但是print(str)參數為string類型

?

看print的源碼

?

/*** Prints a String and then terminate the line. This method behaves as* though it invokes <code>{@link #print(String)}</code> and then* <code>{@link #println()}</code>.** @param x The <code>String</code> to be printed.*/public void println(String x) {synchronized (this) {print(x);newLine();}}

? ? public void print(String s) {

if (s == null) {s = "null";}write(s);}

? ? private void write(String s) {

try {synchronized (this) {ensureOpen();textOut.write(s);//java.io.BufferedWriter.write(s)textOut.flushBuffer();charOut.flushBuffer();//java.io.OutputStreamWriter.flushBuffer();if (autoFlush && (s.indexOf('\n') >= 0))out.flush();}}

?//在看看write方法調用了getChars方法,這是輸出string,char數組值的關鍵

public void write(String s, int off, int len) throws IOException {synchronized (lock) {ensureOpen();int b = off, t = off + len;while (b < t) {int d = min(nChars - nextChar, t - b);s.getChars(b, b + d, cb, nextChar);b += d;nextChar += d;if (nextChar >= nChars)flushBuffer();}}}

? ?//最終調用了printStream方法的write方法

public void write(byte buf[], int off, int len) {try {synchronized (this) {ensureOpen();out.write(buf, off, len);if (autoFlush)out.flush();//關鍵在這一步調用了java.io.BufferedOutputStream的flushBuffer方法}}catch (InterruptedIOException x) {Thread.currentThread().interrupt();}

??java.io.BufferedOutputStream的flushBuffer方法

/** Flush the internal buffer */最終調用了java.io.FileOutputStream..write(buf, 0, count);方法private void flushBuffer() throws IOException {if (count > 0) {out.write(buf, 0, count);//java.io.FileOutputStream..write(buf, 0, count);count = 0;}}

? ? //?java.io.FileOutputStream..write最終調用了writeBytes方法

public void write(byte b[], int off, int len) throws IOException {writeBytes(b, off, len);}

??//writeBytesa方法是本地方法最終以byte數組輸出

private native void writeBytes(byte b[], int off, int len) throws IOException;

? ?jdK native code

??JNIEXPORT void JNICALL

Java_java_io_FileOutputStream_writeBytes(JNIEnv *env,jobject this, jbyteArray bytes, jint off, jint len) {jboolean append = (*env)->GetBooleanField(env, this, fos_append);FD fd = GET_FD(this, fos_fd);if (fd == -1) {JNU_ThrowIOException(env, "Stream Closed");return;}if (append == JNI_TRUE) {if (IO_Lseek(fd, 0L, SEEK_END) == -1) {JNU_ThrowIOExceptionWithLastError(env, "Append failed");}}writeBytes(env, this, bytes, off, len, fos_fd); }

轉載于:https://my.oschina.net/xiaominmin/blog/1598168

總結

以上是生活随笔為你收集整理的print打印字符串之谜的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。