📄 commonmethod.java
字号:
package com.doone.fj1w.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.doone.data.DacClient;
import com.doone.data.DataTable;
import com.doone.util.FileLogger;
public class CommonMethod {
/**
* 根据员工ID获取员工名帐号
* @param id
* @return
*/
public static String getStaffCode(String code) {
try {
String sql = "select staffcode from tf_staff where staffid=? and STATE='E'";
DacClient client = new DacClient();
DataTable dt = client.executeQuery(sql, new Object[] {code});
if (dt != null && dt.getRows().getCount() > 0) {
return dt.getRow(0).getString("staffcode");
}
} catch (Exception ex) {
FileLogger.getLogger().warn(ex.getMessage(),ex);
}
return "";
}
/**
*
* @param dObj
* @return
*/
public static String fromDate(Date dObj,String format) {
try {
if( dObj == null ) { return "";}
if( format.equals("") ) {
format = "yyyy-MM-dd HH:mm";
}
SimpleDateFormat f1 = new SimpleDateFormat(format);
return f1.format(dObj);
} catch (Exception e) {
FileLogger.getLogger().warn(e.getMessage(), e);
}
return null;
}
/**
* @param sDate
* @param format
* @return
*/
public static Date toDate(String sDate,String format) {
try {
if( sDate == null || sDate.equals("")) {
return new Date();
}
if( format.equals("") ) {
format = "yyyy-MM-dd HH:mm";
}
SimpleDateFormat f1 = new SimpleDateFormat(format);
return f1.parse(sDate);
} catch (Exception e) {
FileLogger.getLogger().warn(e.getMessage(), e);
}
return null;
}
/**
* 生成文件.
* @param pathFileName
* @return
* @throws RuntimeException
*/
public static File ganericFile(String path,String FileName)throws Exception{
try {
if(!ganericPaths(path)) {throw new RuntimeException("路径信息.");}
File file = new File(path+FileName);
if(!file.exists()){
file.createNewFile();
}
return file;
} catch (Exception e) {
throw e;
}
}
/**
* 生成文件.
* @param pathFileName
* @return
* @throws RuntimeException
*/
public static File ganericFile(String FileName)throws Exception{
try {
File file = new File(FileName);
if(!file.exists()){
file.createNewFile();
}
return file;
} catch (Exception e) {
throw e;
}
}
/**
* 生成路径.
* @param path
* @throws Exception
*/
public static boolean ganericPath(String path)throws Exception{
try {
File file = new File(path);
if(!file.exists()){
return file.mkdir();
}
return false;
} catch (Exception e) {
throw e;
}
}
/**
* 生成路径.
* @param path
* @throws Exception
*/
public static boolean ganericPaths(String path)throws Exception{
try {
File file = new File(path);
if(!file.exists()){
return file.mkdirs();
}
return true;
} catch (Exception e) {
throw e;
}
}
/**
* 覆盖写入文件.
* @param file
* @param value
* @throws Exception
*/
public static void overWriterFile(File file,String value)throws Exception{
try {
FileWriter writer = new FileWriter(file);
writer.write(value);
writer.flush();
writer.close();
} catch (Exception e) {
throw e;
}
}
/**
*
* @param file
* @param value
* @throws Exception
*/
public static void overWriterFile(File file,byte[] value)throws Exception{
try {
FileOutputStream out = new FileOutputStream(file);
out.write(value, 0, value.length);
out.flush();
out.close();
} catch (Exception e) {
throw e;
}
}
/**
* 添加写入文件,从最尾行开始.
* @param file
* @param value
* @throws Exception
*/
public static void appendWriterFile(File file,String value)throws Exception{
try {
FileWriter writer = new FileWriter(file,true);
writer.write(value);
writer.flush();
writer.close();
} catch (Exception e) {
throw e;
}
}
/**
* 读取文件.
*
* @param file
* @param encode
* @return
* @throws Exception
*/
public static StringBuffer getFileBody(File file,String encode)throws Exception{
InputStreamReader read = null;
BufferedReader reader = null;
try {
read = new InputStreamReader(new FileInputStream(file),encode);
reader=new BufferedReader(read);
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
return buffer;
} catch (Exception ex) {
throw ex;
} finally {
try {
reader.close();
read.close();
} catch (Exception ex) {}
}
}
/**
*
* @param file
* @param encode
* @return
* @throws Exception
*/
public static byte[] getBody(File file,String encode)throws Exception{
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] body = new byte[(int)file.length()+1];
if ( inputStream.read(body, 0, (int)file.length()) != -1 ) {
return body;
}
return new byte[0];
} catch (Exception ex) {
throw ex;
} finally {
try {
inputStream.close();
} catch (Exception ex) {}
}
}
/**
* 是否是扩展名.
* @param file
* @param fixName
* @return
*/
public static boolean isFixName(String file,String[] fixName) {
try {
String end2 = file.substring(file.lastIndexOf(".")+1,file.length());
for (int i = 0; i < fixName.length; i++) {
if(end2.equalsIgnoreCase(fixName[i])) {
return true;
}
}
} catch (Exception ex) { }
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -