1

In the below mentioned code I am writing dummy data to all files present in external storage and renaming to "abc.txt". But what I want is to write dummy data to only that file which I select and rename that particular file. How do I do?

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);
} 
neo108
  • 5,156
  • 3
  • 27
  • 41
  • Select the file that you want to rename using the file name - `getName()` and then rename it. – neo108 Jun 05 '13 at 05:27
  • what I want is, to rewrite the contents of each and every file of external storage and then rename them. My first preference to rewriting the contents of a file to dummy data. – user2435558 Jun 05 '13 at 05:30
  • Have a look at [this tutorial](http://www.vogella.com/articles/JavaIO/article.html) for how to read and write files. – neo108 Jun 05 '13 at 05:35
  • what I want is, to rewrite the contents of each and every file of external storage to zero and then rename them. My first preference to rewriting the contents of a file to zeros. – user2435558 Jun 05 '13 at 05:39

0 Answers0