file.createNewFile() 报错

2019-08-28  本文已影响0人  feng_wy

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();
            }
上一篇 下一篇

猜你喜欢

热点阅读