天天看點

java建立檔案夾

用java建立檔案夾

package test;

public class mulutest

{

public static void main(string[] args)

 mulutest 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("建立目錄成功");

 }

 catch   (exception   e)

  system.out.println("建立目錄操作出錯");

  e.printstacktrace();

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:建立檔案夾成功!" );

               } else  {

                   system.out.println( "err:建立檔案夾失敗!" );                    

               }

          }

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

       if  (file  !=  null   &&   ! file.isempty())   {

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

               file file2  =  new  file(fullpath);

               file2.delete();

               result  =  true ;

          }   catch  (exception e)   {

               e.printstacktrace();

               result  =   false;

   public     boolean    deletefolder(file   folder)    {  

             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))  {

                               } else {

                 }

             folder.delete();  

        } catch (exception e)  {

             e.printstacktrace();

             result  =  false ;

        }

150%;�yac���8~�amily:simsun;mso-hansi-font-family:simsun;color:#323e32'>十進制整數轉換成二進制整數,傳回結果是一個字元串:

integer.tobinarystring(int i);

integer和long提供了tobinarystring,tohexstring和tooctalstring方法,可以友善的将資料轉換成二進制、十六進制和八進制字元串。功能更加強大的是其tostring(int/long i, int radix)方法,可以将一個十進制數轉換成任意進制的字元串形式。

byte, short, float和double等資料類型,可以利用integer或者是long的tobinarystring, tohexstring, to octalstring和tostring方法轉換成其他進制的字元串形式。

4 其它進制到十進制的轉換

五進制字元串14414轉換成十進制整數,結果是1234:

system.out.println(integer.valueof("14414", 5);

integer和long提供的valueof(stringsource, int radix)方法,可以将任意進制的字元串轉換成十進制資料。

5 整數到位元組數組的轉換

public static byte[] tobytearray(int number)

int temp = number;

byte[] b=new byte[4];

for (int i = b.length - 1; i > -1; i--)

b[i] = new integer(temp & 0xff).bytevalue();

temp = temp >> 8;

return b;

6 位元組數組到整數的轉換

public static int tointeger(byte[] b)

int s = 0;

for (int i = 0; i < 3; i++)

if (b[i] > 0)

s = s + b[i];

else

s = s + 256 + b[i];

s = s * 256;

if (b[3] > 0)

s = s + b[3];

s = s + 256 + b[3];

return s;

7 短整數與位元組數組之間的互相轉換

short與int之間的差別在于short是兩個位元組的,而int是四個位元組的。是以,隻需要将5 與6 中的範例程式小做改動,即可實作短整數與位元組數組之間的互相轉換。

8 位元組數組轉換成雙精度浮點數

public double todouble(byte[] b)

long l = 0;

double d = new double(0.0);

l = b[0];

l |= ((long)b[1]<<8);

l |= ((long)b[2]<<16);

l |= ((long)b[3]<<24);

l |= ((long)b[4]<<32);

l |= ((long)b[5]<<40);

l |= ((long)b[6]<<48);

l |= ((long)b[7]<<56);

return d.longbitstodouble(l);

9 布爾類型轉換成字元串

第一種方法是:

boolean bool = true;

string s = new boolean(bool).tostring();//将bool利用對象封裝器轉化為對象

s.equals("true");

第二種方法是:

string s = string.valueof( bool );

首先,從代碼長度上講第二種方法明顯要比第一種方法簡潔;其次,第一種方法在轉化過程中多引入了一個完全沒有必要的對象,是以,相對第二種方法來說這就造成了記憶體空間的浪費,大大減慢了運作速度。是以,推薦使用第二種方法。

10 數字類型與數字類對象之間的轉換

byte b = 169;

byte bo = new byte( b );

b = bo.bytevalue();

short t = 169;

short to = new short( t );

t = to.shortvalue();

int i = 169;

integer io = new integer( i );

i = io.intvalue();

long l = 169;

long lo = new long( l );

l = lo.longvalue();

float f = 169f;

float fo = new float( f );

f = fo.floatvalue();

double d = 169f;

double dobj = new double( d );

d = dobj.doublevalue();

5.0 string 轉integer

integer in=new integer(string s);

integer in=new integer(integer.parseint(string s));

5.1 string 轉 int

1). int i = integer.parseint([string]); 或

i = integer.parseint([string],[int radix]);

2). int i = integer.valueof(my_str).intvalue();

5.2 如何将整數 int 轉換成字串 string ?

a. 有叁種方法:

1.) string s = string.valueof(i);

2.) string s = integer.tostring(i);

3.) string s = "" + i;

注:double, float, long 轉成字串的方法大同小異.

5.3 string 轉date

導入 java.util.date date=null;

    date=java.sql.date.valueof(string s);