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);
Time format Conversion
public static String timetostring () {
String datetostring =
New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new java.util.Date ());
return datetostring;
}
Time format Conversion
public static String timetostring (date date) {
String datetostring =
New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (date);
return datetostring;
}
Time format Conversion
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;
}
Time converted to data type
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;
}
Read TXT file
public static string Gettxtinfo (String filePath) {
String relstr= "";
try {
String encoding = "UTF-8"; Character encoding
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 ("The specified file was not found. ");
}
catch (Exception e) {
Log.error ("Error reading file contents operation" +e);
}
return relstr;
}
public static Boolean isnotnull (Object o) {
Boolean rel=false;
if (null!=o &&! "". Equals (o)) {
Rel=true;
}
return rel;
}
With the new file
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); Decorative one with buffered output stream
DataOutputStream out = new DataOutputStream (OUT2); Decorate a file output stream
Out.writebytes (str);
Out.write (Str.getbytes ("UTF-8"));
Out2.close ();
Out1.close ();
catch (FileNotFoundException e)
{
E.printstacktrace ();
catch (IOException E)
{
E.printstacktrace ();
}
}
}