我试图使用时间戳作为名称保存文件.我自己命名时可以保存文件没问题但是当我尝试使用时间戳时它不起作用.这是我的代码:
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");
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?
编辑(已解决):我更改了下面的行,并使用时间戳作为xml文件保存文件.
File newxmlfile = new File(Environment.getExternalStorageDirectory()
,ts + ".xml");
Run Code Online (Sandbox Code Playgroud)
我认为你创建的文件路径无效.
当你做字符串连接时:
Environment.getExternalStorageDirectory() + ts
Run Code Online (Sandbox Code Playgroud)
...您将时间戳添加123456到文件路径(类似)/mnt/sdcard.你最终会得到一条无效的路径:
/mnt/sdcard14571747181
Run Code Online (Sandbox Code Playgroud)
(而且你没有权限写入该文件,因为它不在外部目录中.)
您自己添加文件分隔符,或者使用以下命令创建文件:
File newxmlfile = new File(Environment.getExternalStorageDirectory(), ts);
^^
the change
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
906 次 |
| 最近记录: |