在外部存储android文件中编写虚拟内容 [英] Writing dummy contents in files of external storage android

查看:98
本文介绍了在外部存储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();

推荐答案

你好,



您可以使用 File.createTempFile(字符串前缀,字符串后缀,文件目录) API在指定目录中生成随机文件。



问候,
Hello,

You can use File.createTempFile(String prefix, String suffix, File directory) API to generate a random file in the specified directory.

Regards,


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆