2008年8月27日 星期三

Mac無法開機時的處理

資料來源: http://ca96.blogspot.com/2008/04/mac.html

當面臨到無法開機的情況時,首先要判斷的是屬於下列哪一種情況?

1. 空白的藍色或灰色畫面:可能是由磁碟權限、第三方軟體、或偶發的硬體問題所造成的。適用招式:ㄧ、二、三、四

2. 出現 "NO"、壞掉的資料夾或閃動的問號:這是由於 Mac 找不到開機所需的系統,可能是磁碟問題所致。適用招式:三、四

3. 黑色畫面,沒有聲音:你所碰到的可能是電力問題、主機板上的電池壞掉、故障的記憶體或其他的硬體問題


。第一招:以安全模式開機,開機快捷鍵:Shift
Step1: 開機時按住 Shift 鍵不放
Step2: 直到螢幕上出現灰色蘋果標誌及旋轉的齒輪時才放開Shift鍵
Step3: 如果順利啟動,“Safe Boot”字樣應該會在啟動時出現或在顯示於登入視窗中(以安全模式啟動會比一般啟動花更多的時間,因為系統會為開機磁碟進行磁軌檢查。耐著性子吧!若要離開安全模式,將電腦重新開機,並且不要在開機時按任何鍵即可。 )


。第二招:單一使用者模式,開機快捷鍵:Command(蘋果鍵) + S
Step1: 開機時按住 Command + S。如果看到黑底白字的文字模式,別嚇到了,這是正常的
Step2: 在游標後輸入 fsck - fy
Step3: 之後按 return 鍵。如果回覆的訊息是 " File System was Modified ",重複第二步驟直到系統回覆 " No problems were found "
Step4: 輸入 reboot 並按 return 鍵。(之後電腦就會重新開機了,如果順利就會進入系統,如果還是不行,那就試試其他的招式吧!)

其他解決方案:AppleJack(注1)
如果你在你的MAC 出問題之前曾安裝過 AppleJack,那麼過程就會簡單許多
1.輸入"applejack auto restart",之後按 return 鍵
2.稍待個幾分鐘,你的 MAC 將會重新開機並完成所有的修護作業


。第三招:用蘋果 CD/DVD 開機,開機快捷鍵﹕C
Step1: 放入 Mac OS X 安裝光碟(開機時壓住滑鼠不放即可打開光碟托盤[退片])
Step2: 開機時按住 C 鍵,直到灰色蘋果標誌出現在螢幕上才放手
Step3: Mac OS X 10.4.x的使用者:從選單列選擇「工具程式」> 「磁碟工具程式」
Step4: 從左側列出的硬碟圖示中選擇自己的硬碟,然後單點修理工具分頁中的「修復磁碟」。
Step5: 磁碟回報正常後,單點「修復磁碟權限」
Step6: 之後重開機,並在開機時不要按任何按鍵


。第四招:硬碟模式,開機快捷鍵:T
當其他的招式都失效時,硬碟模式可以讓你透過其他電腦來存取你的硬碟。這樣一來你就可以執行一些診斷、維護或者備份重要的工作檔案。
Step1: 開機時按住 T 鍵,直到看見 FireWire 圖示才放開。
Step2: 用一條 FireWire 線將故障的 Mac 接上正常的 Mac。
Step3: 開啟正常 Mac 上的磁碟工具程式 ( 位於應用程式 / 工具程式 )。
Step4: 從左側列出的硬碟圖示中選擇故障的硬碟,然後單點修理工具分頁中的「修復磁碟」。
Step5: 如果在正常 Mac 的桌面上可以看的到故障 Mac 的硬碟圖示,那麼說不定還可以把一些重要的檔案復原或者備份起來。


(注1):AppleJack 是一個專為 MAC OSX 開發的系統維護工具,當無法進入系統的圖形介面而手邊又沒有可開機的光碟時,AppleJack 可以在單一使用者模式下,以選單的方式協助使用者進行障礙排除。

2008年8月11日 星期一

javaMail(一)



Setup


If you use Java 2 Platform,
Enterprise Edition (J2EE) 1.3, you're in luck: it includes JavaMail, so
no additional setup is required. If, however, you're running Java 2
Platform, Standard Edition (J2SE) 1.1.7 and upwards, and you want email
capability for your applications, download and install the following:


資料來源: http://www.javaworld.com/javaworld/jw-10-2001/jw-1026-javamail.html?

Sample

以下的範例,以gmail 為例

package com.mail;

import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class msgsendsample {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";

private static final String emailMsgTxt = "Test Message Contents";
private static final String emailSubjectTxt = "A test from gmail test.....";
private static final String emailFromAddress = "xxxx@gmail.com";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = {"xxxx@gmail.com","xxxx@yahoo.com.tw"};

msgsendsample() {
}

public void sendMail() throws MessagingException {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

new msgsendsample().sendSSLMessage(sendTo, emailSubjectTxt,
emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");

}

public void sendSSLMessage(String recipients[], String subject,
String message, String from) throws MessagingException {
boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");



Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxx", "xxxx"); //Gmail的帳號及密碼
}
});


session.setDebug(debug); // 方便Debug用

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}

public static void main(String args[]) throws Exception {

msgsendsample ms = new msgsendsample();
ms.sendMail();
}


}

p.s 一開始我只下載了javamail 所以在run的過程當中會出現 java.lang.NoClassDefFoundError: javax/activation/DataSource ,後來下載 javaBean Activation Framework加到Lib之後就解決了




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