gpt4 book ai didi

java - 我的日志文件是空的

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:21 26 4
gpt4 key购买 nike

下面代码的问题是,运行应用程序后,“log.txt”为空。为什么?我查看了代码,但找不到任何错误。 包主要;

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;
}
}
}

最佳答案

您需要刷新字符流。在您的 PrintWriter 实例上调用 close()[内部调用 flush()] 或 flush()

    PrintWriter log = new PrintWriter("log.txt");
//your code
log.close();

关于java - 我的日志文件是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28545589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com