Reputation: 13
The problem with the code below is that, after running the application, "log.txt" is empty. Why? I looked over the code, but i can't find something wrong. package Main;
import java.io.*;
import java.util.*;
public class JavaApp1 {
public static void main(String[] args) throws IOException {
File file = new File("log.txt");
PrintWriter Log = new PrintWriter("log.txt");
int Line = 1;
Scanner ScanCycle = new Scanner(System.in);
System.out.println("Cate numere doriti sa fie afisate?");
int Cycle = ScanCycle.nextInt();
Scanner ScanRange = new Scanner(System.in);
System.out.println("Care este numarul maxim dorit?");
int Range = ScanRange.nextInt();
Random Generator = new Random();
for (int idx = 1; idx <= Cycle; ++idx){
int Value = Generator.nextInt(Range);
Log.println("(" + Line + ")" + "Number Generated: " + Value);
Line = Line + 1;
}
}
}
Upvotes: 1
Views: 413
Reputation: 69025
You need to flush your character stream out. Call close()
[which internally calls flush()
] or flush()
on your PrintWriter
instance.
PrintWriter log = new PrintWriter("log.txt");
//your code
log.close();
Upvotes: 1