在外部存储android文件中编写虚拟内容

时间:2013-06-04 09:57:06

标签: android file dummy-data android-external-storage

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

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();

1 个答案:

答案 0 :(得分:0)

File root = android.os.Environment.getExternalStorageDirectory();
File[] files = root.listFiles();
for ( File f : files ) {
  if (f.isDirectory()) 
        continue;
  //write here
}