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因为没有定义“不起作用”