Android:如何写入内存

时间:2013-02-02 10:01:36

标签: android io

我让这段代码写在sdcard上,现在我怎样才能将它转换为写入内部存储器?

//private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/liste.txt"; Path used to write on sdcard

try{
   File file = new File(path);

   BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
   bw.write("something");
    bw.flush();
    bw.close();
catch(Exception e) {...}

3 个答案:

答案 0 :(得分:1)

在android中,每个应用程序在/data/data/package/

下都有自己的文件夹

此dir只能由您的应用访问,如果设备已植根,则

要访问此目录并对其进行读/写,您可以使用:

getApplicationContext().openFileOutput(name, mode);

getApplicationContext().openFileInput(name);

有关此内容的更多信息:Docs

答案 1 :(得分:0)

使用此代码:

FileOutputStream fos;
    fos = context.openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);


    fos.write(data.getBytes()); //write to application top level directory for immediate sending 
    fos.close();

答案 2 :(得分:0)

try{
File file = new File("/data/data/com.example.packagename/files/");

BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}

//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext(); 

filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath);