Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class SaveAndLoad {
- static final String players = "NewPlayers.txt";
- static final String teams = "NewTeams.txt";
- public static void SaveStringPlayer(String s) {
- String pre = LoadStringPlayer();
- try {
- File file = new File(players);
- if (!file.exists()) {
- // file.createNewFile();
- }
- FileWriter fw = new FileWriter(file.getAbsoluteFile());
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(pre+s);
- bw.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void SaveStringTeam(String s) {
- String pre = LoadStringTeam();
- try {
- File file = new File(teams);
- if (!file.exists()) {
- file.createNewFile();
- }
- FileWriter fw = new FileWriter(file.getAbsoluteFile());
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(pre+s);
- bw.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void SavePlayer(Player s){
- String t = s.name+"$"+s.skill+"$"+s.star+"$"+s.position+"#";
- SaveStringPlayer(t);
- }
- public static void SaveTeam(Teams t){
- String players = "";
- for(int i = 0; i < t.players.length-1; i++){
- players += t.players[i].name+"$";
- }
- players += t.players[t.players.length-1].name+"#";
- String team = t.name+"$"+players;
- SaveStringTeam(team);
- }
- public static String LoadStringPlayer(){
- String content = "";
- File f = new File(players);
- if(f.exists() && !f.isDirectory()){
- try {
- content = new String(Files.readAllBytes(Paths.get("C:/Users/Lucas/Desktop/NewPlayers.txt")));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return content;
- }
- public static String LoadStringTeam(){
- String content = "";
- File f = new File(teams);
- if(f.exists() && !f.isDirectory()){
- try {
- content = new String(Files.readAllBytes(Paths.get("C:/Users/Lucas/Desktop/NewTeams.txt")));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return content;
- }
- public static void LoadPlayers(){
- String text = LoadStringPlayer();
- if(!text.isEmpty()){
- String[] full = text.split("#");
- for(int i = 0; i < full.length; i++){
- String[] p = full[i].split("\\$");
- boolean hasStar = (p[2] == "true" ? true : false);
- int playerSkill = Integer.parseInt(p[1]);
- Positions pos = Positions.PIVOT;
- if(p[3].equals("GOALKEEPER"))
- pos = Positions.GOALKEEPER;
- else if(p[3].equals("DEFENDER"))
- pos = Positions.DEFENDER;
- else if(p[3].equals("WINGER1"))
- pos = Positions.WINGER1;
- else if(p[3].equals("WINGER2"))
- pos = Positions.WINGER2;
- else if(p[3].equals("PIVOT"))
- pos = Positions.PIVOT;
- AllPlayers.AddPlayer(hasStar, p[0], playerSkill, pos);
- }
- }
- }
- public static void LoadTeams(){
- String text = LoadStringTeam();
- Player[] playersS = new Player[5];
- String[] p = {};
- if(!text.isEmpty()){
- String[] full = text.split("#");
- for(int i = 0; i < full.length; i++){
- p = full[i].split("\\$");
- for(int j = 0; j < p.length-1; j++){
- playersS[i] = AllPlayers.StringToPlayer(p[j+1]);
- }
- AllTeams.AddTeam(p[0], playersS);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement