Tool class encapsulation (time conversion, document read/write)

Source: Internet
Author: User

Time Processing:

Import Java.util.Date;

Import java.text.ParseException;

Import Java.text.SimpleDateFormat;


public class datetool{

/**

* Convert Date (Chinese) to string (ms)

* @param date

* @param pattern

* @return

*/

public static String datetostring (Date date,string pattern) {

return new SimpleDateFormat (pattern). Format (date);

}

/**

* Convert string (milliseconds) to date (Chinese)

* @param datestring

* @param formate

* @return

* @throws ParseException

*/

public static Date stringtodate (String datestring,string formate) throws parseexception{

return new SimpleDateFormat (formate). Parse (datestring);

}

}

File processing:

Import Java.io.BufferedInputStream;

Import Java.io.BufferedOutputStream;

Import Java.io.File;

Import Java.io.FileInputStream;

Import java.io.FileNotFoundException;

Import Java.io.FileOutputStream;

Import java.io.IOException;


public class Iotool {

/**

* Byte stream loading and output

* @param srcfile

* @param destfile

*/

public static void function (File srcfile,file destfile) {

FileInputStream Fis=null;

FileOutputStream Fos=null;

try {

Fis=new FileInputStream (srcfile);

Fos=new FileOutputStream (destfile);

Byte[]bs=new byte[1024];

int length=0;

while ((Length=fis.read (BS))!=-1) {

Fos.write (BS, 0, length);

}

} catch (FileNotFoundException e) {

Todo:handle exception

E.printstacktrace ();

}catch (IOException e) {

Todo:handle exception

E.printstacktrace ();

}finally {

if (fis!=null) {

try {

Fis.close ();

} catch (IOException e) {

TODO Auto-generated catch block

E.printstacktrace ();

}

}

if (fos!=null) {

try {

Fos.close ();

} catch (IOException e) {

TODO Auto-generated catch block

E.printstacktrace ();

}

}

}

}

/**

* Character stream loading and output

* @param srcfile

* @param destfile

*/

public static void Function02 (File srcfile,file destfile) {

Bufferedinputstream Bis=null;

Bufferedoutputstream Bos=null;

try {

Bis=new Bufferedinputstream (New FileInputStream (srcfile));

Bos=new Bufferedoutputstream (New FileOutputStream (DestFile));

Byte[]bs=new byte[1024];

int length=0;

while ((Length=bis.read (BS))!=-1) {

Bos.write (bs,0,length);

}

} catch (FileNotFoundException e) {

Todo:handle exception

E.printstacktrace ();

}catch (IOException e) {

Todo:handle exception

E.printstacktrace ();

}finally {

if (bis!=null) {

try {

Bis.close ();

} catch (IOException e) {

TODO Auto-generated catch block

E.printstacktrace ();

}

}

if (bos!=null) {

try {

Bos.close ();

} catch (IOException e) {

TODO Auto-generated catch block

E.printstacktrace ();

}

}

}

}

}


Tool class encapsulation (time conversion, document read/write)

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.