当前位置: 代码迷 >> Android >> 怎么写文件到/data/data/packagename/files中
  详细解决方案

怎么写文件到/data/data/packagename/files中

热度:84   发布时间:2016-05-01 21:18:31.0
如何写文件到/data/data/packagename/files中
不好用openfileout...

其他有什么方法么?

普通的创建文件直接报 open failed: EACCES (Permission denied) 的异常。

麻烦帮帮忙!

------解决方案--------------------
FileOutputStream os = ctx.openFileOutput(file.getName(),Context.MODE_WORLD_WRITEABLE);
------解决方案--------------------
Java code
if (!file.exists()) {                    file.createNewFile();                }                FileOutputStream os = ctx.openFileOutput(file.getName(),                        Context.MODE_WORLD_WRITEABLE);                byte[] bytes = new byte[512];                int i = -1;                while ((i = is.read(bytes)) > 0) {                    os.write(bytes);                }                os.close();                is.close();                String permission = "666";                try {                    String command = "chmod " + permission + " " + apkPath                            + "/" + destapkName;                    Runtime runtime = Runtime.getRuntime();                    runtime.exec(command);                } catch (IOException e) {                    e.printStackTrace();                }
------解决方案--------------------
如果packagename,不是你当前调用createnewfile的应用的包名,是没有权限的
------解决方案--------------------
我看了一下,/data/data/packagename/files中files的权限是700,system system ,证明只有system权限才有在里面创建文件的权限,你如果是在想要在里面创建文件的话需要手动修改files文件的权限。
------解决方案--------------------
Java code
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.os.Bundle;public class FileTestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //创建输入输出流对象        FileOutputStream outfile = null;        FileInputStream infile = null;        try {            //创建openFile的两个对象 openFileOutput()的两个参数            //第一个是文件名,第二个是属性,0代表只允许应用程序操作这个文件            outfile = this.openFileOutput("test.txt", 0);            infile = this.openFileInput("test.txt");        } catch (FileNotFoundException e1) {            // TODO Auto-generated catch block            e1.printStackTrace();        }        DataOutputStream dout = null ;        try {            //写文件             dout = new DataOutputStream(outfile );            dout.writeUTF("testtesttesttesttesttest");            dout.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        DataInputStream din = null;        //读文件        din = new DataInputStream(infile);        String result = null;        try {            result = din.readLine();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println(result);    }}
  相关解决方案