File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating A Folder With Files - Access Denied

 
Rick Tasche
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rick Tasche
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siva Vulchi
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic