这几天公司项目想拿数据库的数据,但是安卓设备没有root权限,所以自己想了个办法:直接把所有data/data/app下的所有文件全部复制下来了,然后就想看啥数据看啥数据,文件结构如下:
image.png
具体的操作如下:
- 一个复制文件夹的工具类
public static void copyPath(final File oldPath, final File newPath){
if(!oldPath.isDirectory() || !newPath.isDirectory()){
new IllegalArgumentException("File mast is Directory");
}
if(newPath.exists()){
new IllegalArgumentException("newFile not is exists");
}
try{
//io耗时操作,放在子线程
new Thread(new Runnable() {
@Override
public void run() {
copyFiles(oldPath,oldPath.getPath(),newPath.getPath());
}
}).start();
}catch (Exception e){
e.printStackTrace();
}
}
public static void copyFiles(File oldFile,String oldPath,String newPath){
File[] files = oldFile.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getPath());
if(files[i].isDirectory()){
createDirectory(files[i],oldPath,newPath);
File[] fs = files[i].listFiles();
if(fs != null && fs.length != 0){//遍历文件夹的下一层
copyFiles(files[i],oldPath,newPath);
}
}else if(files[i].isFile()){
copyFile(files[i],oldPath,newPath);
}
}
}
public static void copyFile(File file,String oldPath,String newPath){
String path = file.getPath();
String nPath = path.replace(oldPath, newPath);
File nfile = new File(nPath);
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(nfile);
byte[] bs = new byte[1024];
int len = -1;
while((len = in.read(bs))!= -1){
out.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void createDirectory(File dir,String oldPath,String newPath){
String path = dir.getPath();
String nPath = path.replace(oldPath, newPath);
File file = new File(nPath);
file.mkdirs();
}
- 然后调用就可以了
File f = new File(Environment.getDataDirectory().getPath()+"/data/"+this.getPackageName() + "/");
FileUtil.copyPath(f,Environment.getExternalStorageDirectory());
3.sql数据库结构可以参照这篇博客sqllite的数据结构
网友评论