gpt4 book ai didi

java - 在android中创建文件的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:30 25 4
gpt4 key购买 nike

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

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());

当我从 eclipse DDMS 检查模拟器的文件系统时,我可以看到创建了一个目录“app_themes”。但在里面我看不到 "new_file.png"。日志显示 new_file 不存在。有人可以告诉我问题出在哪里吗?

问候,阿尼斯

最佳答案

试试这个,

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());

但要确定,

public boolean createNewFile () 

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

关于java - 在android中创建文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9697180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com