How To Create File Or Directory In Java
Project Source Code:
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Java_Create_File_Or_Directory extends javax.swing.JFrame {
/**
* Creates new form Java_Create_File_Or_Directory
*/
public Java_Create_File_Or_Directory() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextFieldPath = new javax.swing.JTextField();
jButtonCreate_File = new javax.swing.JButton();
jButtonCreate_Directory = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextFieldPath.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jTextFieldPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldPathActionPerformed(evt);
}
});
jButtonCreate_File.setText("Create File");
jButtonCreate_File.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCreate_FileActionPerformed(evt);
}
});
jButtonCreate_Directory.setText("Create Directory");
jButtonCreate_Directory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCreate_DirectoryActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jTextFieldPath, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jButtonCreate_File, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonCreate_Directory, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(45, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(jTextFieldPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonCreate_File)
.addComponent(jButtonCreate_Directory))
.addContainerGap(45, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextFieldPathActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButtonCreate_FileActionPerformed(java.awt.event.ActionEvent evt) {
File f = new File(jTextFieldPath.getText());
if(!f.exists())
{
try {
f.createNewFile();
f.mkdir();
JOptionPane.showMessageDialog(null, "File Created");
} catch (IOException ex) {
Logger.getLogger(Java_Create_File_Or_Directory.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
JOptionPane.showMessageDialog(null, "File Already Exists");
}
}
private void jButtonCreate_DirectoryActionPerformed(java.awt.event.ActionEvent evt) {
File directory = new File(jTextFieldPath.getText());
if(!directory.exists())
{
directory.mkdir();
JOptionPane.showMessageDialog(null, "directory Created");
}
else
{
JOptionPane.showMessageDialog(null, "directory Already Exists");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Java_Create_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Java_Create_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Java_Create_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Java_Create_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Java_Create_File_Or_Directory().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonCreate_Directory;
private javax.swing.JButton jButtonCreate_File;
private javax.swing.JTextField jTextFieldPath;
// End of variables declaration
}