gpt4 book ai didi

java - 程序执行时无法正确写入/读取文件

转载 作者:行者123 更新时间:2023-12-01 06:01:12 25 4
gpt4 key购买 nike

我有一个程序,应该将内容写入文件,然后在程序末尾显示相同的文件。我有一个较小的测试程序,可以执行相同的操作并且可以工作,但原始程序无法工作。

while( input != 5 )
{
System.out.println();
System.out.print("Enter Selection (1-5): ");
input = in.nextInt();

File myFile = new File ("password.txt");
PrintWriter outFile = new PrintWriter(myFile);

if(input == 1 )
{
System.out.print("Password Length (6 +): ");
inputLength = in.nextInt();
if(inputLength >= 6)
{
for(int codeLength = 0; codeLength <= inputLength;)
{
randNum = random.nextInt(123);
if(randNum >= 97 && randNum <= 122)
{
code+=(char)randNum;
codeLength++;
}
else if(randNum >= 48 && randNum <= 57)
{
code+=(char)randNum;
codeLength++;
}

}

outFile.println(" " + codeNum + "\t" + code);
outFile.close();
}
else
System.out.println("The password is too short");
}
else if(input == 2)
{
System.out.print("Password Length (6 +): ");
inputLength = in.nextInt();
if(inputLength >= 6)
{
for(int codeLength = 0; codeLength <= inputLength;)
{
randNum = random.nextInt(123);
if(randNum >= 65 && randNum <= 90)
{
code+=(char)randNum;
codeLength++;
}
else if(randNum >= 48 && randNum <= 57)
{
code+=(char)randNum;
codeLength++;
}
}

outFile.println(" " + codeNum + "\t" + code);
outFile.close();
}
else
System.out.println("The password is too short");
}
else if(input == 3 )
{
System.out.print("Password Length (6 +): ");
inputLength = in.nextInt();
if(inputLength >= 6)
{
for(int codeLength = 0; codeLength <= inputLength;)
{
randNum = random.nextInt(123);
if(randNum >= 33 && randNum <= 47)
{
code+=(char)randNum;
codeLength++;
}
else if(randNum >= 48 && randNum <= 57)
{
code+=(char)randNum;
codeLength++;
}
}

outFile.println(" " + codeNum + "\t" + code);
outFile.close();
}
else
System.out.println("The password is too short");
}
else if(input == 4 )
{
System.out.print("Password Length (6 +): ");
inputLength = in.nextInt();
if(inputLength >= 6)
{
for(int codeLength = 0; codeLength <= inputLength;)
{
randNum = random.nextInt(123);
if(randNum >= 33 && randNum <= 126)
{
code+=(char)randNum;
codeLength++;
}
else if(randNum >= 48 && randNum <= 57)
{
code+=(char)randNum;
codeLength++;
}
}

outFile.println(code);
outFile.close();
}
else
System.out.println("The password is too short");
}
else if(input == 5 )
{
System.out.print("Thank you for using Fast Pass :)\n");
}
else
{
System.out.println("Invalid option. Try again.");
}
System.out.println();
System.out.println("Here are your randomly generated codes:");

System.out.println(code);
Scanner inFile = new Scanner("password.txt");
while( inFile.hasNext() )
{
String file = inFile.next();
System.out.println(file);
}

应该发生的是显示如下代码 1 -------- 2--------

但是发生的情况是代码没有写入文件,因此运行时文件不会打印

最佳答案

PrintWriter 构造函数中,使用 FileOutputStream 包装 File 参数后,传入 true 作为第二个参数。 (使用参数化构造函数,将 File 参数和 true 作为 FileOutputStream 的第二个参数)
PrintWriter 构造函数中的 true 参数用于启用自动刷新,当您使用 printlnprintf 等方法时它会起作用> 或 format (但不是 write,在这种情况下您必须手动调用 flush 方法)
Here is the documentation for that

关于java - 程序执行时无法正确写入/读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58532093/

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