• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5479
  • Last Modified:

Create directory/folder using JSP?

How to create a directory/folder in my website using JSP?

What is mean by System.getProperty("survey.dir")? How do i determine whether a directory is exist or not, if not then create a new directory.

Thanks.
0
trowa
Asked:
trowa
  • 6
  • 4
1 Solution
 
cheekycjCommented:
check out the File i/o tutorials here:
http://www.jspin.com/home/tutorials/filemani
0
 
cheekycjCommented:
did that link help or do you need specific code?

CJ
0
 
trowaAuthor Commented:
Thanks CJ, but which one should i try? The code there is just complicated for me! Yes, i do need specific code, thanks.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
cheekycjCommented:
Here is basic code:

<%@ page import="java.io.*,java.io.File" %>
<%
     File dir = new File("path/to/directory");
     if (dir.exists()) {
          if (dir.isDirectory()) {
               // path exists and is a directory
          }
          else {
               // path does exist but is not a directory -- probably just a file
          }
     }
     else {
          // path does not exist so create directory
          if (dir.mkdir()) {
               // directory creation successful
          }
          else {
               // directory creation unsuccessful
          }
     }
%>
0
 
trowaAuthor Commented:
Hi CJ,

The code above working well :)

Can you do me a favour? How can i delete the folder? :P

Of course points will be added for your contributions.

Thanks a lot.
0
 
cheekycjCommented:
if the directory is empty its simple:

<%@ page import="java.io.*,java.io.File" %>
<%
    File dir = new File("path/to/directory");
    if (dir.exists()) {
         if (dir.isDirectory()) {
              dir.delete();
         }
         else {
              // path does exist but is not a directory -- probably just a file
         }
%>

But if the directory has files you must delete the files in the dir.

HTH,
CJ
0
 
trowaAuthor Commented:
Thanks CJ, will try later.
0
 
cheekycjCommented:
ok, let me know how it works out.

CJ
0
 
trowaAuthor Commented:
Thanks CJ. I will ask another question regarding how to delete a nested folder later. Hope you can take sometime to solve this problem
0
 
cheekycjCommented:
I will try :-)

Glad I could help and Thanx for the "A".

CJ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now