如何在一个文件中创建不同的目录Java的

时间:2016-07-08 06:12:54

标签: java file-handling

问题: - 我必须创建新文件,file1用于创建两个新目录。

我正在寻找一种方法,我可以通过文件来完成它。

这也是我的第五次文件处理练习,我是java的新手,对我的代码的任何建议都是最受欢迎的

代码:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;

class CreateUser implements ActionListener
{   
    JFrame fr, fr1; //Frame
    JButton b1;  //Create Button
    JLabel lb1, lb2, lb3;   //Username and password
    JTextField tf;  //Username and password input fields
    JPasswordField pf;
    JPanel p1;

    CreateUser()
    {
        //Setting the frame
        fr=new JFrame();
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        fr.setLayout(null);
        fr.setSize(400,400);

        //setting panel
        p1=new JPanel();
        p1.setBounds(0,0,400,400);
        p1.setLayout(null);

        //setting Username Label
        JLabel lb1=new JLabel("Username: ");
        lb1.setBounds(50,50,70,30);
        p1.add(lb1);

        //setting Username Text Field
        tf= new JTextField();
        tf.setBounds(150,50,150,30);
        p1.add(tf);


        //setting Password Label
        JLabel lb2=new JLabel("Password: ");
        lb2.setBounds(50,100,70,30);
        p1.add(lb2);

        //setting Password Text Field
        pf = new JPasswordField();
        pf.setBounds(150,100,150,30);
        p1.add(pf);

        //setting Button
        b1=new JButton("Create");
        b1.setBounds(100,200,100,40);   
        p1.add(b1);

        fr.add(p1);
        fr.setVisible(true);    
        b1.addActionListener(this);
        tf.addActionListener(this);
        pf.addActionListener(this);

    }

    public static void main(String[] s)
    {
        SwingUtilities.invokeLater(() -> new CreateUser());
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {   
            String uid = tf.getText();
            String pass = String.valueOf(pf.getPassword());


            //stores current file path to dir
            String dir = System.getProperty("user.dir");

            //Creating a new Users folder if doesn't exists

            File file = new File(dir=dir+"\\users");
            if (!file.exists()) 
            {
                if (file.mkdir()) 
                {
                    System.out.println("Directory is created!");
                } 
                else 
                {
                    System.out.println("Failed to create directory!");
                }
            }
            String dir1 = dir+"\\users";

            //Creating a folder named with username inside Users folder

            File file1 = new File(dir=dir+"\\"+uid);
            if(file1.exists())
            {
                JOptionPane.showMessageDialog(null, "User Already Exists!! \nPlease choose different Username!");
            }
            else
            //if (!file1.exists()) 
            {
                if (file1.mkdir()) 
                {
                    System.out.println("Directory is created!");
                } 
                else 
                {
                    System.out.println("Failed to create directory!");
                }
            }

            //Storing Password.txt inside users/username folder
            try
            {
                FileOutputStream fout=new FileOutputStream(dir+"\\password.txt");
                byte b[]=pass.getBytes();
                fout.write(b);
            }
            catch(Exception ee)
            {
                System.out.println("Exception Found in password fie output!!");
            }
        }
    }           
}

1 个答案:

答案 0 :(得分:0)

代码应该是:

try {

String filename = "newFile.txt";
String workingDirectory = System.getProperty("user.dir");

//****************//

String absoluteFilePath = "";

//absoluteFilePath = workingDirectory + System.getProperty("file.separator") + filename;
absoluteFilePath = workingDirectory + File.separator + filename;

System.out.println("Final filepath : " + absoluteFilePath);

//****************//

File file = new File(absoluteFilePath);

if (file.createNewFile()) {
    System.out.println("File is created!");
} else {
    System.out.println("File is already existed!");
}

} catch (IOException e) {
  e.printStackTrace();
}