Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用时间戳保存文件_Android_Timestamp - Fatal编程技术网

Android 使用时间戳保存文件

Android 使用时间戳保存文件,android,timestamp,Android,Timestamp,我正在尝试使用时间戳作为名称保存文件。我可以保存文件,当我自己命名时没有问题,但是当我尝试使用时间戳时,它不起作用。这是我的代码: Long tsLong = System.currentTimeMillis()/1000; String ts = tsLong.toString(); File newxmlfile = new File(Environment.getExternalStorageDirectory()

我正在尝试使用时间戳作为名称保存文件。我可以保存文件,当我自己命名时没有问题,但是当我尝试使用时间戳时,它不起作用。这是我的代码:

        Long tsLong = System.currentTimeMillis()/1000;
        String ts = tsLong.toString();

        File newxmlfile = new File(Environment.getExternalStorageDirectory()
                 + ts);
        try {
            newxmlfile.createNewFile();
        } catch (IOException e) {
            Log.e("IOException", "exception in createNewFile() method");
        }

        FileOutputStream fileos = null;
        try {
            fileos = new FileOutputStream(newxmlfile);
        } catch (FileNotFoundException e) {
            Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
有人知道怎么做吗

编辑(已解决):我更改了下面的行,它使用时间戳将文件保存为xml文件

File newxmlfile = new File(Environment.getExternalStorageDirectory()
                 ,ts + ".xml");

我认为您创建的文件路径无效

进行字符串浓缩时:

Environment.getExternalStorageDirectory() + ts
。。。将时间戳
123456
添加到文件路径(类似于)
/mnt/sdcard
。最终会得到一条无效路径,如:

/mnt/sdcard14571747181
(您没有写入该文件的权限,因为它不在外部目录中。)

您可以自己添加文件分隔符,也可以使用以下内容创建文件:

File newxmlfile = new File(Environment.getExternalStorageDirectory(), ts);
                                                                    ^^
                                                                the change

我认为您创建的文件路径无效

进行字符串浓缩时:

Environment.getExternalStorageDirectory() + ts
。。。将时间戳
123456
添加到文件路径(类似于)
/mnt/sdcard
。最终会得到一条无效路径,如:

/mnt/sdcard14571747181
(您没有写入该文件的权限,因为它不在外部目录中。)

您可以自己添加文件分隔符,也可以使用以下内容创建文件:

File newxmlfile = new File(Environment.getExternalStorageDirectory(), ts);
                                                                    ^^
                                                                the change

你犯了哪个错误?-1因为没有定义“不起作用”你犯了哪个错误?-1因为没有定义“不起作用”