FileOutputStream写入内容

app包名下创建了一个TXT,想使用FileOutputStream往里写入数据,可以确定文件创建成功了,安卓权限也开了,但一直写不进去内容,也不报错,望高手提示!

  String fileName = "abcd.txt";
        String fileDir = CacheUtils.getCacheDirectory(mContext, fileName);
        Log.e(TAG, "~~" + fileDir);

        File file = new File(fileDir);
        try {
            FileOutputStream fos = new FileOutputStream(file);

            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            fos.write("asd23".getBytes());
            fos.flush();
            fos.close();

        } catch (IOException e1) {
            e1.printStackTrace();
        }

你确定代码执行过程中没有异常的话,试试刷新下你写入的文件目录 。

我测试了,去掉String fileDir = CacheUtils.getCacheDirectory(mContext, fileName);这行代码,直接指定我的文件路径为某个路径的话,就能
真确生成一个文件并且内容是写入的内容。
所以,我推测应该就是这行代码的问题。你日志打印下这个fileDir的值是多少呢?

建议你debug调试下,看看具体是什么地方有问题。如果你的手机是android5.0以上的话还要在手机设置应用里面设置应用允许写的权限