1

在下面提到的代码中,我将虚拟数据写入外部存储中存在的所有文件并重命名为“abc.txt”。但我想要的是仅将虚拟数据写入我选择并重命名该特定文件的那个文件。我该怎么办?

File[] files = root.listFiles();
for ( File f : files ) {
    if (f.isDirectory()) 
        continue;
    FileChannel rwChannel = new RandomAccessFile(f, "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(); 
}
File root1 = android.os.Environment.getExternalStorageDirectory();
File[] files1 = root.listFiles();
for ( File f : files ) {
    if (f.isDirectory()) 
        continue;
    File myFile1 = new File(root,"abc.txt");
    f.renameTo(myFile1);
} 
4

0 回答 0