JAVA Tools Class--------(personal) __java

Source: Internet
Author: 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);

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

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.