【问题标题】:FileOutputStream : FileNotFoundException when attempting to instantiateFileOutputStream :尝试实例化时出现 FileNotFoundException
【发布时间】:2013-10-24 07:46:22
【问题描述】:

试图创建和写入文件,但我每次都得到一个FileNotFoundException,这是我正在使用的方法:

public void saveFileAsPRN(Context context){
    byte[] dataFile = getPrintableFileData();


    String filename = "TestPrn.prn"; 

    // instantiate a file object using the path
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
    Log.e(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());

    //determine if the media is mounted with read & write access
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        Log.e(TAG, "media mounted"); //good
    }else{
        Log.e(TAG, "media NOT mounted"); //bad
    }

    //create directory if it does not exist
    //the default Download directory should always exist
    if (!file.mkdirs()) {
        Log.e(TAG, "Directory not created");
    }

    // determine if the file exists, create it if it does not
    if(!file.exists()){
        try {
            Log.e(TAG, "File does not exist, creating..");
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }else{
        Log.e(TAG, "File Exists");
    }    

    //this makes the blank file visible in the file browser
    MediaScannerConnection.scanFile(context, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + filename}, null, null);

    //create output stream - send data; saving to file
    OutputStream out = null;
    try {           
        FileOutputStream fos = null;
        fos = new FileOutputStream(file); // <---- CRASHES HERE; FileNotFoundException              
        out = new BufferedOutputStream(fos);
        out.write(dataFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}

FileNotFoundException 在以下行引发:

fos = new FileOutputStream(file); // <---- CRASHES HERE;

目录存在,并且在目标目录中创建一个空白文件(在PC上浏览目标文件夹可见)。

File 对象上调用canWrite() 方法返回true - 我有写访问权限。

清单包含:android.permission.WRITE_EXTERNAL_STORAGE"

所以我没有想法,我看到几个人有类似的问题,但我找不到答案。

【问题讨论】:

  • docs.oracle.com/javase/7/docs/api/java/io/… 返回什么?另外,mkdirs() 不会将您的文件创建为目录吗?
  • 检查 reader 是否可以读取,也可以尝试 PrintWriter 将数据写入文件,它可以直接获取 File 对象。

标签: java android io fileoutputstream


【解决方案1】:

注释掉以下代码解决了这个问题:

//create directory if it does not exist
//the default Download directory should always exist
if (!file.mkdirs()) {
    Log.e(TAG, "Directory not created");
}

该代码确实创建了一个空白文件,您可以看到它包含在文件夹中, 但是 - 这非常具有误导性;你不能对这个文件做任何事情,我试着把它从我的设备转移到我的电脑上,我做不到,我也无法打开它。并且你不能在代码中打开一个流。

【讨论】:

【解决方案2】:

试试这个可能对你有帮助。

替换这一行

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), filename);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多