JAVA工具類--------(個人)__JAVA

來源:互聯網
上載者:User
 

 
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();
  }
 
 }
 
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.