如何在eclipse控制台上保存用户输入以保存在文件中?

时间:2018-01-04 05:50:51

标签: java eclipse user-interface console-application user-input

我刚开始学习。所以请承担我的错误。

我想创建一个程序,我可以要求用户写入控制台,用户的输入将保存到文本文件中。简而言之,编写一个将创建,编写和保存文件的程序。

这是基本的,请专家为我提供一些替代方案,这个方案看起来像一个先进的,任何调制,我可以通过它来改进或扩展程序的功能。那对我来说这将是一个很好的教学。提前致谢。

public class MainClass {

public static void main(String[] args) throws IOException {

    //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    Scanner sc = new Scanner(System.in);

    File myfile = new File("C:\\Program Files\\Myfilenew.txt");

    if(!myfile.exists())
        myfile.createNewFile();
    else
        myfile.delete();


    try {
        PrintWriter pwrt = new PrintWriter(myfile);
        pwrt.println("This is my first java created text file");
        System.out.println("Enter the data to be writter into the text file"+"\n"+"Type 'End' to save and exit the file: "+"\n");

        while(sc.hasNext()){
            if(!sc.hasNext("End"))
                pwrt.println(sc.nextLine());
            else
                break;
        }

        System.out.println("Done");
        pwrt.close();
        sc.close();

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } 

}

0 个答案:

没有答案
相关问题