Select File Dialog Box when Pressing a select button
我需要文件的绝对路径位于文本框中。但整个文件内容都在 JTextField
中。
这是我的完整代码:
public class NewWork
{
public JFrame Frame;
public JLabel textLabel;
public JButton readButton, writeButton, printButton;
public JTextField textField;
public FileReader reader;
public FileWriter writer;
public BufferedReader br;
public BufferedWriter bw;
public void PrintFrame()
{
Frame = new JFrame();
Frame.setSize(500, 300);
//Frame.pack();
Frame.setBackground(Color.BLUE);
textLabel = new JLabel();
textLabel.setText("Selected File Path:");
textLabel.setBounds(30, 30, 300, 30);
textLabel.setVisible(true);
textField = new JTextField();
textField.setBounds(30, 70, 300, 30);
textField.setVisible(true);
readButton = new JButton();
readButton.setBounds(350, 70, 100, 30);
readButton.setText("Select File");
readButton.setVisible(true);
readButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser FileChooser = new JFileChooser();
FileChooser.showOpenDialog(null);
File f= FileChooser.getSelectedFile();
String path= f.getAbsolutePath();
try
{
reader = new FileReader(path);
br = new BufferedReader(reader);
textField.read(br, null);
br.close();
textField.requestFocus();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, e1);
// Check Later
JOptionPane.showMessageDialog(FileChooser, e1);
}
}
});
writeButton = new JButton();
writeButton.setBounds(30, 130, 100, 30);
writeButton.setText("Write");
writeButton.setVisible(true);
writeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
writer = new FileWriter("E://new.txt");
bw = new BufferedWriter(writer);
textField.write(bw);
bw.close();
textField.setText("");
textField.requestFocus();
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null, e2);
}
}
});
printButton = new JButton();
printButton.setBounds(190, 130, 100, 30);
printButton.setText("Print Setup");
printButton.setVisible(true);
printButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
boolean complete = textField.print();
if(complete)
{
JOptionPane.showMessageDialog(null, "Done Printing!");
}
else
{
JOptionPane.showMessageDialog(null, " Printing!");
}
}
catch(Exception e3)
{
JOptionPane.showMessageDialog(null, e3);
}
}
});
Frame.add(textLabel);
Frame.add(textField);
Frame.add(readButton);
//Frame.add(writeButton);
Frame.add(printButton);
Frame.setLayout(null);
Frame.setLocationRelativeTo(null);
Frame.setVisible(true);
Frame.setResizable(false);
//
}
public static void main(String[] args)
{
NewWork nw = new NewWork();
nw.PrintFrame();
//System.out.println("Hi");
}
}
最佳答案
这个...
reader = new FileReader(path);
br = new BufferedReader(reader);
textField.read(br, null);
br.close();
textField.requestFocus();
会将整个文件内容读取到JTextField
中,这显然不是您想要做的。
相反,您希望获取文件的路径并将其放入 JTextField
中,但使用实例字段来跟踪所选文件...
private File selectedFile;
//...
selectedFile = null;
JFileChooser FileChooser = new JFileChooser();
if (FileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = FileChooser.getSelectedFile();
String path= f.getAbsolutePath();
textField.setText(path);
}
然后,当您准备好复制/写入文件时,您可以使用类似...
if (selectedFile != null) {
try {
Files.copy(
selectedFile.toPath(),
new File("E:/new.txt").toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
ex.printStackTrace();
}
}
看看Copying a File or Directory了解更多详情
关于java - 在 JTextField 中使用 bufferedreader 时如何显示 Java 中的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181672/