gpt4 book ai didi

java FileNotFoundException 打开的文件太多

转载 作者:搜寻专家 更新时间:2023-11-01 03:12:44 25 4
gpt4 key购买 nike

我正在尝试用 Java 构建一个自动备份脚本。虽然我不太擅长 Java,但事实证明这很困难。

这是我的代码:

package javatest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
public static void main(String[] args) throws Exception {
String path = "/mnt/192.168.1.89";
String destFile = "/home/scott/backup.zip";

zip(path,destFile);
}

private static void zip(String src, String destFile) throws Exception
{
FileOutputStream fileWriter = new FileOutputStream(destFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter);

addFolderToZip("", src, zip);

zip.flush();
zip.close();
}

private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception
{
File folder = new File(srcFolder);

for (String filename : folder.list())
{
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + filename, zip);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + filename, zip);
}
}
}

private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception
{
File folder = new File(srcFile);

if (folder.isDirectory()) {
addFolderToZip(path,srcFile,zip);
} else {
byte[] buf = new byte[1024];

int len;

FileInputStream in = new FileInputStream(srcFile);

zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));

while((len = in.read(buf)) > 0)
{
zip.write(buf,0,len);
}
}
}
}

这里是异常(exception):

Exception in thread "main" java.io.FileNotFoundException: FILENAME (Too many open files)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at javatest.Main.addFileToZip(Main.java:53)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:37)
at javatest.Main.addFileToZip(Main.java:47)
at javatest.Main.addFolderToZip(Main.java:35)
at javatest.Main.zip(Main.java:22)
at javatest.Main.main(Main.java:14)
Java Result: 1

最佳答案

确保你close()你的:

  • ZipOutputStream(您这样做是对的),以及
  • 文件输入流

现在,您的流中有许多打开的连接。

addFolderToZip() 方法中,像这样在最后关闭你的 FileInputStream:

try {
in.close();
} catch (IOException e) {
//Log exception
}

关于java FileNotFoundException 打开的文件太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6268112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com