2008年7月18日 星期五

PAV3檔暫存(讀檔、寫檔的範例)


以下包括 讀檔、寫檔的範例。



public class PAV3 {

public static final String Left = "L";

public static final String Right = "R";

private String BLD_DNLOAD = "/Users/macbook/java/test/FTP_DIR/BLD_DNLOAD";

private String PAV3 = "";

private String LST3_FILE = "";
private String BV1_FILE = "";
private String BV2_FILE = "";
private String BV3_FILE = "";

public void run(String path) {
this.BLD_DNLOAD = path;
}



//先讀取舊的 PAV3.DAT
public Map ReadPAV3() {

ArrayList<String> newList = new ArrayList<String>();
Map newMap = new HashMap(2);

PAV3 = BLD_DNLOAD + "//PAV3.DAT";

FileReader f;
BufferedReader br;


try {
f = new FileReader(PAV3);
br = new BufferedReader(f);

String strLine;

while ((strLine = br.readLine()) != null) {

if(strLine.startsWith("H")){
newMap.put("header", strLine);
}else if(strLine.startsWith("D")){
if(!this.isExistHIJK(strLine)){
//System.out.println("["+strLine+"]");
newList.add(strLine);
}
}else{

}
}
newMap.put("detail", newList);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return newMap;
}



public void run() {

//先讀取舊的 PAV3.DAT
Map oldMap = ReadPAV3();

File f = new File(BLD_DNLOAD);
File[] files = f.listFiles();
TreeMap tm = new TreeMap();

ArrayList<String> al_LST3 = new ArrayList<String>();
ArrayList<String> al_BV1 = new ArrayList<String>();
ArrayList<String> al_BV2 = new ArrayList<String>();
ArrayList<String> al_BV3 = new ArrayList<String>();
ArrayList<String> al_hijk = new ArrayList<String>();

for (int i = 0; i < files.length; i++) {

if (!files[i].isDirectory()
&& !files[i].getName().startsWith(".DS_Store")) {

String filename = files[i].getName();

String sysDate = this.retriSysDate();
String fileDate = this.retriveFileDate(filename);

// 檔案日期 大於或等於 系統日
if (this.compare(fileDate, sysDate)) {

if (this.whichHIJK(filename).equals("i")) {
al_LST3.add(filename);
} else if (this.whichHIJK(filename).equals("j")) {
al_BV1.add(filename);
} else if (this.whichHIJK(filename).equals("k")) {
al_BV2.add(filename);
} else if (this.whichHIJK(filename).equals("h")) {
al_BV3.add(filename);
}

} else {

if (this.whichHIJK(filename).equals("i")) {
if (this.compare(fileDate, LST3_FILE)) {
LST3_FILE = filename;
}
} else if (this.whichHIJK(filename).equals("j")) {
if (this.compare(fileDate, LST3_FILE)) {
BV1_FILE = filename;
}
} else if (this.whichHIJK(filename).equals("k")) {
if (this.compare(fileDate, LST3_FILE)) {
BV2_FILE = filename;
}
} else if (this.whichHIJK(filename).equals("h")) {
if (this.compare(fileDate, LST3_FILE)) {
BV3_FILE = filename;
}
}

}

}

}

if (!LST3_FILE.equals("")) al_LST3.add(LST3_FILE);
if (!BV1_FILE.equals("")) al_BV1.add(BV1_FILE);
if (!BV2_FILE.equals("")) al_BV2.add(BV2_FILE);
if (!BV3_FILE.equals("")) al_BV3.add(BV3_FILE);

al_hijk.addAll(al_LST3);
al_hijk.addAll(al_BV1);
al_hijk.addAll(al_BV2);
al_hijk.addAll(al_BV3);

tm.put("HIJK", al_hijk);
// tm.put("BV1", al_BV1);
// tm.put("BV2", al_BV2);
// tm.put("BV3", al_BV3);

String b = this.buildPav3(tm,oldMap);

System.out.println(b);

}



//產生 PAV3.DAT
public String buildPav3(TreeMap tm,Map oldmap) {


StringBuilder b = new StringBuilder();
// String company = "0001";

// header
//b = this.header(b, company);
b.append(oldmap.get("header")).append("\n");


// detail

int s = 0;

ArrayList l0 = (ArrayList)oldmap.get("detail");

for (java.util.Iterator<String> it = l0.iterator(); it.hasNext();) {
String olddetail = it.next();
b.append(olddetail).append("\n");
s++;
}

List l1 = (ArrayList) tm.get("HIJK");
for (java.util.Iterator<String> it = l1.iterator(); it.hasNext();) {
String filename = it.next();
b = this.detail(b, filename);
s++;
}


// trailer
b = this.trailer(b, s);

//update PAV3
this.writeToPAV3(b.toString());

return b.toString();
}


public void writeToPAV3(String p){

File saveFile=new File(PAV3);
try
{
FileWriter fwriter=new FileWriter(saveFile);
fwriter.write(p);
fwriter.close();
}
catch(Exception e)
{
e.printStackTrace();
}

}


// header
public StringBuilder header(StringBuilder b, String com) {
// 1.
b.append("H");
// 2.
b.append(" ");
// 3.4.
b.append(this.retriSysDateTime());
// 5.
b.append(com);
// 6.
for (int i = 0; i < 12; i++) {
b.append(" ");
}
// 7.
b.append("\n");

return b;
}

// detail
public StringBuilder detail(StringBuilder b, String filename) {
// 1.
b.append("D");
// 2.
b.append(" ");
// 3.
b.append(this.fillZero(filename, 30, "L"));
// 4.
b.append("\n");
return b;
}

// trailer
public StringBuilder trailer(StringBuilder b, int t) {
// 1.
b.append("T");
// 2.
b.append(" ");
// 3.
b.append(this.fillZero(t, 6, "R"));
// 4.
for (int i = 0; i < 24; i++) {
b.append(" ");
}
// 5.
b.append("\n");
return b;
}

// 左邊補0
public String LeftZero(int f, int len) {

String totalcount = "";
String count = String.valueOf(f);
int l = count.length();

for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
totalcount += count;
return totalcount;
}

// 右邊補0
public String RightZero(int f, int len) {

String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
totalcount += count;
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}

return totalcount;
}

// 系統日與FileDate做比較
public boolean compare(String sysDate, String fileDate) {

// System.out.println("Compare:"+sysDate+'/'+fileDate);
if (fileDate.equals(""))
fileDate = "19000000";
if (sysDate.compareTo(fileDate) >= 0) {
return true;
} else {
return false;
}
}

//判斷是否存在文件代碼是 h,i,j,k
public boolean isExistHIJK(String strLine){

if(StringUtils.contains(strLine, "TBLD") || StringUtils.contains(strLine, "BV_")){
return true;
}
return false;

}

// 補0
// Example
// p.fillZero(21, 5, "R") ==>21000
// p.fillZero(21, 5, "L") ==>00021

private String fillZero(int f, int len, String LR) {

String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
if (LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
if (!LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
return totalcount;

}

public String fillZero(Object f, int len, String LR) {

String totalcount = "";
String count = String.valueOf(f);
int l = count.length();
if (LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
for (int j = 0; j < len - l; j++) {
totalcount += "0";
}
if (!LR.equalsIgnoreCase(this.Right)) {
totalcount += count;
}
return totalcount;
}

public boolean isHIJK(String filename) {

if (filename.toUpperCase().startsWith("TBLD")
|| filename.toUpperCase().startsWith("BV_UPDATE3")
|| filename.toUpperCase().startsWith("BV_STARTUP3")
|| filename.toUpperCase().startsWith("BV_MAIN3")) {
return true;
}
return false;
}

public String whichHIJK(String filename) {

if (filename.toUpperCase().startsWith("TBLD")) {// h
return "h";
} else if (filename.toUpperCase().startsWith("BV_UPDATE3")) {// i
return "i";
} else if (filename.toUpperCase().startsWith("BV_STARTUP3")) {// j
return "j";
} else if (filename.toUpperCase().startsWith("BV_MAIN3")) {// k
return "k";
}
return "";
}

public String retriveFileDate(String filename) {

// System.out.println(filename+'/'+filename.length());
if (filename.toUpperCase().startsWith("TBLD")
&& filename.length() == 17) {// h
return filename.substring(5, 13);
} else if (filename.toUpperCase().startsWith("BV_UPDATE3")
&& filename.length() == 19) {// i
return filename.substring(11, 19);
} else if (filename.toUpperCase().startsWith("BV_STARTUP3")
&& filename.length() == 20) {// j
return filename.substring(12, 20);
} else if (filename.toUpperCase().startsWith("BV_MAIN3")
&& filename.length() == 21) {// k
return filename.substring(9, 17);
}
return "00000000";
}

//取得系統時間
public String retriSysDate() {
Calendar cl = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(cl.getTime());
}

public String retriSysDateTime() {
Calendar cl = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(cl.getTime());
}



}

2008年7月7日 星期一

Java 讀檔(使用BufferReader)




String fileName1 = "/Users/macbook/java/test/IC080329.G02";



FileReader f;
BufferedReader br;
SimpleConnection sc = new SimpleConnection();
Connection con = null;

try {
f = new FileReader(fileName1);
br = new BufferedReader(f);
String strLine;
int i = 0;
con = sc.getConnection();
while ((strLine = br.readLine()) != null) {

//長度為135,為了防止表頭及表
if(strLine.length()>100){
//System.out.println("["+strLine.length()+"]"+strLine);
separate(strLine);
if(save2db(con)){
System.out.println("第"+i+"筆匯入成功!");
}
// print();
}
i++;
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}finally{
sc.freeConnection(con);
}


2008年7月1日 星期二

mysql tutorial


1.tutorial
http://dev.mysql.com/doc/refman/5.0/en/tutorial.html

2008年6月29日 星期日

Instanceof 測試

如果要用來判斷傳入的是那種型態,參考如下 :

public class InstanceoftTest {


InstanceoftTest(){

String s = "this is String";
Integer i = new Integer(1);
BigDecimal b = new BigDecimal(2.34);
checkWithInstanceof(s);
checkWithInstanceof(i);
checkWithInstanceof(b);
}

public static void checkWithInstanceof(Object o){

if(o instanceof Integer){
System.out.println("Your Class Type is Integer");
}else if(o instanceof BigDecimal){
System.out.println("Your Class Type is BigDecimal");
}else{
System.out.println("Your Class Type will conver String");
}
}



}

2008年6月26日 星期四

學習 jQuery (一)

1.why jQuery

寫過javascript來操作Dom是很麻煩的,jQuery可以比較簡單的來取出相關的Element。
jQuery 所有的 API 都是定義在 jQuery 這個物件下,沒有對 JavaScript 原生的物件作任何修改。
同時 jQuery 的 selector(取得網頁物件的 API)符合 CSS 的語法,同時寫 jQuery 及 CSS 一點都不會錯亂。

2.Download JQuery

網站上有三個版
1.jquery-xxxx.js
這是 jQuery 程式碼最好讀的版本,如果你打算研究 jQuery 的原始程式碼,那下載這個版本是最清楚的。
2.jquery-xxxx.min.js
這個版本與 1. 是一樣的程式碼,只不過把多餘的空白或是換行給拿掉,以便讓整個 js 檔案大小可以下降,
在上線的網站上使用這個版本可以讓使用者下載量變少一點,以提升整個頁面的載入速度。
3.jquery-xxxx.pack.js
這個版本把原本 jQuery 的程式碼作了「壓縮」,也就是有一些名稱代換的方式把 code size 進行更進一步的塑身。
不過因為它用了 eval() 這個函式,所以 JavaScript 的載入會稍微延遲一點時間。

3.第一個jQuery程式


<html>
<head>
<title>jQuery Tutorial 1</title>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
function showMsg(e) {
$(e.target).attr('disabled', true);
if ($('#msg').html().length == 0) {
$('#msg').html('<h1>Hello</h1>');
}
$('#msg').fadeIn();
setTimeout(function(){
$('#msg').fadeOut();
$(e.target).attr('disabled', false);
}, 3000);
}

$(document).ready(function(e){
$('#btn').click(showMsg);
});
</script>
</head>
<body>
<div id="msg"></div>
<input type="button" value="Click Me" id="btn"/>
</body>
</html>


4. UI plug in


4.1 UI plug : http://ui.jquery.com/themeroller
4.2 UI plug in API: http://docs.jquery.com/UI

2008年6月25日 星期三

從web.xml取出變數


如果想取出web.xml裡的key-value,做法如下:

1. web.xml設定

<context-param>
<param-name>notebook</param-name>
<param-value>1</param-value>
</context-param>

2. 在程式做法(以下做法是在JSF下)

private FacesContext fc = FacesContext.getCurrentInstance();
//透過facesContext可取得httpSession
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
//可取出初始化變數(name="notebook")
String a = session.getServletContext().getInitParameter("notebook");

if(null!=a && a.equals("1")) {
cps_tbld_dir ="//users//macbook//java//test//FTP_DIR//TBLD";
}

2008年6月24日 星期二

Java: System Properties

來源: http://www.leepoint.net/notes-java/io/30properties_and_preferences/40sysprops/10sysprop.html

From System Properties you can find information about the operating system, the user, and the version of Java.

The property names (keys) and values are stored in a Properties structure. (See Properties). A Properties object can also be used to store your own program properties in a file.


Getting the System Properties

Typically you get one property at a time by supplying the key in a call to System.getProperty().

  • String System.getProperty(String key)
    Returns the value of property key as a String.
  • String System.getProperty(String key, String default)
    Returns the value of property key as a string, or default if the property did not exist.
  • Properties System.getProperties()
    Returns a Properties object which has the value of all the properties. There are several ways to work with this object; see below for one example.

Example

String userDir = System.getProperty("user.dir");

A list of system properties

Here are the properties that displayed on my system.

Here are the properties that displayed on my system.

awt.toolkit=sun.awt.windows.WToolkit
file.encoding=Cp1252
file.encoding.pkg=sun.io
file.separator=\
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.class.path=.;c:\classpath\com.fredswartz.utilities.jar;c:\classpath\TableLayout.jar;c:\classpath\swixml.jar;c:\classpath\jdom.jar;c:\classpath\pmd-1.8\lib\pmd-1.8.jar;C:\classpath\pmd-1.8\lib\jaxen-core-1.0-fcs.jar;C:\classpath\com.fredswartz.guiUtils.jar;C:\classpath\com.fredswartz.fmt-0.7.jar;C:\Program Files\IBM\Cloudscape_10.0\lib\derby.jar
java.class.version=49.0
java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0_01\jre\lib\endorsed
java.ext.dirs=C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext
java.home=C:\Program Files\Java\jdk1.5.0_01\jre
java.io.tmpdir=C:\DOCUME~1\Owner\LOCALS~1\Temp\
java.library.path=C:\Program Files\Java\jdk1.5.0_01\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\Python22;C:\Program Files\PC-Doctor for Windows\services;c:\ant\bin;c:\Program Files\Java\jdk\bin;c:\Program Files\Java\jdk\jre\javaws;C:\Program Files\Sybase\Adaptive Server Anywhere 6.0\win32;c:\classpath\jcsc/bin;c:\classpath\jcsc\bin;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin
java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition
java.runtime.version=1.5.0_01-b08
java.specification.name=Java Platform API Specification
java.specification.vendor=Sun Microsystems Inc.
java.specification.version=1.5
java.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
java.version=1.5.0_01
java.vm.info=mixed mode, sharing
java.vm.name=Java HotSpot(TM) Client VM
java.vm.specification.name=Java Virtual Machine Specification
java.vm.specification.vendor=Sun Microsystems Inc.
java.vm.specification.version=1.0
java.vm.vendor=Sun Microsystems Inc.
java.vm.version=1.5.0_01-b08
line.separator=

os.arch=x86
os.name=Windows XP
os.version=5.1
path.separator=;
sun.arch.data.model=32
sun.boot.class.path=C:\Program Files\Java\jdk1.5.0_01\jre\lib\rt.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\i18n.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\jce.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.5.0_01\jre\classes
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_01\jre\bin
sun.cpu.endian=little
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
sun.desktop=windows
sun.io.unicode.encoding=UnicodeLittle
sun.jnu.encoding=Cp1252
sun.management.compiler=HotSpot Client Compiler
sun.os.patch.level=Service Pack 2
user.country=US
user.dir=C:\0www-workingnotes\notes-java-working\io\30properties_and_preferences\40sysprops\SysPropList
user.home=C:\Documents and Settings\Owner
user.language=en
user.name=Owner
user.timezone=
user.variant=

public void SysOsName(){

Properties pr = System.getProperties();
//取得os的名稱
String osName = pr.getProperty("os.name");
if(osName.toLowerCase().subSequence(0,3).equals("mac")){
System.out.println(" Your Computer OS System is Mac Os");
}