本文介绍了在外部存储android文件中编写虚拟内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面提到的代码中,我在一个名为testfile.txt的文件中写入虚拟内容。但我想在外部存储的任何文件中写入虚拟内容。我不希望文件名是硬编码的。我该怎么办?

In the below mentioned code I am writing dummy contents in a file called "testfile.txt". But I want to write dummy contents in any of the files of the external storage. I don't want the file name to be hard coded. How do I do?

String root = android.os.Environment.getExternalStorageDirectory().getPath();
File myFile = new File(root,"testfile.txt");

FileChannel rwChannel = new RandomAccessFile(myFile, "rw").getChannel();
int numBytes = (int)rwChannel.size();
ByteBuffer buffer = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, numBytes);
System.out.println("buffer"+buffer);
byte[] randomBytes = new byte[numBytes];
new Random().nextBytes(randomBytes);
buffer.put(randomBytes);
rwChannel.write(buffer);
rwChannel.close();

推荐答案


这篇关于在外部存储android文件中编写虚拟内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:04