簡體   English   中英

在JTextField中使用bufferedReader時,如何在Java中顯示文件路徑?

[英]How to show the file path in Java when using a bufferedreader in JTextField?

按下選擇按鈕時選擇文件對話框

選擇文件后

我需要文件的絕對路徑在文本框中。 但是整個文件的內容都在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();
    }

}

請參閱復制文件或目錄以獲取更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM