Java FileOutputStream:尝试实例化时FileNotFoundException

Java FileOutputStream:尝试实例化时FileNotFoundException,java,android,io,fileoutputstream,Java,Android,Io,Fileoutputstream,试图创建并写入文件,但每次都会出现FileNotFoundException,下面是我使用的方法: public void saveFileAsPRN(Context context){ byte[] dataFile = getPrintableFileData(); String filename = "TestPrn.prn"; // instantiate a file object using the path File file = new F

试图创建并写入文件,但每次都会出现
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();
        }
    }   
}
该目录存在,并在目标目录中创建一个空白文件(通过浏览PC上的目标文件夹可见)

调用
文件
对象上的方法
canWrite()
返回true-我有写访问权

清单包含:
android.permission.WRITE\u EXTERNAL\u STORAGE


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

评论下面的代码修复了这个问题:

//create directory if it does not exist
//the default Download directory should always exist
if (!file.mkdirs()) {
    Log.e(TAG, "Directory not created");
}
该代码确实创建了一个空白文件,您可以看到它包含在文件夹中,
但是-这是非常误导的;你不能用这个文件做任何事情,我试着将它从我的设备传输到我的电脑,但是我不能,我也不能打开它。而且你不能用代码打开它的流。

试试这可能会对你有所帮助

更换这条线

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


返回什么?另外,不会将文件创建为目录吗?请检查读卡器如果您能够读取它并尝试PrintWriter将数据写入文件,它可以直接获取文件对象。这是因为
新建文件(“hello/world”)。mkdirs()将生成两个目录。请参阅
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), filename);