GoonyKnightMan GoonyKnightMan - 2 years ago 132
Java Question

Access is Denied in

I'm using Eclipse, if that helps.

I have to access files in java using a Scanner, and I'm using them to generate objects. Some of the objects have dependencies on other objects, and the program is supposed to call the appropriate "builder" method for these objects.

For example, I have an

class, which is used by the
class, which are used by the

The methods for generating these are called
effectBuilder(String fileName)
weaponBuilder(String fileName)
, etc. There's no issues with any of these methods but the
enemyBuilder(String fileName)
method, which gives me a .\doc\Builders (Access is denied)
error. The file location is where I'm keeping the text files for these methods.

The enemyBuilder method is as follows:


public static Enemy buildEnemy(String fileName)
Scanner sc;

//creates Scanner, prints error and returns null if file is not found

try {
sc = new Scanner(new File("./doc/Builders/"+fileName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return null;

//values are put into constructor at the end of the method.

String n = sc.nextLine();
int h = sc.nextInt();
int d = sc.nextInt();
int lo = sc.nextInt();
int hi = sc.nextInt();
String g = sc.nextLine();

ArrayList<Weapon> weps = new ArrayList<Weapon>();
while(!g.equals("a") && sc.hasNextLine()){
g = sc.nextLine();

ArrayList<Artifact> facs = new ArrayList<Artifact>();


//converting for constructor purposes

Weapon[] warr = new Weapon[weps.size()];
int x = 0;
for(Weapon e : weps)
warr[x++] = e;

Artifact[] aarr = new Artifact[facs.size()];
x = 0;
for(Artifact e : facs)
aarr[x++] = e;

return new Enemy(n, h, d, lo, hi, warr, aarr);

The other
methods do similar calls to other
methods to create new objects, but this is the only one causing any problems.

For reference, here is a sample of the txt file that is being used (The information in parenthesis details what variable the data should be used for):

Warrior (should be n)
12 (should be h)
10 (should be d)
15 (should be lo)
30 (should be hi)
battleaxe.txt (first instance of g in 1st loop)
longsword.txt (second instance)
a (signifies the computer to move to next while loop)
battlemedallion.txt (first instance of g in 2nd loop)
chestplate.txt (second instance)

Is there a solution for this problem?

Answer Source

If the error is really this one : .\doc\Builders (Access is denied) then it seems you're trying to open a directory, which means buildEnemy is not called with a valid filename

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download