JAVA获取文件大小的函数(原创)

Filed Under (JAVA工程坊, 技术心得) by 有为 on 07-11-2008

public String getFileSize(long fileS){
DecimalFormat df =new DecimalFormat(”#.00″);
String fileSizeString=”";
if (fileS<1024){
fileSizeString=df.format((double)fileS)+”B”;
}else if(fileS<1048576){
fileSizeString=df.format((double)fileS/1024)+”K”;
}else if(fileS<1073741824){
fileSizeString=df.format((double)fileS/1048576)+”M”;
}else{
fileSizeString=”df.format((double)fileS/1073741824)+”G”;
}
return fileSizeString;
}

该代码是在给我院孙老师写网上工具箱代码中其中一个函数,希望对大家有借鉴作用。

java小数保留两位小数

Filed Under (JAVA工程坊, 技术心得) by 有为 on 07-11-2008

double   c=3.154215;

java.text.DecimalFormat myformat=new java.text.DecimalFormat(”0.00″);

String str = myformat.format(c);

java小数点问题:

方式一:

四舍五入
double   f   =   111231.5585;
BigDecimal   b   =   new   BigDecimal(f);
double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
保留两位小数

方式二:

java.text.DecimalFormat   df   =new   java.text.DecimalFormat(”#.00″);
df.format(你要格式化的数字);

例:new java.text.DecimalFormat(”#.00″).format(3.1415926)

#.00 表示两位小数 #.0000四位小数 以此类推…

方式三:

double d = 3.1415926;

String result = String .format(”%.2f”);

%.2f %. 表示 小数点前任意位数   2 表示两位小数 格式后的结果为f 表示浮点型。

在jsp环境中配置使用FCKEditor

Filed Under (FCKeditor, JAVA工程坊) by 有为 on 25-09-2008

FCKeditor是sourceforge.net上面的一个开源项目,主要是实现在线网页编辑器的功能,可以让web程序拥有如MS Word这样强大的编辑功能。官方网站为http://www.fckeditor.net ,在服务器端支持ASP.Net、ASP、ClodFusion、PHP、Java等语言,并且支持IE 5+、Mozilla 、Netscape等主流浏览器。  首先在官方网站下载fckeditor,注意有两个包,一个是主文件,一个是jsp整合包的。
1、解压FCKeditor_2.2.zip,(FCKeditor主文件),将FCKeditor目录复制到网站根目录下。
2、解压FCKeditor-2.3.zip,(jsp,FCKeditor整合包),作用:This is the JSP Integration Pack for using FCKeditor inside a java server page without the complexity of using a Java scriptlets or the javascript api。
3、将FCKeditor-2.3/web/WEB-INF/web.xml中的两个servlet,servlet-mapping定义复制到自已项目的web.xml文件中。 « 阅读全文 »

Java中日期转换、日期加减

Filed Under (JAVA工程坊) by 有为 on 25-09-2008

package com.youwei.common;

import java.text.*;
import java.util.*;

public class CDate {
public static Date toDate(String cDate) throws ParseException {
SimpleDateFormat fmt = new SimpleDateFormat(”yyyy-MM-dd”);
return fmt.parse(cDate);
}

public static Date changeDay(Date date, int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_YEAR,
(calendar.get(Calendar.DAY_OF_YEAR) + offset));
return calendar.getTime();
}
}

如果用JavaBean进行赋值操作(必须应该注意的问题)

Filed Under (JAVA工程坊) by 有为 on 25-09-2008

如果用JavaBean进行赋值操作(必须应该注意的问题)

如果用JavaBean进行赋值操作时JavaBean中的属性的首字母必须为小写,下面是我写的一个例子。
成功的格式:
package com.youwei.persistence;

public class SystemConfigTable {
public int configId;

public void setConfigId(int configId) {
this.configId = configId;
}

public int getConfigId() {
return configId;
}

}
失败的格式:
package com.youwei.persistence;

public class SystemConfigTable {
public int ConfigId;

public void setConfigId(int ConfigId) {
this.ConfigId = ConfigId;
}

public int getConfigId() {
return ConfigId;
}

}