简体   繁体   English

创建一个程序,从用户获取输入并将其写入输出文件

[英]Create a program that takes input from the user and writes it into the output file

This is what I have to do: 这就是我要做的事情:

Create a new copy of the TestFileWriter program called WriterDemo that takes input from the user and writes it into the output file. 创建一个名为WriterDemo的TestFileWriter程序的新副本,该程序从用户获取输入并将其写入输出文件。 The program should continue writing lines (a loop may help) until the user supplies an empty line (no text) as their input. 程序应继续写行(循环可能有帮助),直到用户提供空行(无文本)作为输入。 Hint: a while loop that has a termination condition that depends on the input string from the user is a good place to start... 提示:具有终止条件的while循环取决于来自用户的输入字符串是一个好的起点......

The program should be accessed from the terminal and I can't figure out where to put the while loop without ruining the program. 程序应该从终端访问,我无法弄清楚在不破坏程序的情况下放置while循环的位置。 The code below is the unmodified version of TestFileWriter. 下面的代码是TestFileWriter的未修改版本。 I don't need the full code of WriterDemo, but just some advice on how to use it. 我不需要WriterDemo的完整代码,只需要一些关于如何使用它的建议。 An help is greatly appreciated. 非常感谢帮助。

import java.io.FileReader;

import java.io.FileWriter;

public class WriterDemo {
public static void main(String args[]){;

FileWriter fout;
    FileReader fin;
    String str;
    int k;
    if(args.length==0){
        System.out.println("Use an argument in the command line");
        System.exit(0);
    }
    try{
        fout = new FileWriter("WrittingProbe.txt");
        for(int i=0; i<args.length; i++){
            fout.write(args[i]);
            fout.write(' ');
        }
        fout.close();

        fin= new FileReader("WrittingProbe.txt");
        System.out.println("The file content is:");
        while((k=fin.read()) !=-1) 
        System.out.println((char)k);
        System.out.println();
        fin.close();

        fout = new FileWriter("WrittingProbe.txt", true);
        str="\nAdded Text\n";
        fout.write(str);
        fout.close();

        fin = new FileReader("WrittingProbe.txt");
        System.out.println("\nNow the file content is:");
        while((k=fin.read()) != -1)
            System.out.print((char)k);
            System.out.println();
            fin.close();

    }
    catch(Exception e){
        System.out.println("Exception: " + e);
    }

}
}
    Scanner scn = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<String>();

    list.add(scn.nextLine());
    for(;!list.get(list.size()-1).equals("");){ //Loops until the last input is a blank line
        list.add(scn.nextLine());

    //Or, you can do it here, as you go, if you want
    }
    //Or here, all at once, using the list
 try (FileWriter fileWriter = new FileWriter("G:\\test.txt")) {
            Scanner scn = new Scanner(System.in);

            while (true) {
                String string = scn.nextLine();

                if (string.equals("0")) {
                    break;
                } else {
                    fileWriter.write(string+"\n");
                }
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个程序,收集用户的输入并将信息写入文件(output.txt) - HOW TO Write a program that gathers input from the user and writes the information out to a file (output.txt) 读取输入文本文件并将其写入特定输出文件的程序 - Program that reads input text file and writes to a specific output file 该程序从文本文件获取输入并返回json字符串 - Program which takes input from a text file and returns a json string 通过用户输入创建文件 - Create File From User Input 如何编写一个以用户标记为输入的程序? - How write a program that takes the marks of user as input? 创建jar文件,将输入文件接收到Main并在某个目录中输出输出文件 - Create jar file that takes in input file to Main and outputs an output file in some directory 如何将用户输入行从输入文件打印到输出文件 - How to print user input lines from an input file to an output file 我需要帮助来编写一个程序,该程序需要用户输入并使用堆栈进行反向操作? - I need help to write a program that takes input from user and reverses using a stack? Java程序,将一段作为用户输入,并在控制台上打印多个定界符(例如\\ n,空格等) - Java Program that takes a paragraph as input from user and prints number of delimiters (like \n, space, etc) on to the console 编写一个程序,将用户的输入作为数组接收,并将其与另一个数组进行比较,以使用 while 循环检查正确答案 - Writing a program that takes input from a user as an array and compares it to another array to check for correct answers using a while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM