当前位置: 代码迷 >> java >> 在Java中浏览文件夹的相对路径
  详细解决方案

在Java中浏览文件夹的相对路径

热度:64   发布时间:2023-08-02 10:56:11.0

我想要修改以通过相对路径浏览文件夹。 该代码仅浏览绝对路径,并仅检查提供的目录是否为空... PLZ帮助....在此先感谢

包fyp;

导入java.awt.Color;

公共类Frame2扩展了JFrame {

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame2 frame = new Frame2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });



}

/**
 * Create the frame.
 */
public Frame2() {
    textField = new JTextField();
    textField.setBounds(92, 107, 272, 20);
    setTitle("Plagiarm Detection System\r\n");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 600, 300);
    contentPane = new JPanel();
    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnBrowse = new JButton("Browse");
    btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(new java.io.File("."));
                chooser.setDialogTitle("Browse the folder to process");
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.setAcceptAllFileFilterUsed(true);

               File file=new File("C:/Users/Shahzaib/Desktop/empty");
                //File relative=new File("C:/Users/Shahzaib/Desktop/empty");


               //System.out.println(relative.getName());
               //System.out.println(relative.getPath());


                //File file = new File("directory"); 

                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
                {
                    textField.setText(chooser.getSelectedFile().toString());
                    //System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
                    //System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());


                }
                if(file.isDirectory()){

                    if(file.list().length>0){

                        Component frame = null;
                        JOptionPane.showMessageDialog(frame, "Directory is  Not Empty.");

                    }else{

                        Component frame = null;
                        JOptionPane.showMessageDialog(frame, "Directory is Empty.");

                    }


                //else 
                //{
                  // Component frame = null;
                    //JOptionPane.showMessageDialog(frame, "No Selection.");


                }


            }


        });








        getContentPane().add(btnBrowse);
    btnBrowse.setBounds(374, 106, 89, 23);
    contentPane.add(btnBrowse);

    contentPane.add(textField);
    textField.setColumns(10);

    JButton btnProceed = new JButton("Next");
    btnProceed.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
            new Frame2().setVisible(true);
        }
    });
    btnProceed.setBounds(485, 227, 89, 23);
    contentPane.add(btnProceed);

    JButton btnBack = new JButton("Cancel");
    btnBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            dispose();
            new Frame().setVisible(true);
        }
    });
    btnBack.setBounds(374, 227, 89, 23);
    contentPane.add(btnBack);

    JLabel lblNewLabel = new JLabel("Choose Folder of Assignments");
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblNewLabel.setBounds(180, 11, 235, 50);
    contentPane.add(lblNewLabel);
}

}

当涉及到文件时,通常至少要try{}catch{}

提示:如果要查找文件的目录,请尝试file.getParent()file.getParentFile() 此外,您应该检查文件是否存在.exists()isDirectory()

  相关解决方案