码迷,mamicode.com
首页 > 其他好文 > 详细

工具类封装(时间转换,文档读写)

时间:2016-06-22 16:00:47      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:return   import   public   

时间处理:

        

import java.util.Date;

import java.text.ParseException;

import java.text.SimpleDateFormat;


public class DateTool{

/**

* 将日期(中式)转换成String(毫秒) 

* @param date

* @param pattern

* @return

*/

public static String dateToString(Date date,String pattern){

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

}

/**

* 将String(毫秒)转换成日期(中式)

* @param dateString

* @param formate

* @return

* @throws ParseException

*/

public static Date stringToDate(String dateString,String formate) throws ParseException{

return new SimpleDateFormat(formate).parse(dateString);

}

}

文件处理:

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 {

/**

* 字节流载入与输出

* @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 自动生成的 catch 块

e.printStackTrace();

}

}

if(fos!=null){

try {

fos.close();

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

}

/**

* 字符流载入与输出

* @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 自动生成的 catch 块

e.printStackTrace();

}

}

if (bos!=null) {

try {

bos.close();

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

}

}


工具类封装(时间转换,文档读写)

标签:return   import   public   

原文地址:http://11588233.blog.51cto.com/11578233/1791687

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!