• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating A Folder With Files - Access Denied

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic