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