写文件方法

时间:2014-04-13 00:54:59

标签: java file-io

还好。我必须实现writeFile方法来打开输出文件,用“输入文本:”提示用户,然后通过键盘读取用户的输入,将输入打印到文件,并克隆输出文件。当用户在其自己的行上单独键入stop(大写或小写)时,该方法应该终止。我让userinput转到小写,因此它可以灵活。这是我到目前为止,我不知道我需要下一步

static void writeFile( String outputFile )
 { 
     try
         {
PrintWriter pwOut = new PrintWriter(outputFile);
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Text:");
String userinput = keyboard.nextLine();

while (true)
{
    if (userinput.toLowerCase().equals("stop"))
    {
        pwOut.print(userinput);
        keyboard.close();
        pwOut.close();
        break; 
    }
    else
    {
        keyboard.close();
        pwOut.close();
    }

1 个答案:

答案 0 :(得分:1)

好吧,下次试着提问。刚刚开始:

static void writeFile(String outputFile) throws FileNotFoundException {
    Scanner keyboard = new Scanner(System.in);
    PrintWriter pwOut = new PrintWriter(outputFile);

    System.out.println("Enter Text:");

    while (true) {
        String userinput = keyboard.nextLine();
        if (!userinput.toLowerCase().equals("stop")) {
            pwOut.print(userinput);
        } else {
            break;
        }
    }

    keyboard.close();
    pwOut.close();
}