以下代码的问题是,在运行应用程序后,“log.txt”为空。为什么? 我查看了代码,但我找不到错误。 包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;
}
}
}
答案 0 :(得分:1)
您需要清除角色流。在close()
个实例上拨打flush()
[内部呼叫flush()
]或PrintWriter
。
PrintWriter log = new PrintWriter("log.txt");
//your code
log.close();