如果有帮助的话,我正在使用Eclipse。
我必须使用扫描仪访问java中的文件,我正在使用它们生成对象。有些对象依赖于其他对象,程序应该为这些对象调用适当的“生成器”方法。
例如,我有一个
Effect
类,它由
Weapon
和
Artifact
类,由
Enemy
班
生成这些的方法称为
effectBuilder(String fileName)
,
weaponBuilder(String fileName)
这些方法都没有问题,但
enemyBuilder(String fileName)
方法,它给我一个
java.io.FileNotFoundException: .\doc\Builders (Access is denied)
错误文件位置是我保存这些方法的文本文件的位置。
enemyBuilder方法如下:
类别:
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);
}
其他的
builder
方法执行与其他方法类似的调用
建设者
方法来创建新对象,但这是唯一导致问题的方法。
作为参考,下面是正在使用的txt文件的示例(括号中的信息详细说明了数据应用于哪个变量):
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)
这个问题有解决方案吗?