【问题标题】:how to create different directories in one file | Java如何在一个文件中创建不同的目录|爪哇
【发布时间】:2016-07-08 06:12:54
【问题描述】:

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

我正在寻找一种仅通过文件即可完成的方法。

这也是我在文件处理方面的第 5 次练习,我是 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!!");
            }
        }
    }           
}

【问题讨论】:

  • 抱歉,这些信息不足以了解您遇到的问题。 “在文件中添加新路径” 是什么意思?你遇到了什么错误?如果您没有提供足够的信息,我们将无法为您提供帮助。
  • 你的路是什么?
  • @JimGarrison 更新了,伙计们请不要在那个问题上按那个向下箭头,我是新手,只是想寻求帮助:)
  • @eldo,更新了,兄弟,
  • 谢谢@Appleman,我已经更新了问题

标签: java file-handling


【解决方案1】:

代码应该是这样的:

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();
}

【讨论】:

  • 不一定应该是这样的。如果他想使用现有的文件,他为什么要创建任何类型的文件?为什么要在 try 块中声明所有内容?