【Java】工具类积累

Java byte数据与文件的相互转换方法:FileUtil

1. file转byte[]数组 | 文件转字节流

Java byte数组和文件相互转换
Java byte转化成file

package com.amc.oainterface.service;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class FileUtil {
	/**
	 * 获得指定文件的byte数组 
	 * @param filePath 文件绝对路径
	 * @return
	 */
	public static byte[] file2Byte(String filePath){
		ByteArrayOutputStream bos=null;
		BufferedInputStream in=null;
		try{
			File file=new File(filePath);
			if(!file.exists()){  
	            throw new FileNotFoundException("file not exists");  
	        }
			bos=new ByteArrayOutputStream((int)file.length());
			in=new BufferedInputStream(new FileInputStream(file));
			int buf_size=1024;
			byte[] buffer=new byte[buf_size];
			int len=0;
			while(-1 != (len=in.read(buffer,0,buf_size))){
				bos.write(buffer,0,len);
			}
			return bos.toByteArray();
		}
		catch(Exception e){
			System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
		}
		finally{
			try{
				if(in!=null){
					in.close();
				}
				if(bos!=null){
					bos.close();
				}
            }
			catch(Exception e){
				System.out.println(e.getMessage());
				e.printStackTrace();  
            }
        }
    }

2. byte[]数组转file | 字节流转文件

package com.amc.oainterface.service;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
 
    /**
     * 根据byte数组,生成文件 
     * @param bfile 文件数组
     * @param filePath 文件存放路径
     * @param fileName 文件名称
     */
	public static void byte2File(byte[] bfile,String filePath,String fileName){
		BufferedOutputStream bos=null;
		FileOutputStream fos=null;
		File file=null;
		try{
			File dir=new File(filePath);
			if(!dir.exists() && !dir.isDirectory()){//判断文件目录是否存在  
				dir.mkdirs();  
            }
			file=new File(filePath+fileName);
			fos=new FileOutputStream(file);
			bos=new BufferedOutputStream(fos);
			bos.write(bfile);
		} 
		catch(Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();  
        }
		finally{
			try{
				if(bos != null){
					bos.close(); 
				}
				if(fos != null){
					fos.close();
				}
			}
			catch(Exception e){
				System.out.println(e.getMessage());
				e.printStackTrace();  
			}
		}
    }
}

java 获取当前时间的三种方法:DateUtil

1. 通过Util包中的Date获取

import java.text.SimpleDateFormat;

public static String getDateTime(){
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		//System.out.println(dateFormat.format(date));
		return dateFormat.format(date);
}

2. 通过Util包的Calendar 获取

public static String getCalendarTime(){
		Calendar calendar= Calendar.getInstance();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		//System.out.println(dateFormat.format(calendar.getTime()));
		return dateFormat.format(calendar.getTime());
	}

3. 通过Util包的Calendar 获取时间,分别获取年月日时分秒

public static String getDIYTime(){
		Calendar cal=Calendar.getInstance();
		int y=cal.get(Calendar.YEAR);
		int m=cal.get(Calendar.MONTH);
		int d=cal.get(Calendar.DATE);
		int h=cal.get(Calendar.HOUR_OF_DAY);
		int mi=cal.get(Calendar.MINUTE);
		int s=cal.get(Calendar.SECOND);
		//System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");
		return "现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒";
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章