为什么我在此代码中遇到java.io.FileNotFoundException错误和java.lang.NullPointerException?

时间:2016-03-19 10:49:46

标签: java

这是我的代码段:

public class Compress {
    List<String> filesinDir= new ArrayList<String>();

public static void main(String[] args){

   Compress c= new Compress();
   c.gzipFile();
   String OUTPUT_DIR= "C:\\Users\\Surya's\\Documents\\tmp.zip";
   File dir= new File(" C:\\Users\\Surya's\\Documents\\tmp ");
   c.zipDirectory(dir, OUTPUT_DIR);
 }

public void gzipFile(){

   String OUTPUT_GZIP_FILE= " C:\\Users\\Surya's\\Documents\\file1.gz ";
   String SOURCE_FILE= " C:\\Users\\Surya's\\Documents\\file1.txt ";
   byte[] b= new byte[1024];
   int len;

   try{
   FileOutputStream fos= new FileOutputStream(OUTPUT_GZIP_FILE);
   GZIPOutputStream gz= new GZIPOutputStream(fos);
   FileInputStream in= new FileInputStream(SOURCE_FILE);
      while((len= in.read(b))!= -1){
          gz.write(b, 0, len);
      }
      fos.close();
      in.close();
      gz.finish();
      gz.close();
   }

   catch(IOException e){
       e.printStackTrace();
   }
}

public void zipDirectory(File dir, String OUTPUT_DIR){
   try{
       ListofFiles(dir);
       FileOutputStream of= new FileOutputStream(OUTPUT_DIR);
       ZipOutputStream gzdir= new ZipOutputStream(of);
       for(String filepath : filesinDir){
           ZipEntry ze= new     ZipEntry(filepath.substring(dir.getAbsolutePath().length()+1, filepath.length()));
           gzdir.putNextEntry(ze);
           byte[] b= new byte[1024];
           int len;
           FileInputStream fi= new FileInputStream(filepath);
           while((len=fi.read(b))!=-1){
               of.write(b, 0, len);
           }
           gzdir.closeEntry();
           fi.close();
       }
       gzdir.close();
       of.close();
   }
   catch(IOException e){
       e.printStackTrace();
   }
}

public  void ListofFiles(File dir) throws IOException{
   File[] files= dir.listFiles();
   for(File file : files){
       if(file.isFile()) filesinDir.add(file.getAbsolutePath());
       else ListofFiles(file);
    }
  }
}

我正在尝试压缩单个文件以及包含文件的目录。 gzipFile()处理单个文件的压缩,而zipDirectory()调用函数ListofFiles()来排列数组中的抽象路径名。 zipDirectory使用ZipEntry从文件的开头开始写入并定位

的开头

错误消息是

java.io.FileNotFoundException:  C:\Users\Surya's\Documents\file1.gz  (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at com.assignment.java.Compress.gzipFile(Compress.java:29)
at com.assignment.java.Compress.main(Compress.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.assignment.java.Compress.ListofFiles(Compress.java:73)
at com.assignment.java.Compress.zipDirectory(Compress.java:48)
at com.assignment.java.Compress.main(Compress.java:18)

为什么要显示FileNotFound异常,因为程序应该在Documents文件夹中创建文件file1.gz。

1 个答案:

答案 0 :(得分:2)

你不应该在路径上留出额外的空间。

尝试使用

String OUTPUT_DIR= "C:\\Users\\Surya's\\Documents\\tmp.zip";
File dir= new File("C:\\Users\\Surya's\\Documents\\tmp");

String OUTPUT_GZIP_FILE= "C:\\Users\\Surya's\\Documents\\file1.gz";
String SOURCE_FILE= "C:\\Users\\Surya's\\Documents\\file1.txt";

而不是

String OUTPUT_DIR= "C:\\Users\\Surya's\\Documents\\tmp.zip";
File dir= new File(" C:\\Users\\Surya's\\Documents\\tmp ");

String OUTPUT_GZIP_FILE= " C:\\Users\\Surya's\\Documents\\file1.gz ";
String SOURCE_FILE= " C:\\Users\\Surya's\\Documents\\file1.txt ";