Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FileNotFoundException for zip files

0 views
Skip to first unread message

Petterson Mikael

unread,
Feb 20, 2004, 4:41:09 AM2/20/04
to
Hi,

I get the following exception.

java.io.FileNotFoundException: attrtest.zargo (No such file or
directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:59)
at ZargoFile.unPack(ZargoFile.java:47)
at ZargoFile.getXmiFiles(ZargoFile.java:35)
at ZipTest.startExec(ZipTest.java:17)
at ZipTest.main(ZipTest.java:11)

Since only the file name is sent to unpack ( not the path including
filename) the file will not be found.
Anyone that has an idea on how to send the both path+filename to
unPack()?

BR

//Mikael


/* Make a list of all files in the directory.*/
public ArrayList getXmiFiles(){
ArrayList xmi = new ArrayList();
File directory = new File(dirName);

if(directory.exists()){
// Get the list of the files contained in the package
String [] files = directory.list();
for(int i=0;i<files.length;i++){
if (files[i].endsWith(".zargo")){
//logger.debug("File name is "+file[i]);
System.out.println("File name is "+files[i]);
xmi.add(unPack(files[i]));
}
}
}

return xmi;
}

public ArrayList unPack(String zargo){
ArrayList xmiFiles = new ArrayList();
// crude code to unpack a zip file into element files.
try{
FileInputStream fis = new FileInputStream (zargo) ;
ZipInputStream zip = new ZipInputStream ( fis );

// loop for each entry
while ( true ) {
ZipEntry entry = zip.getNextEntry();
// relative name with slashes to separate dirnames.
String elementName = entry.getName();
//Retrieve *.xmi files.
if(elementName.endsWith(".xmi")){
File elementFile = new File( dirName , elementName
);
}
}
}catch (FileNotFoundException fnf){
fnf.printStackTrace();

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

return xmiFiles;

}

Andrew Thompson

unread,
Feb 20, 2004, 5:21:05 AM2/20/04
to
Petterson Mikael wrote:
..

> I get the following exception.
>
> java.io.FileNotFoundException: attrtest.zargo (No such file or
..

> Since only the file name is sent to unpack ( not the path including
> filename) the file will not be found.

It is better to deal with more appropriate
objects, like, for instance File's

> Anyone that has an idea on how to send the both path+filename to
> unPack()?

See below..

> /* Make a list of all files in the directory.*/
> public ArrayList getXmiFiles(){
> ArrayList xmi = new ArrayList();
> File directory = new File(dirName);
>
> if(directory.exists()){
> // Get the list of the files contained in the package
> String [] files = directory.list();

File[] files = directory.listFiles();

> for(int i=0;i<files.length;i++){
> if (files[i].endsWith(".zargo")){
> //logger.debug("File name is "+file[i]);
> System.out.println("File name is "+files[i]);
> xmi.add(unPack(files[i]));
> }
> }
> }
>
> return xmi;
> }
>
> public ArrayList unPack(String zargo){

public ArrayList unPack(File zargo)

> ArrayList xmiFiles = new ArrayList();
> // crude code to unpack a zip file into element files.
> try{
> FileInputStream fis = new FileInputStream (zargo) ;

HTH

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology


Petterson Mikael

unread,
Feb 20, 2004, 5:55:26 AM2/20/04
to
Hi again,

I have made the following changes:

import java.io.File;
import java.net.URL;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.*;

public class ZargoFile {


private String dirName;

public ZargoFile (String dirName){
this.dirName = dirName;

}

/* Make a list of all files in the directory.*/
public ArrayList getXmiFiles(){
ArrayList xmi = new ArrayList();

//Get a File object for the dirName.
//URL url = Launcher.class.getResource(dirName);


File directory = new File(dirName);

if(directory.exists()){
// Get the list of the files contained in the package

File[] files = directory.listFiles();
for(int i=0;i<files.length;i++){

if (files[i].toString().endsWith(".zargo")){


//logger.debug("File name is "+file[i]);
System.out.println("File name is "+files[i]);
xmi.add(unPack(files[i]));
}
}
}

return xmi;
}

public ArrayList unPack(File zargo){


ArrayList xmiFiles = new ArrayList();
// crude code to unpack a zip file into element files.
try{
FileInputStream fis = new FileInputStream (zargo) ;

ZipInputStream zip = new ZipInputStream ( fis );

// loop for each entry
while ( true ) {
ZipEntry entry = zip.getNextEntry();
// relative name with slashes to separate dirnames.
String elementName = entry.getName();
//Retrieve *.xmi files.
if(elementName.endsWith(".xmi")){
File elementFile = new File( dirName , elementName
);
}
}
}catch (FileNotFoundException fnf){
fnf.printStackTrace();

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

return xmiFiles;

}
}

Hmm now I get a NullPointerException.... Any more hints...


File name is /home/eraonel/moscriptgen/dev/attrtest.zargo
Exception in thread "main" java.lang.NullPointerException
at ZargoFile.unPack(ZargoFile.java:54)

Andrew Thompson

unread,
Feb 20, 2004, 7:26:30 AM2/20/04
to
Petterson Mikael wrote:
...

> I have made the following changes:

..errm. Where did you leave the 'main'?
http://www.physci.org/codes/sscce.jsp

Ryan Stewart

unread,
Feb 20, 2004, 7:39:40 AM2/20/04
to
"Petterson Mikael" <mikael.p...@era.ericsson.se> wrote in message
news:4035E79E...@era.ericsson.se...

Is that the "if (elementName.endsWith(...))" line? If so, it looks like your
elementName is null. You might try outputting it to check its value. I
notice you have an inifinite loop there. What is the supposed outcome of
that? Is there supposed to be an exception thrown to break out of it? If so,
where do you expect it to be thrown? Is it being thrown?


0 new messages