Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.io.FileNotFoundException中的访问被拒绝_Java_Text Files_Java.util.scanner - Fatal编程技术网

java.io.FileNotFoundException中的访问被拒绝

java.io.FileNotFoundException中的访问被拒绝,java,text-files,java.util.scanner,Java,Text Files,Java.util.scanner,如果有帮助的话,我正在使用Eclipse 我必须使用扫描器访问java中的文件,并使用它们生成对象。一些对象依赖于其他对象,程序应该为这些对象调用适当的“生成器”方法 例如,我有一个效果类,由武器和神器类使用,由敌人类使用 生成这些文件的方法称为effectBuilder(字符串文件名),weaponBuilder(字符串文件名),等等。除了enemyBuilder(字符串文件名)方法之外,这些方法都没有问题,它给了我一个java.io.FileNotFoundException:。\doc\B

如果有帮助的话,我正在使用Eclipse

我必须使用扫描器访问java中的文件,并使用它们生成对象。一些对象依赖于其他对象,程序应该为这些对象调用适当的“生成器”方法

例如,我有一个
效果
类,由
武器
神器
类使用,由
敌人
类使用

生成这些文件的方法称为
effectBuilder(字符串文件名)
weaponBuilder(字符串文件名)
,等等。除了
enemyBuilder(字符串文件名)
方法之外,这些方法都没有问题,它给了我一个
java.io.FileNotFoundException:。\doc\Builders(访问被拒绝)
错误。文件位置是我保存这些方法的文本文件的位置

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);
}

有解决此问题的方法吗?

如果错误确实是这样:
java.io.FileNotFoundException:。\doc\Builders(访问被拒绝)
那么您似乎正在尝试打开一个目录,这意味着未使用有效的文件名调用
,如果文件名参数为空字符串,则可能无法访问该文件


检查文件的权限您可能无权以用户身份读取文件。

从消息的其余部分可以看出,当它第一次尝试使用
g
调用另一个
build
方法时,它会被卡住。是否有任何东西会导致它读取错误的行?找到错误,我猜对
nextLine()
的调用会导致以下
nextLine()
调用出现一些奇怪的行为,将它们切换到
next()
清除错误并允许该方法正常运行。它试图用空字符串调用
build
方法,这就是问题所在。@GoonyKnightMan否。问题是您无法打开该文件。构建
扫描仪
失败。您不可能执行了
next()
nextInt()
。我想它是在以前的文件中,包含要传输给每个生成器的文件名,是不是GoonyKnightMan?@EJP,是的,但我想它是由2个
nextInt()
调用引起的,因为它运行得很好,但是在第一个while循环中尝试在
buildblewarm()
调用中使用字符串
g
时崩溃了。将
String g=sc.nextLine()
更改为
String g=sc.next()
解决了问题。
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)