I want to save a player's inventory in a JSON file, and then read it to synchronize the inventory. However, on the reading file part, I have an error from my Bufferedreader. Is it because of characters I can store in the file like """ or "{" or event integers?
Here is my function:
public static String readInv(UUID id) throws IOException {
File file = new File(InventorySyncer.getInstance().getDataFolder(), id ".json");
if (file.exists()) {
BufferedReader reader = Files.newBufferedReader(Paths.get(file.toURI()));
try {
StringBuilder string = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
string.append(line);
}
reader.close();
return string.toString();
} catch (IOException e) {
InventorySyncer.getInstance().getLogger().warning("Error reading player " id " inventory");
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
InventorySyncer.getInstance().getLogger().warning("Cannot close BufferedReader, is it already closed ?");
e.printStackTrace();
}
}
InventorySyncer.getInstance().getLogger().info("Inv of player " id " read successfully");
}
InventorySyncer.getInstance().getLogger().info("Inv of player " id " is not saved");
return null;
}
CodePudding user response:
If you want to read a json you write yourself you already should have the object (in your case the Player class) and you can then read that easily with a FileReader and a JSONParser. To make it easier I just called the file "players.json". The content I assumed is this:
employees.json
[
{
"play": {
"firstName": "P1Fist",
"lastName": "P1Last"
}
},
{
"player": {
"firstName": "P2First",
"lastName": "P2Last"
}
}
]
The main part for reading the file is using a FileReader and a JSONParser.
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("players.json"))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray playerList = (JSONArray) obj;
//Iterate over employee array
playerList.forEach( player -> parsePlayer((JSONObject) player));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
The parsePlayer function could be something like this, qlthough you can leverage libraries to read directly to your already existing java Object.
private Player parsePlayer(JSONObject player)
{
//Get player object within list
JSONObject player = (JSONObject) player.get("player");
//Get employee first name
String firstName = (String) player.get("firstName");
//Get employee last name
String lastName = (String) player.get("lastName");
}
EDIT: I know this is not handling your issue with the reading of the characters but you can get rid of the error by using the FileReader and the JSONParser as the parser will handle them properly already.
CodePudding user response:
I figured it out, stupid mistake from me, I didn't try to debug. After debugging I saw that the problem wasn't coming from my reader function but from how I handle it afterwards. So I will be able to figure the problem out, thanks however for your help.