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

时间:2019-10-23 23:14:17

标签: java

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

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

但是发生的是代码没有写入文件,然后在运行时文件不打印

1 个答案:

答案 0 :(得分:-1)

PrintWriter构造函数中,使用FileOutputStream包装true参数后,将File作为第二个参数传递(将参数化的构造函数与{{1}一起使用}参数和File作为true的第二个参数)
FileOutputStream构造函数中的true参数用于启用自动刷新功能,当您使用PrintWriterprintlnprintf之类的方法(但不能使用{ {1}},在这种情况下,您必须手动调用format方法)
Here is the documentation for that