在android中创建文件的问题

use*_*953 6 java android android-emulator android-intent android-layout

我正在尝试使用以下代码在目录中创建一个文件:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("themes", Context.MODE_WORLD_WRITEABLE);
    Log.d("Create File", "Directory path"+directory.getAbsolutePath());
    File new_file =new File(directory.getAbsolutePath() + File.separator +  "new_file.png");
    Log.d("Create File", "File exists?"+new_file.exists());
Run Code Online (Sandbox Code Playgroud)

当我从eclipse DDMS检查模拟器的文件系统时,我可以看到创建了一个目录"app_themes".但在里面,我看不到"new_file.png".Log说new_file不存在.有人可以告诉我这是什么问题吗?

此致,Anees

use*_*305 14

试试这个,

File new_file =new File(directory.getAbsolutePath() + File.separator +  "new_file.png");
try
  {
   new_file.createNewFile();
  }
  catch (IOException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
Log.d("Create File", "File exists?"+new_file.exists());
Run Code Online (Sandbox Code Playgroud)

但是要确定,

public boolean createNewFile () 
Run Code Online (Sandbox Code Playgroud)

根据存储在此文件中的路径信息在文件系统上创建一个新的空文件.如果创建文件,则此方法返回true;如果文件已存在,则返回false.请注意,即使文件不是文件,它也会返回false(因为它是一个目录,比方说).