JAVA工具类--------(个人)

来源:互联网 发布:下载一亩田软件 编辑:程序博客网 时间:2024/04/30 15:35
 

 
package com.aochuang.lotterynews.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.aochuang.lotterynews.core.ResourceLocale;

/**
 *
 * @author luojiawen
 * @version 1.0, 2011.08.25
 */
public class HelpUtil {
 
 public static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
 private static final Logger log = LogManager.getLogger(ResourceLocale.class);
 
 //时间格式转换 
 public static String timeTostring(){
  String datetostring =
   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
  return datetostring;
 }
 //时间格式转换 
 public static String timeTostring(Date date){
  String datetostring =
   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
  return datetostring;
 }
 
 //时间格式转换 
 public static String timeTostringYMD(){
  String datetostring =
   new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
  return datetostring;
 }
 
 
 public static String timeTospace(){
  String datetostring =
   new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date());
  return datetostring;
 }
 
 //时间转换成data类型
 public static Date getDate(String dateStr){
  SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
  Date rel=null;
  try
  {
   rel = dateFormat.parse(dateStr);
  } catch (ParseException e)
  {
   log.error(e);
  
  }
  return rel;
 }
 
 //读取TXT文件
 public static String getTxtInfo(String filePath) {
  String relstr="";
  try {
   String encoding = "UTF-8"; // 字符编码
   File file = new File(filePath);
   if (file.isFile() && file.exists()) {
    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
    BufferedReader bufferedReader = new BufferedReader(read);
    String lineTXT = null;
    while ((lineTXT = bufferedReader.readLine()) != null) {
     relstr+=lineTXT;
    }
    read.close();
   } else {
    log.error("找不到指定的文件!");
   }
  } catch (Exception e) {
   log.error("读取文件内容操作出错"+e);
  }
  return relstr;
 }

 
 public static boolean isNotNull(Object o){
  boolean rel=false;
  if(null!=o && !"".equals(o)){
   rel=true;
  }
  return rel;
 }
 //跟新文件
 public static void updateFile(String filePath,String str){
 
  File ss = new File(filePath);
  ss.deleteOnExit();
  FileOutputStream out1;
  try
  {
   out1 = new FileOutputStream(filePath, false);
   BufferedOutputStream out2 = new BufferedOutputStream(out1, 2); // 装饰一个带缓冲输出流
   DataOutputStream out = new DataOutputStream(out2); // 装饰一个文件输出流
  // out.writeBytes(str);
   out.write(str.getBytes("UTF-8"));
  
   out2.close();
   out1.close();
  } catch (FileNotFoundException e)
  {
   e.printStackTrace();
  } catch (IOException e)
  {
   e.printStackTrace();
  }
 
 }
 
}

 

原创粉丝点击