I am working on a program that is going to calculate alla prime numbers between 2 and 10 000 and write this to a file.
People tell different ways on how to write to a file so I took one sugestion and tried it. At first i put my file creation in a loop with ended in the program recreating the file each time, then I took the file creation out of the loop and now I get errors.
Could someone tell me if this is the way to do it, if not, how should it be done, or how can i make this work?
To already have a file could also work and then just add/edit/overwrite it from my program.
public static void main(String[] arg){
int prime = 2;
File file = new File("out.txt"); // how should i do this part
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while(prime < 10000){
boolean isPrime = true;
for( int i = 2; i <= sqrt(prime); i++){
if( (prime%i) == 0 ){
isPrime = false;
break;
}
}
if(isPrime){
bw.write(prime + " "); // and this
}
prime++;
}
}