dam1ne
dam1ne

Reputation: 361

FileWriter not writing to file

i have written a program that is supposed to get a value from an existing file, add one to that value, delete the file, create a new instance of the file, and write the new value to the new instance of the file.

public static void main(String[] args) throws InterruptedException, IOException {

    //initialize newFile and writer
    File newFile = new File("C:\\Users\\boung\\Desktop\\python\\daysSince.txt");
    FileWriter writer = new FileWriter("C:\\Users\\boung\\Desktop\\python\\daysSince.txt", true);

    //if newFile doesn't exist or of newFile doesn't contain any content, create newFile and write "1" to newFile
    if(newFile.length() == 0) {

        System.out.println("empty");
        writer.write("1");
        writer.write("\r\n");
        writer.close();

    } else {

        //get contents of newFile
        StringBuilder contentBuilder01 = new StringBuilder();                       
        try (Stream<String> stream = Files.lines( Paths.get("C:\\Users\\boung\\Desktop\\python\\daysSince.txt"), StandardCharsets.UTF_8)) {

                stream.forEach(s -> contentBuilder01.append(s).append("\n"));

            } catch (IOException e) {

                e.printStackTrace();

            }

        //convert content to integer
        String content = contentBuilder01.toString();
        content = content.replaceAll("\\D+", "");
        int value = Integer.parseInt(content);
        System.out.println(value);

        //add 1 to the value that was returned from getting the contents of newFile and assign it to newValue
        int newValue = value + 1;

        //delete newFile
        newFile.delete();

        //create new instance of newFile to prepare for next execution
        if(newFile.length() == 0) { 
        newFile = new File("C:\\Users\\boung\\Desktop\\python\\daysSince.txt");
        }

        FileWriter writer1 = new FileWriter("C:\\Users\\boung\\Desktop\\python\\daysSince.txt", true);          

        //write newValue to new instance of newFile
        writer1.write(newValue);
        System.out.println("printed " + newValue);
        writer1.write("\r\n");
        writer1.close();
    }



}       

the problem is occurring in this area

        writer1.write(newValue);
        System.out.println("printed " + newValue);
        writer1.write("\r\n");
        writer1.close();

assuming that newFile doesnt exist, after running the program twice the expected output would look like this

2

but this is the output i am getting

1

but here the program has no problem writing 1 to the file if the file is empty or does not exist

    System.out.println("empty");
    writer.write("1");
    writer.write("\r\n");
    writer.close();

i assume i have made a mistake with the logic of the program, can anyone help?

Upvotes: 0

Views: 167

Answers (2)

Julien Rheinbach
Julien Rheinbach

Reputation: 313

The newFile.delete(); is unnecessary, you don't need to delete the file to write to it again. The following code sample works for me and outputs correctly.

    StringBuilder contentBuilder01 = new StringBuilder();
    try (Stream<String> stream = Files.lines( Paths.get(fileName), StandardCharsets.UTF_8)) {
        stream.forEach(s -> contentBuilder01.append(s).append("\n"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileWriter writer = new FileWriter(fileName);
    writer.write(Integer.toString(Integer.parseInt(contentBuilder01.toString().trim()) + 1));
    writer.close();

Upvotes: 0

Charlie Armstrong
Charlie Armstrong

Reputation: 2342

I believe your program has two problems:

  1. newFile.delete(); does nothing because writer is still open. You need to first close writer by doing writer.close();.
  2. writer1.write(newValue); writes newValue as an int. That means it's literally taking all the binary digits that represent that number and just writing them straight to the file. Instead, you want to write newValue as a String, which will produce that number in text so you can read it. So you want to use writer1.write(Integer.toString(newValue)); instead.

Upvotes: 1

Related Questions