문제

Is there any way you can change the name of a file each time doing a loop? Lets say I've got the following sample code:

while(somecondition) {    
    try (PrintWriter w = new PrintWriter("output.txt", "UTF-8");){
        w.println("Bye World");
    } 
}

Now i want to change the name of output.txt each time the loop starts again to output++.txt.

I would now receive:

output1.txt
output2.txt
output3.txt
outputn.txt
도움이 되었습니까?

해결책

You can use a for loop:

for (int i = 1; i <= n && someCondition; i++) {    
    try (PrintWriter w = new PrintWriter("output" + i + ".txt", "UTF-8");){
        w.println("Bye World");
    } 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top