Java 文件写入问题-文件为空
这是代码,我在创建文件时遇到问题,我在键入内容时,它在文件中显示为空白。您是在事件驱动环境中操作的,这意味着在创建组件和显示UI以及发生某些事情之间,时间已经过了 这意味着当你做一些像Java 文件写入问题-文件为空,java,swing,file,file-io,io,Java,Swing,File,File Io,Io,这是代码,我在创建文件时遇到问题,我在键入内容时,它在文件中显示为空白。您是在事件驱动环境中操作的,这意味着在创建组件和显示UI以及发生某些事情之间,时间已经过了 这意味着当你做一些像 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; impor
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.CardLayout;
import javax.swing.JSplitPane;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import java.awt.Color;
import java.awt.Font;
import javax.swing.DropMode;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;
public class Program extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Program frame = new Program();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws BadLocationException
*/
public Program() throws BadLocationException {
double points = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 1200, 760);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JEditorPane dtrpnTypeTextHere = new JEditorPane();
dtrpnTypeTextHere.setContentType("type/normal");
dtrpnTypeTextHere.setToolTipText("");
dtrpnTypeTextHere.setFont(new Font("Arial", Font.PLAIN, 16));
dtrpnTypeTextHere.setForeground(Color.GREEN);
dtrpnTypeTextHere.setBackground(Color.BLACK);
dtrpnTypeTextHere.setBounds(10, 23, 1152, 671);
contentPane.add(dtrpnTypeTextHere);
int length = dtrpnTypeTextHere.getDocument().getLength();
String text = dtrpnTypeTextHere.getDocument().getText(0, length);
JLabel lblNewLabel = new JLabel("Power Text Editor");
lblNewLabel.setBackground(Color.BLACK);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setForeground(Color.GREEN);
lblNewLabel.setBounds(515, 0, 125, 14);
contentPane.add(lblNewLabel);
JLabel lblFileName = new JLabel("File Name:");
lblFileName.setBounds(660, 703, 75, 14);
contentPane.add(lblFileName);
JButton btnFinishEditing = new JButton("Finish editing");
btnFinishEditing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedWriter writer = null;
try {
//create a temporary file
File logFile = new File("yas.txt");
// This will output the full path where the file will be written to...
System.out.println(logFile.getCanonicalPath());
writer = new BufferedWriter(new FileWriter(logFile));
writer.write(text);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
// Close the writer regardless of what happens...
writer.close();
} catch (Exception e1) {
}
System.out.println("Quit because of user exit");
System.exit(0);
}
}
});
btnFinishEditing.setForeground(Color.GREEN);
btnFinishEditing.setBackground(Color.BLACK);
btnFinishEditing.setBounds(532, 699, 108, 23);
contentPane.add(btnFinishEditing);
textField_1 = new JTextField();
textField_1.setBounds(726, 700, 131, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
}
}
JEditorPane dtrpnTypeTextHere = new JEditorPane();
dtrpnTypeTextHere.setContentType("type/normal");
dtrpnTypeTextHere.setToolTipText("");
dtrpnTypeTextHere.setFont(new Font("Arial", Font.PLAIN, 16));
dtrpnTypeTextHere.setForeground(Color.GREEN);
dtrpnTypeTextHere.setBackground(Color.BLACK);
dtrpnTypeTextHere.setBounds(10, 23, 1152, 671);
contentPane.add(dtrpnTypeTextHere);
int length = dtrpnTypeTextHere.getDocument().getLength();
String text = dtrpnTypeTextHere.getDocument().getText(0, length);
然后像
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.CardLayout;
import javax.swing.JSplitPane;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import java.awt.Color;
import java.awt.Font;
import javax.swing.DropMode;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;
public class Program extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Program frame = new Program();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws BadLocationException
*/
public Program() throws BadLocationException {
double points = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 1200, 760);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JEditorPane dtrpnTypeTextHere = new JEditorPane();
dtrpnTypeTextHere.setContentType("type/normal");
dtrpnTypeTextHere.setToolTipText("");
dtrpnTypeTextHere.setFont(new Font("Arial", Font.PLAIN, 16));
dtrpnTypeTextHere.setForeground(Color.GREEN);
dtrpnTypeTextHere.setBackground(Color.BLACK);
dtrpnTypeTextHere.setBounds(10, 23, 1152, 671);
contentPane.add(dtrpnTypeTextHere);
int length = dtrpnTypeTextHere.getDocument().getLength();
String text = dtrpnTypeTextHere.getDocument().getText(0, length);
JLabel lblNewLabel = new JLabel("Power Text Editor");
lblNewLabel.setBackground(Color.BLACK);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setForeground(Color.GREEN);
lblNewLabel.setBounds(515, 0, 125, 14);
contentPane.add(lblNewLabel);
JLabel lblFileName = new JLabel("File Name:");
lblFileName.setBounds(660, 703, 75, 14);
contentPane.add(lblFileName);
JButton btnFinishEditing = new JButton("Finish editing");
btnFinishEditing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedWriter writer = null;
try {
//create a temporary file
File logFile = new File("yas.txt");
// This will output the full path where the file will be written to...
System.out.println(logFile.getCanonicalPath());
writer = new BufferedWriter(new FileWriter(logFile));
writer.write(text);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
// Close the writer regardless of what happens...
writer.close();
} catch (Exception e1) {
}
System.out.println("Quit because of user exit");
System.exit(0);
}
}
});
btnFinishEditing.setForeground(Color.GREEN);
btnFinishEditing.setBackground(Color.BLACK);
btnFinishEditing.setBounds(532, 699, 108, 23);
contentPane.add(btnFinishEditing);
textField_1 = new JTextField();
textField_1.setBounds(726, 700, 131, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
}
}
JEditorPane dtrpnTypeTextHere = new JEditorPane();
dtrpnTypeTextHere.setContentType("type/normal");
dtrpnTypeTextHere.setToolTipText("");
dtrpnTypeTextHere.setFont(new Font("Arial", Font.PLAIN, 16));
dtrpnTypeTextHere.setForeground(Color.GREEN);
dtrpnTypeTextHere.setBackground(Color.BLACK);
dtrpnTypeTextHere.setBounds(10, 23, 1152, 671);
contentPane.add(dtrpnTypeTextHere);
int length = dtrpnTypeTextHere.getDocument().getLength();
String text = dtrpnTypeTextHere.getDocument().getText(0, length);
发生这种情况时,text
的值不会更改,但JEditorPane
的内容会更改。您分配给text
的值与JEditorPane
的内容之间没有任何关系,除非您及时这样做
所以,与其这样做
btnFinishEditing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedWriter writer = null;
try {
//create a temporary file
File logFile = new File("yas.txt");
// This will output the full path where the file will be written to...
System.out.println(logFile.getCanonicalPath());
writer = new BufferedWriter(new FileWriter(logFile));
writer.write(text);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
// Close the writer regardless of what happens...
writer.close();
} catch (Exception e1) {
}
System.out.println("Quit because of user exit");
System.exit(0);
}
}
});
在构造函数中,您应该在ActionListener
中执行此操作
int length = dtrpnTypeTextHere.getDocument().getLength();
String text = dtrpnTypeTextHere.getDocument().getText(0, length);
举个例子
您可能还想看看,这将降低文件编写的复杂性try catch finally
block;)
作为补充说明,避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing旨在与核心的布局管理器一起工作,丢弃它们将导致无休止的问题,您将花费越来越多的时间试图纠正这些问题。您的方法程序将在程序开始时运行一次以设置UI。您正在程序运行时声明和设置变量文本。如果在初始化程序时没有键入任何文本,则文件中总是会出现一个空字符串
您需要做的是将变量文本和读取文本的代码从JEditorPane dtrpnTypeTextHere
移动到操作的开头。contentPane.setLayout(null)代码>Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。请同时查看。。