【问题标题】:Putting date as xml file name将日期作为 xml 文件名
【发布时间】:2014-03-19 14:44:45
【问题描述】:

我想创建一个xml文件并将日期作为名称

//我在这里新建一个名为Date.xml的xml文件

Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
String Date= df.format(c.getTime());
File newxmlfile = new File(Environment.getExternalStorageDirectory()+ "/"+Date+".xml");

try {
Log.v("CreateXML", "create file:" + 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 文件未创建,我收到该错误

E/(12438): 19-mars-2014 05:06:19 E/IOException(12438):createNewFile() 方法中的异常 E/FileNotFoundException(12438): 无法创建 FileOutputStream E/Exception(12438): 创建 xml 文件时出错

【问题讨论】:

    标签: android xml


    【解决方案1】:

    只需使用Date 而不是当前文件名"new"

    例如:new File(Environment.getExternalStorageDirectory()+ "/"+Date+".xml");

    【讨论】:

    • 这不是答案。
    • 我已经试过你所说的@D.R.但无法创建xml文件
    • 那么请给我们具体的错误信息。可能: 不允许在文件系统的文件名中使用。相应地调整您的日期格式。
    • 正如我所说,从日期格式中删除 :。很可能它们在您的文件系统上是不允许的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多