Regarding zip files in java [modified]
-
Hi , ANYONE LET ME KNOW WHAT IS GOING WRONG HER, I AM TRYING TO CREATE ZIP FILE IN JAVA, BUE IAM GETTING THE ERROR AS FALLOWS, GO THRU THE CODE ALSO WHAT I WROTE import java.util.zip.*;import java.io.*; public class ZipFileExample { public static void main(String args[])throws IOException { FileInputStream fis=new FileInputStream("Allpaths.txt"); FileOutputStream fos=new FileOutputStream("kkk.txt"); DeflaterOutputStream dos=new DeflaterOutputStream(fos); int data; while((data=fis.read())!=-1){ dos.write(data); fis.close(); dos.close(); } } } eRROR: Exception in thread "main" java.io.FileNotFoundException: Allpaths.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at ZipFileExample.main(ZipFileExample.java:4)
modified on Thursday, July 16, 2009 11:06 PM
-
Hi , ANYONE LET ME KNOW WHAT IS GOING WRONG HER, I AM TRYING TO CREATE ZIP FILE IN JAVA, BUE IAM GETTING THE ERROR AS FALLOWS, GO THRU THE CODE ALSO WHAT I WROTE import java.util.zip.*;import java.io.*; public class ZipFileExample { public static void main(String args[])throws IOException { FileInputStream fis=new FileInputStream("Allpaths.txt"); FileOutputStream fos=new FileOutputStream("kkk.txt"); DeflaterOutputStream dos=new DeflaterOutputStream(fos); int data; while((data=fis.read())!=-1){ dos.write(data); fis.close(); dos.close(); } } } eRROR: Exception in thread "main" java.io.FileNotFoundException: Allpaths.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at ZipFileExample.main(ZipFileExample.java:4)
modified on Thursday, July 16, 2009 11:06 PM
Hi, please check your Caps lock. And use pre tags. Anyway, are you sure "Allpaths.txt" exists? Of course you are, otherwise you wouldn't be posting here with ALL CAPS, but would you check it again? And that while loop looks fishy. Where is the closing }? I think it should be after the dos.write, but in this case you would accomplish the same by removing the opening {. This shouldn't even compile, but putting the } after dos.close is pretty much guaranteed to be wrong.
-
Hi, please check your Caps lock. And use pre tags. Anyway, are you sure "Allpaths.txt" exists? Of course you are, otherwise you wouldn't be posting here with ALL CAPS, but would you check it again? And that while loop looks fishy. Where is the closing }? I think it should be after the dos.write, but in this case you would accomplish the same by removing the opening {. This shouldn't even compile, but putting the } after dos.close is pretty much guaranteed to be wrong.
-
chinnasri wrote:
i closed properlly
No you didn't, you only closed in a way that would compile, not in the way that you should close. But anyway, the problem is not caused by the weird closing - the problem happens before that. But if you don't fix the closing, it will be your next problem. Or maybe there will be something in between, but eventually it will be a problem if you leave it like this. Please verify that the file that you are opening actually exists.
-
Hi , ANYONE LET ME KNOW WHAT IS GOING WRONG HER, I AM TRYING TO CREATE ZIP FILE IN JAVA, BUE IAM GETTING THE ERROR AS FALLOWS, GO THRU THE CODE ALSO WHAT I WROTE import java.util.zip.*;import java.io.*; public class ZipFileExample { public static void main(String args[])throws IOException { FileInputStream fis=new FileInputStream("Allpaths.txt"); FileOutputStream fos=new FileOutputStream("kkk.txt"); DeflaterOutputStream dos=new DeflaterOutputStream(fos); int data; while((data=fis.read())!=-1){ dos.write(data); fis.close(); dos.close(); } } } eRROR: Exception in thread "main" java.io.FileNotFoundException: Allpaths.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at ZipFileExample.main(ZipFileExample.java:4)
modified on Thursday, July 16, 2009 11:06 PM
The documentation for FileInputStream is very clear, if you read it:
If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.
So, either make sure that Allpaths.txt is in the classpath for your application, or specify the full path to its location. Your while loop is wrong. You close the input stream during the first iteration through the loop, immediately after writing to the deflater. So, the second time you try to read from it, it will fail because it is no longer open.