I am trying to create a file and keep on appending into that file. In my code file is getting created but It is not appending anything in that file. Don’t know why? Can anyone suggest me what wrong I am doing..
Updated Code:-
File file =new File("D://GeoData1.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
int j = -1;
while(true) {
j++;
String ipAddress = generateIPAddress(j);
try {
out.write(ipAddress);
System.out.println(ipAddress);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Are you closing the writer?
Update:
If you are using Java 7, try this:
Code adapted from The Java Tutorials.
Check if the IPS are being printed to the console and the file.
Fully working code with a mock implementation of
generateIPAddress. It appends 5000 random IPv4 addresses (not checking for validity) tomyfile.txteverytime it is executed.