【问题标题】:failed to save new file with time stamp无法保存带有时间戳的新文件
【发布时间】:2014-01-16 23:50:13
【问题描述】:

我想将文件保存到 sd 卡中,我使用时间戳,所以如果已经有一个同名的文件不会被替换 这是我的代码

public void onClick(View v) {
            // TODO Auto-generated method stub

            boolean mExternalStorageAvailable = false;
            boolean mExternalStorageWriteable = false;
            String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_MOUNTED.equals(state)) {

                mExternalStorageAvailable = mExternalStorageWriteable = true;
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

                mExternalStorageAvailable = true;
                mExternalStorageWriteable = false;
            } else {

                mExternalStorageAvailable = mExternalStorageWriteable = false;
            }

            try {

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'/'HHmm");
                String timestamp = dateFormat.format(new Date());
                String filename = "_" + timestamp + ".txt";

                File myFile = new File(Environment.getExternalStorageDirectory(), "Preposisi Removal/PreposisiRemoval");
                myFile.getParentFile().mkdir();
                myFile.createNewFile();
                FileOutputStream FOut = new FileOutputStream(myFile+filename);
                OutputStreamWriter myOutWriter =
                        new OutputStreamWriter(FOut);
                        myOutWriter.append(tv.getText());
                        myOutWriter.close();
                        FOut.close();
                Toast.makeText(getBaseContext(), "Save Files Successful", Toast.LENGTH_SHORT).show();
            }
            catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });

    button = (Button) findViewById(R.id.button7);
    button.setOnClickListener (this);

}

但是当我运行它时,我收到了 toast 消息“/mnd/sdcard/Preposisi Removal/PreposisiRemoval_20140117/0633.txt: open failed:ENOENT (No such file or directory)

我猜是因为没有文件夹“Preposisi Removal” 那么如果文件夹不存在如何自动创建呢?

或者我的编码有什么问题吗? 每次按下保存按钮我都想保存新文件而不是覆盖以前的文件

【问题讨论】:

    标签: java android eclipse save timestamp


    【解决方案1】:

    先创建目录(如果已经存在则不会报错):

    File directory = new File(Environment.getExternalStorageDirectory(), "Preposisi Removal/");
    directory.mkdirs();
    

    然后在该目录中创建文件(使用带有时间戳和后缀的完整名称):

    File myFile = new File(directory, "PreposisiRemoval" + filename); //Changed code
    //myFile.getParentFile().mkdir();  //No longer required
    myFile.createNewFile();
    FileOutputStream FOut = new FileOutputStream(myFile);  //Changed code
    

    编辑:

    好的,现在我看到了实际问题:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'/'HHmm");
    

    文件名的时间戳部分包含一个“/”。您不能在文件名中使用该字符,因为它表示文件夹或目录。换成其他字符,例如:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'_'HHmm");
    

    您的代码(或我之前建议的更新)应该可以工作。

    【讨论】:

    • 错误先生,得到消息“打开失败:ENOENT(没有这样的文件或目录)
    • SD 卡是否可用,您是否在清单中有 WRITE_EXTERNAL_STORAGE 权限?我看到你在前面的代码中检查了外部存储状态,然后不管结果如何都去保存文件。
    • 我在 android manifest 中有这样的权限 " 和 sd 卡可用,如果检查我的 sd 的代码有问题卡?我这里只是跟着一些教程,我是android编程的初学者,如有错误请指出,我只是想做文件存储的过程
    • 我刚刚注意到实际问题是文件名中的 / 。请看我上面的编辑。
    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2017-01-29
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多