gpt4 book ai didi

java - Android:无缓冲 IO

转载 作者:行者123 更新时间:2023-11-29 06:19:34 24 4
gpt4 key购买 nike

我想使用非阻塞 IO 从后台进程读取流/输出。谁能给我一个关于如何在 Android 上使用非阻塞 IO 的示例?

感谢您的帮助。

最佳答案

这里是 the class I use从 Internet 下载文件或在文件系统中复制文件以及我如何使用它:

// Download a file to /data/data/your.app/files
new DownloadFile(ctxt, "http://yourfile", ctxt.openFileOutput("destinationfile.ext", Context.MODE_PRIVATE));

// Copy a file from raw resource to the files directory as above
InputStream in = ctxt.getResources().openRawResource(R.raw.myfile);
OutputStream out = ctxt.openFileOutput("filename.ext", Context.MODE_PRIVATE);
final ReadableByteChannel ic = Channels.newChannel(in);
final WritableByteChannel oc = Channels.newChannel(out);
DownloadFile.fastChannelCopy(ic, oc);

还有选择器方法,这里有一些关于选择器、 channel 和线程的很棒的 (Java) 教程:

  1. http://jfarcand.wordpress.com/2006/05/30/tricks-and-tips-with-nio-part-i-why-you-must-handle-op_write
  2. http://jfarcand.wordpress.com/2006/07/06/tricks-and-tips-with-nio-part-ii-why-selectionkey-attach-is-evil/
  3. http://jfarcand.wordpress.com/2006/07/07/tricks-and-tips-with-nio-part-iii-to-thread-or-not-to-thread/
  4. http://jfarcand.wordpress.com/2006/07/19/httpweblogs-java-netblog20060719tricks-and-tips-nio-part-iv-meet-selectors/
  5. http://jfarcand.wordpress.com/2006/09/21/tricks-and-tips-with-nio-part-v-ssl-and-nio-friend-or-foe/

关于java - Android:无缓冲 IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3693758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com