开发者

java FileNotFoundException Too many open files

I'm trying to build an automatic backup script in Java. I'm not very good at Java though so this is proving difficult.

Here's my code:

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);
          }
     }
 }
}

And here's the 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.a开发者_如何学CddFileToZip(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


Make sure you close() both your:

  • ZipOutputStream (which you rightly did so), and
  • FileInputStream

Right now, there's many open connections in your streams.

in addFolderToZip() method, close your FileInputStream at the end like so:

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


close those streams best practice is to do it in try-finally blocks like so (finally blocks are nearly always executed when the try block exits

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

     addFolderToZip("", src, zip);

     zip.flush();
   }finally{
     if(zip!=null)zip.close();//close also does a flush
     if(fileWriter!=null)fileWriter.close();
   }
 }

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 = null;
          try{
              in = new FileInputStream(srcFile);
              zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));

              while((len = in.read(buf)) > 0)
              {
                   zip.write(buf,0,len);
              }
          }finally{if(in!=null)in.close()}//<<--ensure in is closed you forgot this in the example code
     }
 }


FileInputStream in = new FileInputStream(srcFile);

zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while((len = in.read(buf)) > 0)
{
    zip.write(buf,0,len);
}
in.close(); //You have to close file !

You have to close file when you don't need it anymore


In addFileToZip, just do in.close() at the end of the else, it should do the trick. Of course, proper code would also check Exceptions in order to close streams even in case of problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜