Java FileOutputStream创建的文件未解锁

Java FileOutputStream创建的文件未解锁,java,file-io,Java,File Io,目前,我的代码中的FileOutputStream存在问题。我使用FileOutputStream在磁盘中创建文件。创建文件后,无法打开、删除或移动文件,当停止web服务器时,获取错误消息已被其他用户锁定。它工作正常。如何解决此问题 private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) { try {

目前,我的代码中的FileOutputStream存在问题。我使用FileOutputStream在磁盘中创建文件。创建文件后,无法打开、删除或移动文件,当停止web服务器时,获取错误消息已被其他用户锁定。它工作正常。如何解决此问题

private void writeToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {

            try {
                OutputStream out = new FileOutputStream(new File(
                        uploadedFileLocation));
                int read = 0;
                byte[] bytes = new byte[1024];

                out = new FileOutputStream(new File(uploadedFileLocation));
                while ((read = uploadedInputStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
                out.close();

            } catch (IOException e) {

                e.printStackTrace();
            }

        }

您正在创建两个
FileOutputStream
实例,并将这两个实例分配给
out
,但仅关闭一个实例

删除第二个
out=newfileoutputstream(新文件(uploadedFileLocation))

也应考虑使用资源块

的尝试。
private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    try (OutputStream out = new FileOutputStream(new File(uploadedFileLocation))) {
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}
最后
阻塞以确保流关闭

private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    OutputStream out = null;
    try {
        out = new FileOutputStream(new File(uploadedFileLocation))
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException exp) {            
            }
        }
    }

}

有关更多详细信息,请参见,您正在创建两个
FileOutputStream
实例,并将这两个实例分配给
out
,但仅关闭一个实例

删除第二个
out=newfileoutputstream(新文件(uploadedFileLocation))

也应考虑使用资源块

的尝试。
private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    try (OutputStream out = new FileOutputStream(new File(uploadedFileLocation))) {
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}
最后
阻塞以确保流关闭

private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    OutputStream out = null;
    try {
        out = new FileOutputStream(new File(uploadedFileLocation))
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException exp) {            
            }
        }
    }

}

有关更多详细信息,请参见,您正在创建两个
FileOutputStream
实例,并将这两个实例分配给
out
,但仅关闭一个实例

删除第二个
out=newfileoutputstream(新文件(uploadedFileLocation))

也应考虑使用资源块

的尝试。
private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    try (OutputStream out = new FileOutputStream(new File(uploadedFileLocation))) {
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}
最后
阻塞以确保流关闭

private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    OutputStream out = null;
    try {
        out = new FileOutputStream(new File(uploadedFileLocation))
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException exp) {            
            }
        }
    }

}

有关更多详细信息,请参见,您正在创建两个
FileOutputStream
实例,并将这两个实例分配给
out
,但仅关闭一个实例

删除第二个
out=newfileoutputstream(新文件(uploadedFileLocation))

也应考虑使用资源块

的尝试。
private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    try (OutputStream out = new FileOutputStream(new File(uploadedFileLocation))) {
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}
最后
阻塞以确保流关闭

private void writeToFile(InputStream uploadedInputStream,
    String uploadedFileLocation) {

    OutputStream out = null;
    try {
        out = new FileOutputStream(new File(uploadedFileLocation))
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

    } catch (IOException e) {

        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException exp) {            
            }
        }
    }

}

有关更多详细信息,请参见

您正在分配
输出
两个不同的
文件输出流
为什么分配两次?如果发生异常,为什么不刷新并最终关闭输入和输出流呢?程序员:两个“输出”有何不同?相同的对象名。。但一个是没有冲水,没有error@MidhunPottammal您创建了一个
FileOutputStream
的实例,从不关闭它,然后用另一个
FileOutputStream
的实例替换它并关闭它,这将使一个流保持打开状态…无法保证对象会被GC,特别是当它保持本机资源打开时,您永远不应该依赖于您正在分配
out
两个不同的
FileOutputStream
为什么要分配两次?如果发生异常,为什么不刷新并最终关闭输入和输出流呢?程序员:两个“输出”有何不同?相同的对象名。。但一个是没有冲水,没有error@MidhunPottammal您创建了一个
FileOutputStream
的实例,从不关闭它,然后用另一个
FileOutputStream
的实例替换它并关闭它,这将使一个流保持打开状态…无法保证对象会被GC,特别是当它保持本机资源打开时,您永远不应该依赖于您正在分配
out
两个不同的
FileOutputStream
为什么要分配两次?如果发生异常,为什么不刷新并最终关闭输入和输出流呢?程序员:两个“输出”有何不同?相同的对象名。。但一个是没有冲水,没有error@MidhunPottammal您创建了一个
FileOutputStream
的实例,从不关闭它,然后用另一个
FileOutputStream
的实例替换它并关闭它,这将使一个流保持打开状态…无法保证对象会被GC,特别是当它保持本机资源打开时,您永远不应该依赖于您正在分配
out
两个不同的
FileOutputStream
为什么要分配两次?如果发生异常,为什么不刷新并最终关闭输入和输出流呢?程序员:两个“输出”有何不同?相同的对象名。。但一个是没有冲水,没有error@MidhunPottammal您创建了一个
FileOutputStream
的实例,从不关闭它,然后用另一个
FileOutputStream
的实例替换它并关闭它,这将使一个流保持打开状态…无法保证对象会被GC,特别是当它保持本地资源开放时,你永远不应该依赖它