Results 1 to 4 of 4
  1. #1
    bachtoutou is offline Member
    Join Date
    May 2008
    Posts
    11
    Rep Power
    0

    Default How to create a directory using the library java.io

    Hello evrybody
    T want to create a directory which name exists in a JtextField "app" and the path is in a JtextField "mod".This directory is create when i press the button Jbutton2.I have developed a code but it dosen't create anything
    Java Code:
    private void jButton2_actionPerformed(ActionEvent e) {
    File fichier=new File (app.getText());
                            File fichier_parent=new File (mod.getText()); 
                            fichier_parent.mkdirs();
    }

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What about mkdir() method?
    freedom exists in the world of ideas

  3. #3
    radtad82 is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    mkdirs is a better method to use, since it will create any parent directories as well.
    My guess is there's something wrong with the way the path is specified. Are you sure the complete path is specified?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import javax.swing.*;
    
    public class MakeFiles {
        private JPanel getContent() {
            final JTextField fileName = new JTextField(12);
            final JTextField parentName = new JTextField(12);
            JButton button = new JButton("make files");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String name = fileName.getText().trim();
                    String parent = parentName.getText().trim();
                    System.out.printf("check for file %s in %s%n",
                                       name, parent);
                    File file = new File(parent, name);
                    if(file.exists())
                        System.out.println("files exist");
                    else
                        file.mkdirs();
                }
            });
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            gbc.weighty = 1.0;
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            panel.add(new JLabel("file name"), gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(fileName, gbc);
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            panel.add(new JLabel("parent file name"), gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(parentName, gbc);
            gbc.gridwidth = 2;
            panel.add(button, gbc);
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MakeFiles().getContent());
            f.setSize(300,160);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. Replies: 3
    Last Post: 09-07-2009, 02:28 PM
  2. How to create directory through Java Code
    By Java Tip in forum java.io
    Replies: 1
    Last Post: 04-14-2009, 04:34 PM
  3. Which Java-XML library or technology is best for my needs?
    By nitingupta183 in forum Advanced Java
    Replies: 1
    Last Post: 03-15-2008, 07:34 AM
  4. Create a new directory in existing Zip archive
    By satishbejgum in forum Advanced Java
    Replies: 1
    Last Post: 12-23-2007, 07:05 AM
  5. Java Machine Learning Library 0.0.12
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-20-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •