We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Unable to create a file using java.io.File

bent27
bent27 asked
on
Medium Priority
284 Views
Last Modified: 2013-12-29
I  have attached my code

The problem is, no exception is thrown in
'File tempFile = new File("C://workspace//tempData//" + fileName);

but, a FileNotFoundException is thrown in
ZipFile zipFile = new ZipFile(tempFile);

Also, it specifies the Absolute Path, but when I check there, there is no file created.

Any help is appreciated.
TIA

public boolean validateZip(MultipartFile file, List<String> files) throws Exception {
		String fileName = file.getOriginalFilename();
		File tempFile = new File("C://workspace//tempData//" + fileName);
		LOG.info("Absolute path : " + tempFile.getAbsolutePath());
		ZipFile zipFile = new ZipFile(tempFile);
		LOG.info("No of enteries in the zip : " + zipFile.size());
		//loop through the list and check if the entry is in the zip file
 
	}

Open in new window

Comment
Watch Question

Commented:
You've only created a file instance, not file itself. Create a file:

File file = new File("test");
file.createNewFile();

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
mahome, I did try implementing

file.createNewFile();,  it doesn't seem to work...
CERTIFIED EXPERT
Top Expert 2016

Commented:
Try the following:
File dir = new File("C:/workspace/tempData");
dir.mkdirs();
File tempFile = new File(dir, fileName);
LOG.info("Absolute path : " + tempFile.getAbsolutePath());
ZipFile zipFile = new ZipFile(tempFile);

Open in new window

Author

Commented:
CEHJ,  thanks a ton for your code, it works when I debug the application, but, if I try to run it, it throws the FileNotfoundException at this line ....




ZipFile zipFile = new ZipFile(tempFile);

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
You have a logical error perhaps. Using that code (your last post) you are expecting a zip file to already exist with that path. Does one exist? I guess not...
CERTIFIED EXPERT
Top Expert 2016

Commented:
If you need to create one - you need to use JarFile. afaik, ZipFile is read-only

Author

Commented:
my intention is to validate the contents in the ZipFile, so, is it that, when I debug, the app has time to actually write the tempFile to the correct location, but when I run the app, its quick, the file isn't created, and so, it throws the Exception...
CERTIFIED EXPERT
Top Expert 2016

Commented:
Can you tell me what the value of 'tempFile' is?

Author

Commented:
'tempFile' is the file created from a multipartfile which the user has uploaded, now this multipartfile contains a zip file. I'm trying to validate the contents of the zipfile, in order to do that, I'm reading the multipartfile as a File, then, I'm reading it as a ZipFile...............

It works perfect when I debug the app, but when I run the app, the File from the multipartfile is not created and this causes the ZipFile creation to throw a FileNotFoundException....
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK, but what is the value of the String 'tempFile' ?

Author

Commented:
when I debug it, the tempFile instance is has only 1 field, thats 'path', and it indicates the location where its stored :

like : C:\\workspace\\tempData\\Sample.zip
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>the File from the multipartfile is not created

So you create the file and then your code you posted above is there to validate it? How are you creating the file first with the multipart in it?

Incidentally you should create the file in your code using


C:/workspace/tempData/Sample.zip

Author

Commented:
Thanks CEHJ, apparently, this is a race condition, I've decided to implement apache commons FileUpload instead...,,
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.