【发布时间】:2012-01-28 20:07:10
【问题描述】:
这是我用来从文件加载保存的游戏数据的方法。因为这个,我在某个时候得到了一个叫做 error15 的东西。谷歌上出现的唯一事情是与 http 有关,但那不可能,因为我没有做那样的事情。当对象将字符串打印到控制台时,它会完成第一行保存的数据,但不会继续读取其他数据。我有一种预感,这可能与我对 in.nextLine(); 的使用有关(还有其他我应该使用的东西吗?)如果有人能告诉我我做错了什么,我会永远爱你。
/**
* Returns a 2-dimensional 15*15 array of saved world data from a file named by x and y coordinates
*/
public String[][] readChunkTerrain(int x, int y)
{
String[][] data = new String[15][15]; //the array we will use to store the variables
try {
//initiate the scanner that will give us information about the file
File chunk = new File( "World/" + x + "." + y + ".txt" );
Scanner in = new Scanner(
new BufferedReader(
new FileReader(chunk)));
//go through the text file and save the strings for later
for (int i=0; i<15; i++){
for (int j=0; j<=15; j++){
String next = in.next();
data[i][j] = next;
System.out.println(i + j + next); //temporary so I can see the output in console
System.out.println();
}
in.nextLine();
}
in.close(); //close the scanner
}
//standard exception junk
catch (Exception e)
{System.err.println("Error" + e.getMessage());}
return data; //send the array back to whoever requested it
}
【问题讨论】:
标签: java file-io java.util.scanner filereader