Java 如何将 JTextField 中的内容保存到文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19000737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:15:05  来源:igfitidea点击:

How to save content from a JTextField into a file

javaswingfile-iojtextfieldjtextarea

提问by Oliver Bath

I am doing a project where i am inputting recipes and their corresponding info, (title, ingredients, utilities, directions) in a JFrame, and then i want to save that content to a file. The fields where the user inputs the data are(title,ingredients etc.) and they are JTextFields. What i need to know is how the text from that JTextField can be written to a new file. I would prefer that each field has its own file, so the title of the recipe alone would have its own file, e.g stirfry.txt, and then the content would be "stirfry"

我正在做一个项目,我在 JFrame 中输入食谱及其相应的信息(标题、成分、实用程序、方向),然后我想将该内容保存到文件中。用户输入数据的字段是(标题、成分等),它们是 JTextFields。我需要知道的是如何将来自 JTextField 的文本写入新文件。我更希望每个字段都有自己的文件,因此仅菜谱的标题就有自己的文件,例如stirfry.txt,然后内容将是“stirfry”

Any feedback would be much appreciated

任何反馈将不胜感激

采纳答案by Micha?l Benjamin Saerens

Get the content of your textfield using the getText() function Create a new file using java.io and write the content of the file using a BufferedStream.

使用 getText() 函数获取文本字段的内容使用 java.io 创建一个新文件并使用 BufferedStream 写入文件的内容。

回答by Jiji

Would this help?

这会有帮助吗?

void saveToFile(String fileName, JTextField textField) throws Exception {
   FileOutputStream out = new FileOutputStream(fileName, true);
   out.write(textField.getText().getBytes());
} 

回答by user2795605

Try

尝试

JTextField titleTextField = ...;
String title = titleTextField.getText();
FileWriter fw = new FileWriter(new File(title+".txt"));
fw.write(title);
fw.close();

回答by MadProgrammer

The question depends on the format you want the file to take.

问题取决于您希望文件采用的格式。

The text of the fields can retrieved by using JTextField#getText. Take a look at How to use text fieldsfor more details...

可以使用 检索字段的文本JTextField#getText。查看如何使用文本字段以获取更多详细信息...

If you simply have a key/name pair of values, for example, you could simply add each of the values from your text fields into a Propertiesobject and utilise it's savefunctionality. The has the added benefit of being able to load the file again via the PropertiesAPI. Take a look at the Propertiestutorial for more details

例如,如果您只有一个键/名称值对,您可以简单地将文本字段中的每个值添加到一个Properties对象中并利用它的save功能。具有能够通过PropertiesAPI再次加载文件的额外好处。查看属性教程以获取更多详细信息

This does, however assume, that each element is unique.

然而,这确实假设每个元素都是唯一的。

If you're after a more complex or flexible data structure you may need to use XML. Luckily, Java has a bindings API which can allow you to transform an object to and from XML. Take a look at Introduction to JAXBfor more details.

如果您追求更复杂或更灵活的数据结构,您可能需要使用 XML。幸运的是,Java 有一个绑定 API,它允许您将对象转换为 XML 或从 XML 转换对象。有关更多详细信息,请查看JAXB 简介

The ultimate solution would be to use some kind of database ;)

最终的解决方案是使用某种数据库;)