简体   繁体   English

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

[英]Relative path for browsing folder in java

I want to modify to browse the folder through the relative path. 我想要修改以通过相对路径浏览文件夹。 The code only browse through the absolute path and check only the provided directory is empty or not...plz help....thanks in advance 该代码仅浏览绝对路径,并仅检查提供的目录是否为空... PLZ帮助....在此先感谢

package fyp; 包fyp;

import java.awt.Color; 导入java.awt.Color;

public class Frame2 extends JFrame { 公共类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);
}

} }

When it comes to files usually you at least want to have a try{} and catch{} . 当涉及到文件时,通常至少要try{}catch{}

Hints: if you want to find the directory of a file, try file.getParent() or file.getParentFile() . 提示:如果要查找文件的目录,请尝试file.getParent()file.getParentFile() Additionally, you should check to see if the file exists .exists() or isDirectory() . 此外,您应该检查文件是否存在.exists()isDirectory()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM