0

I have several threads. Each thread haves a while(true) loop inside, where I add cycle-by-cycle text. I don't find a good method to change the while(true) loop with a flag, in such a way that I can close the file when I come out from the cycle. I want to do this when I type something for example, or when I press the Eclipse red button.

This is the constructor (Node is a Thread)

public Node(Channel c, int address) {
    my_address=address;
    try {
        writer = new CSVWriter(new FileWriter(my_address + "_id.csv"), ',', ' ' , ' ' ,"\n");
        writer2 = new CSVWriter(new FileWriter(my_address + "_label.csv"), ',', ' ' , ' ' ,"\n");
        String[] entries = "num#state#duration#event#condition#condition result#action1#action2#backoff value".split("#");
        writer.writeNext(entries);
        writer2.writeNext(entries);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is the loop in which I modify the file:

while (true) {
        //write id value
        String id_to_split = num+"#"+fsm.current_state.nome+"#"+tempo_minore+"#"+
                fsm.current_transition.e.getId()+"#"+ fsm.current_transition.c.getId()+"#"+
                fsm.current_transition.c.getFlag()+"#"+fsm.current_transition.a.getId()+"#"+
                fsm.current_transition.a2.getId()+"#"+backoff;

        String[] id_entries = id_to_split.split("#");
        writer.writeNext(id_entries);

        //write name
        String label_to_split = num+"#"+fsm.current_state.nome+"#"+tempo_minore+"#"+
                fsm.current_transition.e.getLabel()+"#"+fsm.current_transition.c.getLabel()+"#"+
                fsm.current_transition.c.getFlag()+"#"+fsm.current_transition.a.getLabel()+"#"+
                fsm.current_transition.a2.getLabel()+"#"+backoff;

        String[] label_entries = label_to_split.split("#");

        writer2.writeNext(label_entries);

        num++;
}
    closeCSVs();
}

The method closeCSVs():

    public void closeCSVs() {
    try {
        writer.close();
        writer2.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
4
  • 1
    Please post some code examples. Commented Feb 3, 2017 at 12:22
  • How about closing the file after the loop and breaking the loop whenever a certain condition is met? What condition is to be checked and how is up to you. I'd also not open the file stream in the constructor (of the thread?) but right before entering the loop, if that is possible. Commented Feb 3, 2017 at 12:27
  • To fix the code to do what you want, you will most likely need to change. Commented Feb 3, 2017 at 12:29
  • or when I pressed the Eclipse red button This simply kill the process so there is no clear solution for that. Commented Feb 3, 2017 at 12:42

2 Answers 2

2

If I understood your question correctly, what you're looking for is either a try-with-resources block which works like following:

try(FileReader reader = new FileReader("path")) {
    while(true) {
        //use resources
    }
}

You can use this with any Class that implements the AutoClosable-Interface (basically every class that offers a .close()-Method). The resource will be closed automatically after the try-block is escaped.

Same solution different code would be to wrap with a classic try and adding a finally block to it.

try {
    FileReader reader = new FileReader("path");
    while(true) {
        //use resources
    }
} finally {
    reader.close();
}
Sign up to request clarification or add additional context in comments.

16 Comments

It is good practice for the first bit of code to also add the finally and close the FileReader there.
@TotumusMaximus, try-with resources will do it since it is AutoClosable
You are right. I would prefer the classic try-catch-finally over try with resources anyways since i think is more readable and also I found that it performed way better on I/O. Not sure if that is generally true though.
I have to write lines on the file, inside the while loop, and I look for a good method to stop the while cycle, for example when I press a key.
Okay so i think the simplest way for your solution would be a while(flag) loop, the boolean flag initially is true and gets set to false by console input (use a scanner with system.in) and simply put a check for scanner.hasNext() in the while-loop.
|
0

You may need to implement closing file in shutdown hook Runtime.addShutdownHook

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.