您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Android access "/data/data/

Android access "/data/data/

来源:伴沃教育

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 !

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务