0

在下面提到的代码中,我在一个名为“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();
4

1 回答 1

0
File root = android.os.Environment.getExternalStorageDirectory();
File[] files = root.listFiles();
for ( File f : files ) {
  if (f.isDirectory()) 
        continue;
  //write here
}
于 2013-06-04T10:01:35.437 回答