aspose file tools*
The moose likes Beginning Java and the fly likes Populating A Folder With Files - Access Denied Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populating A Folder With Files - Access Denied " Watch "Populating A Folder With Files - Access Denied " New topic
Author

Populating A Folder With Files - Access Denied

Rick Tasche
Greenhorn

Joined: Feb 20, 2012
Posts: 10
Quite a few of you are going to get a chuckle out of the mess I made of this code. (( my native programming language is not Java ))

What I would like to do is create a folder, and in that folder create files using the names from a list, and in each file, add text.

When using a version of this code, that only create the folder and 1 file inside, it works great. It's when I add the while{} loop that I get the access denied error.

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

Hi Rick Tasche,

Welcome to CodeRanch!

Have you tried to debug the code? Your code almost works fine after fixing a glitch. I believe you'll get it during first pass of debugging itself

Please debug and let us know if you still face any issues.

By the way (its not related to your issue, but) please do not keep empty try/catch blocks. Catching java.lang.Exception and having an empty catch block is very bad programming practice. Secondly, name of your class Make_Files looks more like a method name instead of class name. How about FileMaker?

All the best!


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Rick Tasche
Greenhorn

Joined: Feb 20, 2012
Posts: 10
Here is a nice "newbie" question .... how does one debug in Java ( Eclipse IDE )?

The code stops working right at the while() loop.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

Rick Tasche wrote: Here is a nice "newbie" question .... how does one debug in Java ( Eclipse IDE )?

The code stops working right at the while() loop.

Ok. Let's do it step by step.

First things first : you are not printing access denied, still you are getting it. How are you getting it? Exactly after which statement you are reaching there? Why you are reaching there?

To narrow down, how about printint stack trace in case of exception? It'll give you line number(s).
Rick Tasche
Greenhorn

Joined: Feb 20, 2012
Posts: 10
1: -
Access Denied is caught by


2: -
A file is created, which means that the code fails after


3: -
I've spent over 5 hours now trying to get that code to work ... I've since split it into separate methods, which work great .

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8216
    
  23

Rick Tasche wrote:Access Denied is caught by

Not the best, but that's by-the-by.
My advice: Instead of a message, print out the stack trace. That will give you a lot more information.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
Good effort Rick.

My advice is that, first have a look at File API which describes the methods functionality clearly and then try to resolve your issue.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

Hi Rick Tasche,

As I said,
Anayonkar Shivalkar wrote:To narrow down, how about printint stack trace in case of exception? It'll give you line number(s).
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
The problem is at these lines:



The first line will create a new directory called "My New Folder".
The second line will try to create a file called "My New Folder". When you look at the API, you will notice that instantiating a new FileWriter using the constructor that takes a String will try to create a file with that String value as a name. But since you've already created a directory with that name, there's where it fails.

So the second line should be:



And your good to go.

P.s. I'm not getting why your trying to create a FileWriter that refers to a directory anyway?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating A Folder With Files - Access Denied