Datasets:
ArXiv:
License:
File size: 2,730 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
package battleship.Utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Scanner;
public class Settings {
private static final Settings settings = new Settings();
//public static String IP;
public static String Port = "25564";
private Settings() {}
public static Settings getInstance()
{
ReadFile();
Settings.WriteFile();
return settings;
}
private static void ReadFile()
{
try
{
File file = new File("settings.cfg");
Scanner reader = new Scanner(file);
Field[] fields = Settings.class.getFields();
while (reader.hasNextLine())
{
String data = reader.nextLine();
for (int i = 0; i < fields.length; ++i)
{
if (data.contains(fields[i].getName()))
{
try
{
String value = data.substring(data.indexOf(" ")+1);
fields[i].set(Settings.class.getClass(), value);
}
catch(IllegalAccessException ex)
{
System.out.println(ex.getMessage());
}
}
}
}
}
catch(FileNotFoundException | IllegalArgumentException | SecurityException ex)
{
System.out.println(ex.getMessage());
Port = "25564";
WriteFile();
}
}
public static void WriteFile()
{
try
{
File file = new File("settings.cfg");
if(!file.createNewFile())
{
file.delete();
file.createNewFile();
}
try (FileWriter fw = new FileWriter("settings.cfg"))
{
Field[] fields = Settings.class.getFields();
for (Field field : fields)
{
fw.write(field.getName() + " " + field.get(Settings.class.getClass()) + System.lineSeparator());
}
}
}
catch(IOException | IllegalAccessException ex)
{
System.out.println(ex.getMessage());
}
}
public static String getIP()
{
return "0.0.0.0";
}
public static int getPort()
{
return Integer.parseInt(Port);
}
public static void setPort(int port)
{
Port = String.valueOf(port);
}
} |