Create a folder in Java

Source: Internet
Author: User

Create a folder in Java

Package test;

Public class mulutest
{
Public static void main (string [] ARGs)
{
Mulutest = new mulutest ();
Mulutest. newfolder ("D: \ abcds ");
}
Public void newfolder (string folderpath)
{
String filepath = folderpath;
Filepath = filepath. tostring ();
Java. Io. File myfilepath = new java. Io. File (filepath );
Try
{
If (myfilepath. isdirectory ())
{
System. Out. println ("the directory isexists! ");
}
Else
{
Myfilepath. mkdir ();
System. Out. println ("directory created successfully ");
}
}
Catch (exception E)
{
System. Out. println ("Directory creation error ");
E. printstacktrace ();
}
}
}

 

Java creates folders, files, delete folders, and files

Package ifpre. Web;

 

Import gxdmif. gxstringutil;

 

Import java. Io. dataoutputstream;

Import java. Io. file;

Import java. Io. fileoutputstream;

Import java. Io. inputstream;

 

Import org. springframework. Web. multipart. multipartfile;

 

Public class SaveFile {

Public Boolean save (string path, multipartfile file) throws exception {

Gxstringutil GX = new gxstringutil ();

Boolean result = false;

File dirfile = NULL;

Try {

Dirfile = new file (PATH );

If (! (Dirfile. exists ())&&! (Dirfile. isdirectory ())){

Boolean creadok = dirfile. mkdirs ();

If (creadok ){

System. Out. println ("OK: the folder is created successfully! ");

} Else {

System. Out. println ("Err: failed to create folder! ");

}

}

} Catch (exception e ){

E. printstacktrace ();

System. Out. println (E );

Return false;

}

If (file! = NULL &&! File. isempty ()){

String fullpath = path + system. getproperty ("file. separator ")

+ Gx. netstringtogbk (file. getoriginalfilename ());

Dataoutputstream out = NULL;

Inputstream is = NULL;

Try {

Out = new dataoutputstream (New fileoutputstream (fullpath ));

Is = file. getinputstream ();

Byte [] buffer = new byte [1024];

While (is. Read (buffer)> 0 ){

Out. Write (buffer );

}

} Finally {

If (is! = NULL ){

Is. Close ();

}

If (OUT! = NULL ){

Out. Close ();

}

}

Result = true;

}

Return result;

}

 

Public Boolean Delete (string path, multipartfile file) throws exception {

Gxstringutil GX = new gxstringutil ();

Boolean result = false;

If (file! = NULL &&! File. isempty ()){

String fullpath = path + system. getproperty ("file. separator ")

+ Gx. netstringtogbk (file. getoriginalfilename ());

Try {

File file2 = new file (fullpath );

File2.delete ();

Result = true;

} Catch (exception e ){

E. printstacktrace ();

Result = false;

}

}

Return result;

}

Public Boolean deletefolder (file folder ){

Boolean result = false;

Try {

String Childs [] = folder. List ();

If (Childs = NULL | Childs. Length <= 0 ){

If (Folder. Delete ()){

Result = true;

}

} Else {

For (INT I = 0; I <Childs. length; I ++ ){

String childname = Childs [I];

String childpath =

Folder. getpath () + file. Separator + childname;

File filepath = new file (childpath );

If (filepath. exists () & filepath. isfile ()){

If (filepath. Delete ()){

Result = true;

} Else {

Result = false;

Break;

}

}

Else if (filepath. exists () & filepath. isdirectory ()){

If (deletefolder (filepath )){

Result = true;

} Else {

Result = false;

Break;

}

}

}

}

Folder. Delete ();

} Catch (exception e ){

E. printstacktrace ();

Result = false;

}

Return result;

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.