Posted February 27, 20187 yr I need to save a list of strings that gets added to every time a chat event is met, I am wanting this to be done with like a txt file but can't find a way for this to work, I had made this work in my development but it does not work in the real game. public class onChatEvent { @SubscribeEvent public void onChatMessage(ClientChatReceivedEvent event){ String word = event.message.getUnformattedText(); if(word.contains("The theme was: ")) { String file = "C:\\Users\\natha\\AppData\\Roaming\\.minecraft\\words.txt"; file = file.substring(1); String fileCopy = "C:\\Users\\natha\\AppData\\Roaming\\.minecraft\\wordscopy.txt"; fileCopy = fileCopy.substring(1); Scanner reader; PrintWriter writer; try { reader = new Scanner(new File(file)); writer = new PrintWriter(new FileWriter(fileCopy)); while (reader.hasNextLine()) { writer.println(reader.nextLine()); } writer.println(word.substring(15, word.length()-1)); reader.close(); writer.close(); } catch (IOException ex) { ex.getStackTrace(); } try { reader = new Scanner(new File(fileCopy)); writer = new PrintWriter(new FileWriter(file)); while (reader.hasNextLine()) { writer.println(reader.nextLine()); } reader.close(); writer.close(); } catch (IOException ex) { ex.getStackTrace(); } } } }
February 27, 20187 yr file and fileCopy hurt me, they should both be invalid pathnames starting with a colon... if you want to write in the game directory you should be able to just use a relative path (new File("words.txt"), or new File(".", "words.txt") if you want to explicitly mention a directory) besides that copying a file twice to append a line seems highly inefficient, instead try using a FileWriter: new BufferedWriter(new FileWriter("words.txt", true)); The second argument is the append flag, for more information google it. Also please don't forget to .flush() any output stream before closing it to prevent data loss
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.