将多个用户输入保存到文本文件(Saving multiple user inputs to a text file)

首先,如果已经解释过,我必须道歉......虽然我已经完成搜索以找到问题的答案,但在Java方面我仍然很新,所以我无法理解。 (我几天才学习Java。)

我的朋友正在试图教我Java,这使得我必须完成任务,并且我应该了解自己需要做些什么来完成这些任务。 最近,他给了我创建一个程序的挑战,该程序询问用户他们的姓名,年龄和用户名。 这个程序然后需要输出这些句子。 这部分很容易。 我遇到的困难是下一部分,我必须让程序将这些文件保存到一个文件中,以及来自用户的任何未来输入。

以下是我的:

import java.util.Scanner; public class easy1 { @SuppressWarnings("resource") public static void main(String[] args) { System.out.println("Enter your name, age and username: "); Scanner sc = new Scanner(System.in); String name = sc.next(), age = sc.next(), user = sc.next(); System.out.println("Your name is " + name + ", you are " + age + " years old, and your username is " + user + "."); } }

我并不是真的想找人告诉我该怎么做,我只会对我应该做的一点点指导或线索感兴趣。 如果你确实给我一个答案,那么解释它的工作原理是很好的。

谢谢。

Firstly I must apologize if this has already been explained... While I have done searches to find an answer to my problem, I am still very new when it comes to Java so I couldn't really understand. (I've only been learning Java for a couple of days.)

My friend, who is trying to teach me Java sets me assignments that I have to complete and I'm supposed to learn what I need to do to complete these assignments myself. Recently he gave me the challenge of creating a program that asks the user for their name, age and a username. This program then needs to output these in a sentence. That part is easy. What I'm having difficulty with is the next part where I have to make the program save these to a file, along with any future inputs from a user.

Here's what I have:

import java.util.Scanner; public class easy1 { @SuppressWarnings("resource") public static void main(String[] args) { System.out.println("Enter your name, age and username: "); Scanner sc = new Scanner(System.in); String name = sc.next(), age = sc.next(), user = sc.next(); System.out.println("Your name is " + name + ", you are " + age + " years old, and your username is " + user + "."); } }

I'm not really looking for anybody to tell me what to do, what I would appreciate is just a little guidance or a clue as to what I should be doing. If you do give me an answer then an explanation of how it works would be nice.

Thank you.

最满意答案

要保存到文件,您需要创建一个File对象。

File file = new File("someFile.txt"); -- takes file name as argument.

然后你需要一种打印到文件的方法。 您可以使用PrintWriter

PrintWriter writer = new PrintWriter(file); -- takes file as argument.

要写入文件,只需使用PrintWriter write()方法

writer.write(name);

如果你只是在学习的第二天,告诉你的朋友,了解I / O(输入/输出)还为时过早。 你应该学习loops, arrays, methods, operators, if/else if statements, Strings, etc 。 所有的基础知识。 一旦完成基础知识,就可以进入面向对象编程(OOP)。 这就是真正的魔法发生的地方。 因此,花时间学习基础知识,以便您可以为更深层次的东西做好准备。

拿起一本全面的书。 我推荐Introduction to Java Programming by Daniel liang的教科书Introduction to Java Programming by Daniel liang

To save to file you need to create a File object.

File file = new File("someFile.txt"); -- takes file name as argument.

Then you need a way to print to the file. You can use PrintWriter

PrintWriter writer = new PrintWriter(file); -- takes file as argument.

To write to the file, just use the PrintWriter write() method

writer.write(name);

If you're only in your second day of learning, tell your friend it's too early to be learning about I/O (input/output). You should be learning about loops, arrays, methods, operators, if/else if statements, Strings, etc. All the basics. Once you've finished the basics, you're ready to get into Object Oriented Programming (OOP). That's where the real magic happens. So take time to learn the basics, so you'll be ready for the deeper stuff.

Pick up a comprehensive book. I'd recommend a textbook Introduction to Java Programming by Daniel liang

更多推荐