2011-04-14 48 views
0

所以我知道在C++中我可以做到这一点:从文本文件中获取格式化数字的简单方法?

ifstream file("data/maps/ssdebug1.cfg"); 
    string line; 
float startx = 0; 
    sscanf (line.c_str(), "start-x = %f;", &startx); 

但有一个同样简单的方法在Java中做到这一点?

+0

的NumberFormat包HTTP: //download.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html – 2011-04-14 16:44:24

+0

或扫描仪类... – karakuricoder 2011-04-14 16:46:53

回答

2

在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);