簡體   English   中英

如何閱讀文本文件中的其他行

[英]How to read the additional lines in a text file

我有一個創建“地圖”的任務,它實際上是一個包含房間的數組。 在房間里有一些文物,你可以查看或檢查並放入你的庫存或“背包”。 您還可以在地圖上保存和恢復展示位置,以及工件位置和廣告資源。 保存功能將其保存到包含位置,工件和庫存的文本文件中。 我遇到的問題是嘗試從文本文件中獲取工件數據以在程序上恢復。 我沒有加載位置的問題,但無法加載任何工件。 在運行調試器時,它會跳過該行。 這是我正在嘗試的一段代碼。

 File fileRestore = new File("C:\\Users\\mike\\Desktop\\GCPU.txt");
                    try
                    {
                        FileReader reader = new FileReader(fileRestore);
                        BufferedReader buffer = new BufferedReader(reader);
                        String line = buffer.readLine();
                        while (line !=null)
                        {
                            String[] contents = line.split(",");
                            String key = contents[0];

                            if (key.equals("StartLocation"))
                            {
                                row = Integer.parseInt(contents[1]);
                                col = Integer.parseInt(contents[2]);
                            }

                            key = contents [1];
                            if (key.equals("Artifact"))
                            {
                                String name = contents [1];
                                row = Integer.parseInt(contents [2]);
                                col = Integer.parseInt(contents [3]);
                                if (name.equals("vending"))
                                        map.rooms[row][col].contents = map.vending;
                                if (name.equals("beaker"))
                                        map.rooms[row][col].contents = map.beaker;
                                if (name.equals("gameboy"))
                                        map.rooms[row][col].contents = map.gameboy;
                                if (name.equals("paper"))
                                        map.rooms[row][col].contents = map.paper;
                                if (name.equals("trees"))
                                        map.rooms[row][col].contents = map.trees;
                                if (name.equals("desk"))
                                        map.rooms[row][col].contents = map.desk;
                                if (name.equals("brewer"))
                                        map.rooms[row][col].contents = map.brewer;
                                if (name.equals("statue"))
                                        map.rooms[row][col].contents = map.statue;

                            }

這是我正在閱讀的文本文件:

StartLocation,0,2 Artifact,Eerie statue,2,0 Artifact,A small redwood sprout,3,0 Artifact,Gameboy Color,0,1 Artifact,Lapdesk,1,1 Artifact,A piece of paper,2,1 Artifact,Industrial coffee maker,1,3

所以重申一下我的問題,如何從文本和StartLocation行中讀取Artifact行?

感謝您抽出時間來閱讀。

您只需從文件中讀取一次。 替換這兩行:

String line = buffer.readLine();
while (line !=null)
...

有這樣的事情:

String line = "";
while((line = buffer.readLine()) != null)
...

應該允許您在每次while循環迭代時讀取下一行。

或者,您也可以在while循環的line = buffer.readLine()大括號之前使用line = buffer.readLine() 這種方法也是有效的,但是,從我經歷的教程來看,簡直不太受歡迎。

String line = buffer.readLine();

應該在while循環中。 它只被執行一次。

雖然確保修改line初始值和循環條件,以便循環開始。

祝好運。

  1. String line = buffer.readLine(); 將跳過第一行。
  2.
       if (key.equals("StartLocation"))
          {   row = Integer.parseInt(contents[1]);
              col = Integer.parseInt(contents[2]);
          }

 will fail because key = " StartLocation". there is a space in your
 text file. use line=line.trim() to remove unnecessary characters.

 3. Your program will read only 1 line.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM