如何在 Java 中將內容從 JTextField 儲存到檔案中?
是的,我們可以使用 FileWriter 類將內容儲存到檔案中。將 JTextFile 元件設定如下所示 −
JTextField emailId = new JTextField(20);
emailId.setText("abc@example.com");從 JTextField 設定檔案位置,以儲存內容 −
String file = "E:\
ew.txt";
現在,使用 FileWriter 儲存內容 −
FileWriter fileWriter = new FileWriter(file); emailId.write(fileWriter); fileWriter.close();
以下是一個示例,用於將內容從 JTextFile 儲存到檔案中。此處,我們將 JTextField 中的文字儲存在以下位置的檔案中:“E:\
ew.txt” −
示例
package my;
import java.awt.FlowLayout;
import java.io.FileWriter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class SwingDemo {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Demo");
JLabel label;
frame.setLayout(new FlowLayout());
label = new JLabel("EmailId : ", SwingConstants.LEFT);
JTextField emailId = new JTextField(20);
emailId.setText("abc@example.com");
String file = "E:\
ew.txt";
FileWriter fileWriter = new FileWriter(file);
emailId.write(fileWriter);
fileWriter.close();
frame.add(label);
frame.add(emailId);
frame.setSize(550,250);
frame.setVisible(true);
}
}輸出

現在,具有 JTextField 文字的檔案將儲存在指定位置,即 −
E:\
ew.txt
以下是從 JTextField 中的檔案內容 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP