简体   繁体   English

不连续地在Java中写入文件的最佳方法是什么?

[英]what is the best way to write to a file in java uncontinuously?

I'm programming an android app that transfers a file over bluetooth. 我正在编程一个通过蓝牙传输文件的android应用。

My question is, what is the best way to write a file uncontinuously, meaning the file is being written in the external storage directory while the data is sent in chunks via bluetooth. 我的问题是,连续写入文件的最佳方法是什么,这意味着文件正在通过蓝牙以大块发送的方式写入外部存储目录中。 the writing obviously occurs faster and takes pauses between receiving two chunks. 显然,写入速度更快,并且在接收两个块之间需要暂停。

Are RandomAccessFile and FileChannel efficient for that task? RandomAccessFile和FileChannel对于该任务有效吗?

Thanks in advance. 提前致谢。

PS: file size is about 10MB PS:文件大小约为10MB

if you go to utilize RandomAccessFile , so beware about the flushing mechanism, its not manual, and it may give some time(specially when you write data small). 如果您要使用RandomAccessFile ,请提防刷新机制,它不是手动的,它可能会花费一些时间(特别是当您小的数据写入时)。 my suggestion is FileChanel , you would take control of flushing, of course it doesn't support direct functionality for writing primitive data, but you would utilize ByteBuffer to doing so. 我的建议是FileChanel ,您可以控制刷新,当然它不支持直接功能来写入原始数据,但是您可以利用ByteBuffer来做到这一点。

Why random access? 为什么要随机访问? Are you going to write in the middle of the file? 您要写在文件中间吗? If not than a simple file appender will do. 如果不是,那么简单的文件附加器就可以了。 And you don't need to keep your file open all the time... 而且您不需要一直保持文件打开状态...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM