Hi guys,
i have the following class that writes to a file but is not working as i want. I call the write method in a while loop so that it writes a string to a new line. It only writes the last string. All the previous ones are not written except the last one.
Here is the class:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Writer {
private static PrintWriter outputStream = null;
public Writer(String algorithmName){
try {
outputStream = new PrintWriter(new FileWriter(algorithmName+".txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void write(String str){
try {
outputStream.append(str);
}catch(Exception exc){
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
}
example code:
Writer w = new Writer("filename");
for(int = i; i < 10; i++){
w.write(i);
}
In this case i get as results:
9
instead of
0
1
2
3
...
9
what am i doing wrong?
thanks