0
所以我知道在C++中我可以做到这一点:从文本文件中获取格式化数字的简单方法?
ifstream file("data/maps/ssdebug1.cfg");
string line;
float startx = 0;
sscanf (line.c_str(), "start-x = %f;", &startx);
但有一个同样简单的方法在Java中做到这一点?
所以我知道在C++中我可以做到这一点:从文本文件中获取格式化数字的简单方法?
ifstream file("data/maps/ssdebug1.cfg");
string line;
float startx = 0;
sscanf (line.c_str(), "start-x = %f;", &startx);
但有一个同样简单的方法在Java中做到这一点?
在Java中,你可以从Java 1.5版做
FileInputStream fis = new FileInputStream("data/maps/ssdebug1.cfg");
Properties prop = new Properties();
prop.load(fis);
fis.close();
// you can have any number of properties with comments.
// However, it is not assumed aline ends with a ';'
String start_x = prop.getProperty("start-x");
double startx = Double.parseDouble(start_x);
可以使用java.util.Scanner中也..
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
例子:
Scanner in = new Scanner(new File("data/maps/ssdebug1.cfg"));
while (in.hasNextLong()) {
long my = in.nextLong();
}
的NumberFormat包HTTP: //download.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html – 2011-04-14 16:44:24
或扫描仪类... – karakuricoder 2011-04-14 16:46:53