Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7.  
  8. public class SaveAndLoad {
  9. static final String players = "NewPlayers.txt";
  10. static final String teams = "NewTeams.txt";
  11. public static void SaveStringPlayer(String s) {
  12. String pre = LoadStringPlayer();
  13. try {
  14. File file = new File(players);
  15. if (!file.exists()) {
  16. // file.createNewFile();
  17. }
  18. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  19. BufferedWriter bw = new BufferedWriter(fw);
  20. bw.write(pre+s);
  21. bw.close();
  22. }
  23. catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. public static void SaveStringTeam(String s) {
  29. String pre = LoadStringTeam();
  30. try {
  31. File file = new File(teams);
  32. if (!file.exists()) {
  33. file.createNewFile();
  34. }
  35. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  36. BufferedWriter bw = new BufferedWriter(fw);
  37. bw.write(pre+s);
  38. bw.close();
  39. }
  40. catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45. public static void SavePlayer(Player s){
  46. String t = s.name+"$"+s.skill+"$"+s.star+"$"+s.position+"#";
  47. SaveStringPlayer(t);
  48. }
  49.  
  50. public static void SaveTeam(Teams t){
  51. String players = "";
  52. for(int i = 0; i < t.players.length-1; i++){
  53. players += t.players[i].name+"$";
  54. }
  55. players += t.players[t.players.length-1].name+"#";
  56. String team = t.name+"$"+players;
  57. SaveStringTeam(team);
  58. }
  59.  
  60. public static String LoadStringPlayer(){
  61. String content = "";
  62. File f = new File(players);
  63. if(f.exists() && !f.isDirectory()){
  64. try {
  65. content = new String(Files.readAllBytes(Paths.get("C:/Users/Lucas/Desktop/NewPlayers.txt")));
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. return content;
  71. }
  72.  
  73. public static String LoadStringTeam(){
  74. String content = "";
  75. File f = new File(teams);
  76. if(f.exists() && !f.isDirectory()){
  77. try {
  78. content = new String(Files.readAllBytes(Paths.get("C:/Users/Lucas/Desktop/NewTeams.txt")));
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. return content;
  84. }
  85.  
  86. public static void LoadPlayers(){
  87. String text = LoadStringPlayer();
  88. if(!text.isEmpty()){
  89. String[] full = text.split("#");
  90. for(int i = 0; i < full.length; i++){
  91. String[] p = full[i].split("\\$");
  92. boolean hasStar = (p[2] == "true" ? true : false);
  93. int playerSkill = Integer.parseInt(p[1]);
  94. Positions pos = Positions.PIVOT;
  95.  
  96. if(p[3].equals("GOALKEEPER"))
  97. pos = Positions.GOALKEEPER;
  98. else if(p[3].equals("DEFENDER"))
  99. pos = Positions.DEFENDER;
  100. else if(p[3].equals("WINGER1"))
  101. pos = Positions.WINGER1;
  102. else if(p[3].equals("WINGER2"))
  103. pos = Positions.WINGER2;
  104. else if(p[3].equals("PIVOT"))
  105. pos = Positions.PIVOT;
  106.  
  107. AllPlayers.AddPlayer(hasStar, p[0], playerSkill, pos);
  108. }
  109. }
  110. }
  111.  
  112. public static void LoadTeams(){
  113. String text = LoadStringTeam();
  114. Player[] playersS = new Player[5];
  115. String[] p = {};
  116. if(!text.isEmpty()){
  117. String[] full = text.split("#");
  118. for(int i = 0; i < full.length; i++){
  119. p = full[i].split("\\$");
  120. for(int j = 0; j < p.length-1; j++){
  121. playersS[i] = AllPlayers.StringToPlayer(p[j+1]);
  122. }
  123. AllTeams.AddTeam(p[0], playersS);
  124. }
  125. }
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement