English 中文(简体)
Java:文件读取问题
原标题:Java: FileRead question

你会如何编写这段代码?

这个问题是关于一个迷宫游戏,它有一个占据者的数组列表,包括探险家(你),怪物(接触即会死亡)和宝藏。游戏使用方块对象的块,这些占据者居住在其中。我想要做的特定事情是文件读取,可以导出迷宫的当前配置或将它们作为txt文件导入。

The specs: First read in the rows and cols of the Maze to create a Square[][] of the appropriate size. Then construct and read in all the Squares/Occupants.

对于方块迷宫,迷宫将首先确定该行以“Square”开头。然后读取方块的行和列,并使用该信息构造一个方块对象。最后,它将剩下的扫描器传递给方块的toObject方法,以便它可以初始化自己。

对于所有其他的住户,迷宫将确定其是哪种类型的住户,并使用仅接受迷宫的构造函数构造相应的对象。它不会从扫描仪中读取行或列,而是将扫描仪简单地传递给新创建的对象的toObject方法。

这是我目前拥有的代码,可能是错误的:

    public void readMazeFromFile(String fileName) throws IOException, FileNotFoundException, MazeReadException
   {
        Scanner fileSc = new Scanner(new File(fileName));
        String line = fileSc.nextLine(); //whats on the line, will be overwritten
        Scanner lineSc = new Scanner(line);
        String temp;
        lineSc.useDelimiter(",");
        int lineNum = 1; //every time you scan a line out, do lineNum++
        int r1, r2, r3, r4, c1, c2, c3, c4;

        rows = fileSc.nextInt();
        cols = fileSc.nextInt();
        Square hi = new Square(rows, cols);
        line = fileSc.nextLine();
        while ( line != null)
        {
            line = lineSc.nextLine();
            lineSc = new Scanner(line);

            if( lineSc.equals("Square"))
            {
                r1 = lineSc.nextInt();
                c1 = lineSc.nextInt(); 
                hi.toObject(lineSc);
            }
            if (lineSc.equals("Explorer"))
            {

                explorer.toObject(lineSc);
            }
            if (lineSc.equals("Treasure"))
            {
                Treasure.toObject(lineSc);
            }


            lineNum++;
        }

这里是示例输出:

5,5
Square,0,0,true,false,false,true,true,true
Square,0,1,true,false,true,false,true,true
Square,0,2,true,false,true,false,false,false
Square,0,3,true,false,false,false,false,false
Square,0,4,true,true,false,false,false,false
Square,1,0,false,false,true,true,true,true
Square,1,1,true,false,true,false,false,false
Square,1,2,true,true,false,false,false,false
Square,1,3,false,true,false,true,false,false
Square,1,4,false,true,false,true,false,false
Square,2,0,true,false,false,true,false,false
Square,2,1,true,false,true,false,false,false
Square,2,2,false,true,false,false,false,false
Square,2,3,false,true,false,true,false,false
Square,2,4,false,true,false,true,false,false
Square,3,0,false,true,false,true,false,false
Square,3,1,true,false,false,true,false,false
Square,3,2,false,true,false,false,false,false
Square,3,3,false,true,true,true,false,false
Square,3,4,false,true,false,true,false,false
Square,4,0,false,true,true,true,false,false
Square,4,1,false,true,true,true,false,false
Square,4,2,false,false,true,true,false,false
Square,4,3,true,false,true,false,false,false
Square,4,4,false,true,true,false,false,false
Explorer,0,0,Scary Name
Treasure,4,4,true
Treasure,2,2,false
Monster,4,4
Monster,3,3

你会为这一部分写什么?

最佳回答

这是一个可以开始的框架。在这里,您真的不需要比 Scanner 更多的东西。它可以完成您需要扫描输入并执行转换等操作的一切。您想使用它的 next() nextInt() nextBoolean() nextLine()方法。

    Scanner in = new Scanner(new File(filename));
    in.useDelimiter("\s+|,");

    int rows = in.nextInt();
    int cols = in.nextInt();
    // construct the array

    while (in.hasNext()) {
        String type = in.next();
        int r = in.nextInt();
        int c = in.nextInt();
        // read more depending on type
    }
问题回答

Use a FileReader on the file and then attach a StreamTokenizer to that, far more efficient :)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签