I'm trying to have a dedicated thread that periodically overwrites a file such that the only thing that actually changes in the file is something that I've changed in the GUI. Here is the relevant Code:
private ThreadClass writeToFile = new ThreadClass("toFile"){
@Override
public void run(){
synchronized(lock){
NameAddressID store[] = new NameAddressID[10];
boolean tmpFile = true;
Scanner loadOver;
PrintWriter overWrite;
while(true){
try {
if(tmpFile){
loadOver = new Scanner(new File("NAIList.txt"));
overWrite = new PrintWriter("NAIList.tmp", "UTF-8");
}else{
loadOver = new Scanner(new File("NAIList.tmp"));
overWrite = new PrintWriter("NAIList.txt", "UTF-8");
}
for(int i = 0; i < count-1; i++){
for(int j = 0; j<10; j++){
if(loadOver.hasNextLine()){
String[] tokens = loadOver.nextLine().split(";");
store[j] = new NameAddressID(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], Integer.valueOf(tokens[6]), Integer.valueOf(tokens[7]));
}else{
store[j] = null;
}
}
for(int j = 0; j<store.length;j++){
if(store[j] != null){
overWrite.print(store[j].getFirst()+";");
System.out.print(store[j].getFirst()+" ");
overWrite.print(store[j].getMiddle()+";");
System.out.print(store[j].getMiddle()+" ");
overWrite.print(store[j].getLast()+";");
System.out.print(store[j].getLast()+" ");
overWrite.print(store[j].getStreet()+";");
System.out.print(store[j].getStreet()+" ");
overWrite.print(store[j].getCity()+";");
System.out.print(store[j].getCity()+" ");
overWrite.print(store[j].getState()+";");
System.out.print(store[j].getState()+" ");
overWrite.print(store[j].getZip()+";");
System.out.print(store[j].getZip()+" ");
overWrite.println(store[j].getId());
System.out.println(store[j].getId());
store[j] = null;
overWrite.flush();
}else{
break;
}
}
}
saveArrayText("NAIList.tmp", overWrite);
for(int i = 0; i < addressList.size(); i++){
if(loadOver.hasNextLine()){
String skip = loadOver.nextLine();
}
}
while(loadOver.hasNextLine()){
int index = 0;
String[] tokens = loadOver.nextLine().split(";");
store[index++] = new NameAddressID(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], Integer.valueOf(tokens[6]), Integer.valueOf(tokens[7]));
for(int j = 0; j<store.length;j++){
if(store[j] != null){
overWrite.print(store[j].getFirst()+";");
overWrite.print(store[j].getMiddle()+";");
overWrite.print(store[j].getLast()+";");
overWrite.print(store[j].getStreet()+";");
overWrite.print(store[j].getCity()+";");
overWrite.print(store[j].getState()+";");
overWrite.print(store[j].getZip()+";");
overWrite.println(store[j].getId());
store[j] = null;
overWrite.flush();
break;
}
}
}
tmpFile = !tmpFile;
} catch (IOException e1) {
e1.printStackTrace();
}
lock.notifyAll();
System.out.println("Saved");
try {
lock.wait();
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
I know that it reaches the for loop where I take all of the elements in store[] and write them to a file, and it does print the correct data to the console. However, any time it modifies either NAIList.txt or NAIList.tmp, it just makes them completely blank. I have no clue why it isn't working since the code is essentially copied from a different method run on the main thread that did work as I wanted it to.
Ah, forgot to add, as an artificial limitation that my teacher gave us, we are only allowed to have Ten NameAddressID
objects in the RAM at any given time.
EDIT: Fixed code, only thing that changed is that overWrite.flush();
was added twice
Don't you need to flush the PrintWriter?
See the PrintWriter constructor's javadoc:
Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.