时间处理:
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();
}
}
}
}
}