安卓:创建使用createNewFile文件()方法 [英] Android: Creating file using createNewFile() method

查看:634
本文介绍了安卓:创建使用createNewFile文件()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Android文档中所述,要创建一个将设在应用程序的目录中的新文件没有在上下文类中的方法: openFileOutput ()

但是,在会如果我用简单的进行文件位于 createNewFile()方法文件类。<? / p>

解决方案

CreateNewFile()使用这样的:

 文件文件=新的文件(数据/数据​​/你的包名/的test.txt);
如果(!file.exists()){
        尝试{
            file.createNewFile();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
}

所以,你会告诉它应该被创建的文件。请记住,你只能在你的包中创建新文件。这就是数据/数据​​/你的包名/\".

As described in android documentation, to create a new file which will be situated in the application's directory there is the method in Context class: openFileOutput().

But where will be the file situated if I use simple createNewFile() method from File class.?

解决方案

CreateNewFile() is used like this:

File file = new File("data/data/your package name/test.txt");
if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

So you will tell the file where it should be created. Remember that you can only create new files on your package. That is "data/data/your package name/".

这篇关于安卓:创建使用createNewFile文件()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆