Android access "/data/data/packageName/" folder

Sometimes in android test we need to write to "/data/data/packageName/" folder .

Context mContext = InstrumentationRegistry.getTargetContext();
File textFile = new File(mContext.getApplicationInfo().dataDir + File.separator + "textFile.txt");
if (textFile.createNewFile()){
    Log.i(TAG, "testMethods: Create text file succeed .");
} else {
    Log.w(TAG, "testMethods: Fail to create text file !!!");
}

When declaring this :

mContext.getApplicationInfo().dataDir

it shows :

mContext.getApplicationInfo().dataDir : /data/user/0/com.mrvl.mypackage

but

xxx@xxx:/data/data/com.mrvl.mypackage # ls -al
-rw------- u0_a52   u0_a52          0 2017-08-08 03:45 textFile.txt

So, after some searching , I found this :

/data/user was added in Jelly Bean as part of multi-user support.
 Each user on the device gets a directory in there named after their user ID,
 and that directory contains each app's data directory for that user. /data/user/0 is a symlink to /data/data.
A symbolic link, also termed a soft link, is a special kind of file that points to another file,
 much like a shortcut in Windows or a Macintosh alias. 
Unlike a hard link, a symbolic link does not contain the data in the target file.
 It simply points to another entry somewhere in the file system. 
This difference gives symbolic links certain qualities that hard links do not have,
 such as the ability to link to directories, 
or to files on remote computers networked through [NFS](https://kb.iu.edu/d/adux). 
Also, when you delete a target file, symbolic links to that file become unusable, 
whereas hard links preserve the contents of the file.

Problem solved !

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 韩国导演李宋喜一作品。网上没找到资源,所以略过了导演前期的作品。《夜间飞行》很压抑,压抑得我又得忧郁好几天了。 校...
    freantx阅读 4,558评论 0 4
  • 午后的阳光,静好。 操场旁边的芒果树,正陪着它的孩子享受着这静好的阳光。 我思索着,走在校...
    大圣老师阅读 5,596评论 1 9
  • 上海今天下大雨了。 早晨还在睡梦中的麋鹿小姐,迷迷糊糊听到了雨打窗台的声音,于是她出于原始本能地判断这又是个发霉的...
    鹿马厨房阅读 2,701评论 0 0
  • 上周看了两篇文章,对我的触动很大。 第一篇出自心乐土的“父母必须面对现实:你的孩子终将平凡”,文章提出了一个尖锐的...
    AllenDUAN2017阅读 2,555评论 0 1