[英]Why do I get a java.io.FileNotFoundException error and a java.lang.NullPointerException in this code?
Here is my code snippet: 这是我的代码段:
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);
}
}
}
I am trying to zip a single file as well as well as a directory with files in it. 我正在尝试压缩单个文件以及其中包含文件的目录。 gzipFile() handles the compression of the single file while zipDirectory() calls a function ListofFiles() for arranging the abstract pathnames in an array. gzipFile()处理单个文件的压缩,而zipDirectory()调用函数ListofFiles()来在数组中排列抽象路径名。 zipDirectory uses ZipEntry to begin writing from the start of the file and positions the start of the zipDirectory使用ZipEntry从文件的开头开始写入,并定位文件的开头。
The error message is 错误消息是
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)
Why is a FileNotFound exception being shown since the program is supposed to create a file file1.gz in the Documents folder. 为什么会显示FileNotFound异常,因为该程序应该在Documents文件夹中创建文件file1.gz。
You shouldn't put extra spaces to the path. 您不应在路径上放置多余的空格。
Try using 尝试使用
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";
instead of 代替
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 ";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.