【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+"秒";
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章