2011-04-14 46 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);