Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java与Android在删除FileInputStream打开的文件方面的比较_Java_Android - Fatal编程技术网

Java与Android在删除FileInputStream打开的文件方面的比较

Java与Android在删除FileInputStream打开的文件方面的比较,java,android,Java,Android,我有一个代码片段,它将通过输入/输出流打开本地文件,但它将休眠或等待片刻(此时,流仍处于打开状态,而不是关闭状态),而我将通过另一个文件管理程序删除该文件。Java和Android的行为似乎有所不同 Java:无法删除该文件,因为另一个程序正在访问它-FileInputStream仍然未关闭 File f = new File("D:\\" + File.separator + "testfile.txt"); try { f.createNewFile();

我有一个代码片段,它将通过输入/输出流打开本地文件,但它将休眠或等待片刻(此时,流仍处于打开状态,而不是关闭状态),而我将通过另一个文件管理程序删除该文件。Java和Android的行为似乎有所不同

Java:无法删除该文件,因为另一个程序正在访问它-FileInputStream仍然未关闭

   File f = new File("D:\\" + File.separator + "testfile.txt");
   try {
        f.createNewFile();
        FileInputStream fis = new FileInputStream(f);
        // Still not close
        try {
            Thread.sleep(60000); // At this time I will use another program to delete this file
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "testfile.txt");
    try {
        f.createNewFile();
        FileInputStream fis = new FileInputStream(f);
        // Still not close
        try {
            Thread.sleep(60000); // At this time I will use another program to delete this file
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
Android:该文件可以正常删除-尽管FileInputStream仍然没有关闭

   File f = new File("D:\\" + File.separator + "testfile.txt");
   try {
        f.createNewFile();
        FileInputStream fis = new FileInputStream(f);
        // Still not close
        try {
            Thread.sleep(60000); // At this time I will use another program to delete this file
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "testfile.txt");
    try {
        f.createNewFile();
        FileInputStream fis = new FileInputStream(f);
        // Still not close
        try {
            Thread.sleep(60000); // At this time I will use another program to delete this file
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

有谁能向我解释一下为什么这些平台之间存在差异?我只知道android重用java sdk,可能是他们的行为在从java迁移到android时被修改了?或者另一个原因……?

正如m0skit0所指出的,这不是Java vs。Java@Android差别。如果您在Linux上尝试Android示例,您将得到相同的结果

当涉及到文件操作时,Java依赖于底层操作系统的行为,这就是Windows(您的第一个示例)和Linux的不同之处(至少与其对应的默认/标准文件系统不同)

有谁能向我解释一下为什么这些平台之间存在差异

这实际上是Linux(以及Android和MacOSX)1与Windows之间的区别

  • Linux等通过“取消链接”文件来实现文件删除,并且只有在关闭所有文件句柄后才实际删除文件

  • 在Windows中(至少在历史上),删除一个文件会将其删除,因此删除一个打开的文件是可以实现的。这种情况已经改变,但对删除的限制仍然存在。(请参阅MSDN中的页面。)



1-Linux、Android和Mac OSX操作系统的设计都源于经典的UNIX,而UNIX至少从版本6开始就使用“取消链接”语义来删除文件。Windows最终源于MS/DOS。

Java和Android不是一个有效的比较。Android是一个操作系统,Java不是。