-1

I have been looking for the past hour or so trying to find the reason for this, but have found nothing. It is a very small text file (only 4 characters at most), thus the reason I did not bother with a BufferedReader or BufferedWriter. The problem lies in the fact that while I have the writer put the variable into the file and even close the file, it does not actually keep the change in the file. I have tested this by checking the file immediately after running the method containing this code.

try {
    int subtract = Integer.parseInt(secMessage[2]);
    try {
        String deaths = readFile("C:/Users/Samboni/Documents/Stuff For Streaming/deaths.txt", Charset.defaultCharset());
        FileWriter write = new FileWriter("C:/Users/Samboni/Documents/Stuff For Streaming/deaths.txt");
        int comb = Integer.parseInt(deaths) - subtract;
        write.write(comb);
        write.close();
        sendMessage(channel, "Death count updated to " + comb);
    } catch (IOException e) {
        e.printStackTrace();
    }
} catch (NumberFormatException e) {
    e.printStackTrace();
    sendMessage(channel, "Please use numbers to modify death count");
}

EDIT: Since it was asked, here is my readFile message:

static String readFile(String path, Charset encoding) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, encoding);
}

I have already tested it and it returns the contents without error.

EDIT2: Posting the readFile method made me think of something to try, so I removed the call to it (code above also updated) and tried it again. It now writes to the file, but does not write what I want. New question will be made for this.

Samboni
  • 7
  • 1
  • 5
  • 1
    What does `readFile(...)` do? I don't think that your posted code and text is adequate to allow us to understand your problem enough to answer it. If you don't get a decent answer soon, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Aug 10 '14 at 04:55
  • 1
    Please don't shift your requirements like this on us. Your question has been answered and you should accept the first correct solution. You now have a new problem and this should be part of a new separate question. – Hovercraft Full Of Eels Aug 10 '14 at 05:24
  • Oh ok, sorry about that. I'll do that then. – Samboni Aug 10 '14 at 05:24

2 Answers2

2
FileWriter write = new FileWriter(readFile("C:/Users/Samboni/Documents/Stuff For Streaming/deaths.txt", Charset.defaultCharset()));

You're trying to write a file named after the contents of deaths.txt. It's possible that you intend to be writing to the file itself.

Joe
  • 29,416
  • 12
  • 68
  • 88
  • I am trying to overwrite the contents so that the new number stored within the text file will be shown on the death counter which uses the file. – Samboni Aug 10 '14 at 05:05
  • I realize now what you meant was happening. The code has been modified and the question altered to ask about the new problem that arose. – Samboni Aug 10 '14 at 05:15
0

From http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html

FileWriter(String fileName) Constructs a FileWriter object given a file name.

    FileWriter write = new FileWriter(readFile("C:/Users/Samboni/Documents/Stuff For Streaming/deaths.txt", Charset.defaultCharset()));

Currently you are using the contents of the file instead of the file name.

MeowMeow
  • 622
  • 1
  • 8
  • 15
  • what is the result of this? int subtract = Integer.parseInt(secMessage[2]); – MeowMeow Aug 10 '14 at 05:18
  • That is there to turn the section of a message received in chat into an int variable instead of String variable, that way it can be subtracted from the number acquired from the text file. – Samboni Aug 10 '14 at 05:21