如何在文件目录的子目录下创建具有世界可读权限的文件 [英] how to create a file with world readable permission under subdirectory of files directory

查看:45
本文介绍了如何在文件目录的子目录下创建具有世界可读权限的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序中使用全局权限在 myapp/files/subdir 下创建文件.我这样做是因为我使用外部应用程序打开一些文件使用这个

I need to create files under myapp/files/subdir with global permission in my application. I do this because I use external applications to open some files Using this

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

仅在文件夹下创建文件.使用

creates file only under files folder. Using

    File dir=new File(Constants.TASK_DIRECTORY);
    dir.mkdirs();
    File file=new File(dir, FILENAME);         
    file.createNewFile(); FileOutputStream fos=new FileOutputStream(file);

在子目录下创建文件,但具有私有权限.我需要找到一种方法来组合它们以在子目录中创建一个文件以供全世界阅读

creates files under subdirectories but with private permissions. I need to find a way to compose those both to create a file in a subdirectory to be world readable

我一直在尝试很多东西,但没有一个对我有帮助,这是我最长时间未回答的问题

I have been trying a lot of things but none helped me and this was the longest time unanswered question of mine

推荐答案

我知道这是一个老问题,但这是正确的方法

I know this is an old question, but here is the correct way

public void makeFileWorldReadable(String path)
{
    File f = new File(path);

    if(!f.exists()) // Create the file if it does not exist.
        f.createNewFile();
    f.setReadable(true, false);
}

这篇关于如何在文件目录的子目录下创建具有世界可读权限的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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