扫描仪.下一个返回文件的路径



我有一个扫描仪:

m = new Scanner(Values.mazegen.replace("\","//") + "//maze.txt");

(Values.mazegen.replace ("","//") + "//maze.txt"打印C:////上的用户名都用户//dangillmor桌面)当我用m.next()它返回C://users//myusername//Desktop//maze.txt。我不知道为什么…?完整代码:

public void openFile(){
        try {
            if(Values.custom == true && Values.customSolved == false){
            m = new Scanner(new File(Values.MazeFile));
            }else if(Values.customSolved == true || Values.custom == false){
            m = new Scanner(Values.mazegen.replace("\","//") + "//Maze.txt");
            }else{
                m = new Scanner(Values.mazegen.replace("\","//") + "//Maze.txt");
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
        }
        if(Values.custom == true && Values.customSolved == false){
            m.nextLine();
        }
    }
    public void readFile(){
        System.out.println("Got this far...");
        while(m.hasNext()){
            for(int i = 0; i < Values.x; i++){
                Maze[i] = m.next();
                System.out.println(Maze[i]);
            }
            m.nextLine();
        }
    }
    public void closeFile(){
        m.close();
    }

(OpenFile, readFile, closeFile按顺序执行)

Scanner(String)扫描您传递给它的字符串字面量,而不是文件。使用new Scanner(new File("path to file"))

扫描文件

最新更新