我正在为Android游戏设计一个简单的关卡编辑器。我用swing编写了GUI(绘制网格)。你点击你想放置一个瓷砖的方块,它会改变颜色。完成后,您将所有内容写入文件。读取文件中的整数值(Java)
我的文件包含类似下面的(这只是一个例子):
我用星号来确定正在读取的级别号和连字符告诉读者停止阅读。
我的文件读取代码在下面,选择要读取的部分工作正常 - 例如。如果我在第2通过执行以下操作:
readFile(2);
然后打印所有字符在第二部分
我想不出什么了,一旦我到了“开始'点,我怎么实际读取整数和不是个别字符?
代码
public void readFile(int level){
try {
//What ever the file path is.
File levelFile = new File("C:/Temp/levels.txt");
FileInputStream fis = new FileInputStream(levelFile);
InputStreamReader isr = new InputStreamReader(fis);
Reader r = new BufferedReader(isr);
int charTest;
//Position the reader to the relevant level (Levels are separated by asterisks)
for (int x =0;x<level;x++){
//Get to the relevant asterisk
while ((charTest = fis.read()) != 42){
}
}
//Now we are at the correct read position, keep reading until we hit a '-' char
//Which indicates 'end of level information'
while ((charTest = fis.read()) != 45){
System.out.print((char)charTest);
}
//All done - so close the file
r.close();
} catch (IOException e) {
System.err.println("Problem reading the file levels.txt");
}
}
谢谢!这工作很好:-) – Zippy