美文网首页
file.createNewFile() 报错

file.createNewFile() 报错

作者: feng_wy | 来源:发表于2019-08-28 14:03 被阅读0次

1.在创建文件的时候报错,路径为多文件

            java.util.Date date = new java.util.Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
            String strRecordFile = Environment.getExternalStorageDirectory().getPath() + "/DCIM/ScreenRecorder/" + simpleDateFormat.format(date) + "-start" + ".mp4";
            File file = new File(strRecordFile);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

2.解决方法,先创建文件夹,再创建文件

            String path1 = Environment.getExternalStorageDirectory().getPath() + "/DCIM/ScreenRecorder/";
            File pathfile=new File(path1);
            if (!pathfile.exists()){
                pathfile.mkdirs();
            }

相关文章

网友评论

      本文标题:file.createNewFile() 报错

      本文链接:https://www.haomeiwen.com/subject/ambpectx.html