2015-02-11 37 views
2

我现在有一个数据库访问类拉从我的名单像这样列出的数据:获取列表从一个文本文件中的java

Pique:CBPique.png:41:22:55:91 
Ronaldo:STRonaldo.png:89:85:92:91 
... 

类如下:

class DatabaseAccess { 




static DatabaseAccess dataAccessor; 


static DatabaseAccess getInstance(String dbPath) { 

    if (dataAccessor == null) { 
     dataAccessor = new DatabaseAccess(dbPath); 
    } 

    return dataAccessor; 
} 


private DatabaseAccess(String dbPath) { 
    dbLocation = dbPath; 
} 


List<FootballPlayer> getCards() { 
    return this.getData(); 
} 


private List<FootballPlayer> getData() { 

    List<FootballPlayer> theData = new ArrayList<FootballPlayer>(); 

    // create a Scanner and grab the data . . . 

    Scanner scanner = null; 

    String dataPath = dbLocation + File.separator + "text" + File.separator + "players.db"; 

    String imagePath = dbLocation + File.separator + "images"; 

    try { 

     scanner = new Scanner(new File(dataPath)); 

    } catch (FileNotFoundException fnf) { 

     System.out.println(fnf.getMessage()); 


     System.exit(0); 
    } 

    // scan players.db file line-by-line 

    scanner.useDelimiter("\n"); 

    while (scanner.hasNext()) { 

     String line = scanner.next().trim(); 

     // trim used to trim for new line 

     String[] bits = line.split(":"); 

     String t = bits[0];     // title 
     String imgFileName = bits[1];   // image file name 
     int pa = Integer.parseInt(bits[2]);  // pace 
     int sh = Integer.parseInt(bits[3]);  // shooting 
     int dr = Integer.parseInt(bits[4]); // dribbling 
     int ph = Integer.parseInt(bits[5]); // physical 

     // create the image 

     ImageIcon img = new ImageIcon(imagePath + File.separator + imgFileName); 

     // Create the business object 

     FootballPlayer player = new FootballPlayer(t, img, pa, sh, dr, ph); 

     // add it to the list ... simple as ... 

     theData.add(player); 
    } 

    scanner.close(); 

    return theData; 

} 

我想要做的是从这个列表中提取数据并在另一个类中显示/使用它,例如拉取图像文件名以供使用,甚至在列表中显示所有数据。

一直在努力与它和任何帮助将非常感激。

+0

凹凸凹凸任何帮助找回您的播放器? – Nebbyyy 2015-02-11 02:11:10

回答

1

这可能是扫描仪看起来像这样的一种方式:它读取文件并将每行行保存到ArrayList中。

ArrayList<String> singleParts = new ArrayList<String>(); 

    try { 
     int index = 0; 
     Scanner scanner = new Scanner(new File("files/"+filename+".txt")); 
     while (scanner.hasNextLine()) { 
      String actualLine = scanner.nextLine(); 
      singleParts.add(actualLine); 
     } 
     scanner.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch(Throwable te) { 
     te.printStackTrace(); 
    } 

在此之后,你可以迭代你的ArrayList行每行char每个字符。为了使事情更容易,你可以使用':'作为分隔符。

例第一行,名称:

String name = ""; 
boolean saved = false; 

for(int line = 0; line < singleParts.size(); line++) { 
    for(int char = 0; char < singleParts.get(line).length(); char++) { 
     if(char<singleParts.get(line).indexOf(':') { 
      name += singleParts.get(line).charAt(char); 
     } 
    } 
} 

为了得到休息,你有很多的可能性。例如,删除已经使用的字符并重新使用与此类似的循环。

1

您的Player对象是否具有唯一的属性/属性,如名称或ID?

既然你已经有一个包含所有你需要的属性Player对象,为什么不把它放在一个HashMap而不是Arraylist,考虑到如果你有一个关键确定具体Player

FootballPlayer player = new FootballPlayer(t, img, pa, sh, dr, ph); 
yourKey = t // an identifier to you player, in this case i used t for example 
playersMap.put(t,player); 

这样你就可以很容易地通过使用

Player myPlayer = playersMap.get(yourKey) 

// you can then get the properties of the Player 
myPlayer.getImg(); 
myPlayer.getPa(); 

希望这有助于

相关问题