GoonyKnightMan GoonyKnightMan - 6 months ago 32
Java Question

Access is Denied in java.io.FileNotFoundException

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

Effect
class, which is used by the
Weapon
and
Artifact
class, which are used by the
Enemy
class.

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
java.io.FileNotFoundException: .\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:

Class:

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
e.printStackTrace();
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()){
weps.add(Builder.buildWeapon(g));
g = sc.nextLine();
}

ArrayList<Artifact> facs = new ArrayList<Artifact>();
while(sc.hasNextLine()){
facs.add(Builder.buildArtifact(sc.nextLine()));
}

sc.close();

//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
builder
methods do similar calls to other
builder
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

If the error is really this one : java.io.FileNotFoundException: .\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

Comments