在JTextField中使用bufferedreader时,如何用Java显示文件路径?
我需要文件的绝对路径在文本框中。但是整个文件内容都出现在在JTextField中使用bufferedreader时,如何用Java显示文件路径?,java,swing,Java,Swing,我需要文件的绝对路径在文本框中。但是整个文件内容都出现在JTextField中 这是我的全部代码: public class NewWork { public JFrame Frame; public JLabel textLabel; public JButton readButton, writeButton, printButton; public JTextField textField; public FileReader reader;
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();
}
}
查看更多详细信息好的,您认为
textField.read(br,null)
在做什么?与其将文件内容读取到文本字段,不如使用file#getAbsolutePath。当您准备好编写文件时,可以使用