I am trying to read from a file and then writing back to that same while while appending it.
The file to be read has 5,10
and after writing, it the file should have 5,10"\n" 15
.
But instead of that it has 5,10"\n" SI
.
here is my source code -
import java.io.*;
import java.util.*;
public class Ash {
public static void main(String[] args) throws IOException{
File file = new File("ashray.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
Scanner input = new Scanner(reader);
String str = input.nextLine();
String [] sc = str.split(",");
int x = Integer.parseInt(sc[0]);
int y = Integer.parseInt(sc[1]);
int z = x+y;
input.close();
FileWriter fw = new FileWriter(file,true);
BufferedWriter writer = new BufferedWriter(fw);
writer.write(Integer.toString(z));
fw.close();
}
}