分類
git 使用紀錄

Git 使用記錄(移除檔案+更改檔名)

1.移除檔案
在一個容器中新增檔案時,此檔案會被歸類於未追蹤
我們新增一個文件叫 testrm 來試試看,輸入

echo 'test in rm' > testrm
git status

 

的確是在未追蹤,現在我們把它加入容器中(改變狀態為已加入)
輸入

git add testrm

如果這時候想把它從已加入去除的話,輸入

git rm --cached

OK~ 看起來是從已加入去除了,不過為了保險起見,還是輸入 git status 看看

git status

testrm 的確回復到未追蹤的狀態
那如果檔案已加入而且已被提交,如何從容器中去除?
再次把 testrm 加入並提交,輸入

git add testrm
git commit -m "commit testrm"

到此 testrm 已被提交,從已提交中移除需要2個步驟
第ㄧ種情況想保留 testrm 檔案,單純的想從容器中移出,輸入

git rm --cached testrm

再輸入

git commit -m "delete testrm"

如此檔案本身存在只是會被移出容器
第二種情況如果想連檔案本身一起移除的話,輸入

git rm testrm

到此,觀察資料夾雖然可以看到 testrm 已經不存在,但還是要提交,輸入

git commit -m "delete testrm"

現在 testrm 不但被移出容器外且本身也不存在
2.更改檔名
先新增一個檔案為 testmv ,輸入

echo 'test_mv' > testmv

更改檔名只能在已加入的狀態修改,所以先把它加入吧,輸入

git add testmv

更改檔名的格式為 git mv 原檔名 新檔名 ,輸入

git mv testmv testmv2

在資料夾中可以看到 testmv 已經變成 testmv2 了
不過輸入 git status 看看,會提示必須提交才算完成

git status

輸入

git commit -m "rename testmv to testmv2"
git status

可以看到更名的步驟已經提交完成

至於已經提交檔案的更名步驟也是相同,必須先輸入更名的指令,再提交

分類
git 使用紀錄

Git 使用記錄(初始化容器+複製外部容器+加入檔案+觀察容器檔案內容+觀察檔案差異+提交檔案+觀察提交檔案內容)

1.初始化容器
選擇想要的資料夾當作容器,在家目錄中新建資料夾 Git_Project 為範例
開啓終端機,輸入

cd ~
mkdir Git_Project
cd Git_Project
echo 'test in git' > testword

到此完成 Git_Project 資料夾和 以 test in git 為內容的 testword 文件建立
接著輸入

git init

指定以目前資料夾(Git_Project)為容器
2.複製外部容器
這裡以 google code 為例
輸入

git clone https://0000foxx@code.google.com/p/foxx-gitproject-gittest/

也可以指定名稱(dir_name)
輸入

git clone https://0000foxx@code.google.com/p/foxx-gitproject-gittest/  dir_name

2.1 使用 git ssh clone

git clone ssh://username@xxx.xxx.xxx.xxx/home/username/absolute/path

 
3.加入檔案

git add testword

如果檔案中有許多資料想全部加入追蹤,輸入

git add .

4.觀察容器檔案狀態
輸入

git status

如果照著以上步驟作的話會得到以下畫面

顯示我們在  master 的 branch 上, testword 檔案將會在下次提交的時候記錄起來
接著修改 testword 內容為
test in git 2
再輸入 git status 看看

git status

git 很好心的提示我們 testword 已經被修改了,需要重新 add 才行,輸入

git add testword
git status

OK~ 修改的部分也已經加入了
5.觀察檔案差異
剛剛使用 git status 只會顯示已被修改,若想進一步觀察修改了哪些內容可以使用 git diff
修改 testword 內容為
test in git 3
接著輸入

git diff

上次修改和目前的差異,注意 git diff 只能顯示還未 add 的差異,如果要比較已經提交和目前的差異要用 git diff –cached 或 git diff –staged
6.提交檔案
輸入

git commit

提交時必須輸入註解訊息,如果是用 vim 先按 a ,下方的提示會顯示 INSERT ,再輸入註解訊息完成後,先按 esc 再按 :wq
提交完成會得到以下畫面

可以得知在哪個 branch(master) 上,以及註解內容等等
再次輸入

git status

(可以看到已經沒有東西可以提交)

7.觀察提交檔案內容
想要知道最近提交的內容,可以使用 git log 或 git show
輸入

git log

(會顯示每個提交的內容,識別碼)

git show 可以附註識別碼來指定觀察
輸入

git show 4941f8fbb701c41f71da7dd70653b43ced98d40f

可以看到提交檔案更詳細的內容

分類
git 使用紀錄

Git 使用記錄 (下載Git+安裝Git+設定配置姓名和Email)

1.安裝git
直接到官網下載安裝,除了 Mac/Windows 使用安裝檔(dmg/exe),其他的都用指令安裝
2.測試git
開啓終端機,輸入

git

順利的話會得到類似以下畫面的回應,代表已經安裝完成

來看看目前版本,輸入

git --version

會顯示安裝版本

3.設定個人訊息
在提交檔案之前先來設定 個人訊息 的配置,包含名字和E-mail
輸入

git config user.name "your_name"
git config user.email "your_email"

如果出現

error: could not lock config file .git/config: No such file or directory

 
請加入 –global

git config --global user.name "your_name"
git config --global user.email "your_email"

之後提交檔案都會使用這組個人訊息,當然想重新設定也是沒問題
可以使用 git config –list 來查看個人訊息

想要查詢指令如何使用,輸入
git help 指令

git help config
分類
基本教學

MacBook Pro 編譯 Android 核心

上一篇已建立並編譯 Android Source Tree ,這篇記錄 Android Kernel 的編譯
上篇提到的 Android Source Tree 並不包含 Kernel 的部分,因此當你想對 Kernel 作修改時 必須另外下載 Kernel 的部分
這篇也是照著 Google 提示的操作
先掛載 android.dmg 並移動到之前建立的 android 資料夾中,建立新資料夾(Android_KERNEL)以放置 Kernel 原始碼,如果想針對不同廠商核心編譯可以再建立方便識別的資料夾(panda)
使用 git 下載 Kernel

git clone https://android.googlesource.com/device/ti/panda

移動到 panda資料夾

cd panda

取得 commit message

git log --max-count=1 kernel

出現的訊息會類似下圖,我們需要的部分是 Built from kernel 後面那一串訊息
(cb5fc502c60be9305c5a007be335e860d9e7c0cb)

下載想編譯的kernel ,以google的例子下載 omap

git clone https://android.googlesource.com/kernel/common.git
git clone https://android.googlesource.com/kernel/exynos.git
git clone https://android.googlesource.com/kernel/goldfish.git
git clone https://android.googlesource.com/kernel/msm.git
git clone https://android.googlesource.com/kernel/omap.git
git clone https://android.googlesource.com/kernel/samsung.git
git clone https://android.googlesource.com/kernel/tegra.git

 
確認prebuilts-gcc在path中

export PATH=$(pwd)/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin:$PATH

編譯,其中 commit_from_first_step 填入(cb5fc502c60be9305c5a007be335e860d9e7c0cb)

export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-eabi-
cd omap
git checkout <commit_from_first_step>
make panda_defconfig
make
分類
基本教學

MacBook Pro 建立 Android Source Environment (不需 Ubuntu)

注意此篇不需建立 Ubuntu
規格: MacBook Pro (Lion) , Mac OS X 10.7 , Xcode 4.6.1
參考資料:AOSP官網
步驟直接從 Setting up Mac OS X build environment 開始即可
1.
建立 case-sensitive disk image
首先提到 Mac OS 是 case-insensitive (不區分大小寫) 但這會造成部分 git 指令的錯誤
所以建議我們製作 case-sensitive 的 disk image
你可以使用
A.launch Disk Utility and select “New Image”. A size of 25GB is the minimum to complete the build, larger numbers are more future-proof. Using sparse images saves space while allowing to grow later as the need arises. Be sure to select “case sensitive, journaled” as the volume format.
或者是
B.開啓終端機,輸入以下指令

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg

(強烈建議指令用複製貼上)
完成後在 /User/使用者 會出現 android.dmg.sparseimage ,當掛載這個disk Image 在桌面上便會出現一個磁碟空間
預設掛載disk image後出現的磁碟空間名稱為 untitled ,可改為 android
2.
編輯 ~/.bash_profile
開啓終端機輸入

vi ~/.bash_profile

並加入以下內容
(注意 android.dmg 要看實際的名稱修改,有可能是android.dmg.sparsefile
而 /Volumes/android 中的android也是一樣,視情況修改)

mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }

提示: 操作的步驟為先按 a 進入INSERT模式,再貼上,離開時先按esc結束編輯,再輸入:wq 儲存離開
3.
安裝需要的套件
3-1.
首先安裝 Xcode ,建議 Xcode 版本需要 3.1.4(含)以上, 到 Apple Developer Site 下載
如果已經安裝的 Xcode 版本太高(e.g. 4.x),還需要 Mac OS 10.5 SDK
Mac OS X 10.5 SDK 安裝方式為:先到這裡左邊搜尋框輸入 xcode 3.1.4 並下載,下載完成後點擊 xcode314_2809_developerdvd.dmg
接著在桌面上會出現 Xcode Tools -> Packages -> MacOSX10.5.pkg ,點擊後更改安裝位置選擇 Developer ,完成後可以看到 Developer/SDKs 中多了  MacOSX10.5.sdk
3-2.
安裝 MacPorts,這部分也是照著google的提示
先把

export PATH=/opt/local/bin:$PATH

加到 ~/.bach_profile 中
接著取得 make , git , gpg packages
開啓終端機輸入

POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupg

如果系統是 MacOS 10.4,還要加入以下指令

POSIXLY_CORRECT=1 sudo port install bison

3-3.
為了消除 gmake 3.82 的bug, 建議安裝 gmake 3.81,
首先將以下指令加入 /opt/local/etc/macports/sources.conf 

file:///Users/Shared/dports

建立目錄

mkdir /Users/Shared/dports

移動到 /Users/Shared/dports 中,輸入

svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/

輸入

portindex /Users/Shared/dports

最後安裝 gmake 3.81

sudo port install gmake @3.81


4.
解除 file descriptor limit
在 ~/.bash_profile 加入

set the number of open files to be 1024
ulimit -S -n 1024


5.
下載 Android Source Tree
5-1.
安裝 Repo
開啓終端機輸入

mkdir ~/bin
PATH=~/bin:$PATH

下載 Repo

curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
chmod a+x ~/bin/repo

5-2.
初始化 Repo
先掛載步驟1.建立的disk image,接著桌面上會出現一個磁碟空間
開啓終端機輸入指令移動到該磁碟空間中並建立一個空的目錄(WORKING_DIRECTORY)

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

(WORKING_DIRECTORY,名稱可自取)
接著輸入初始化指令(這個是下載 master 版本,如果Mac OS 版本為10.7 強烈建議選擇)

repo init -u https://android.googlesource.com/platform/manifest

也可以看看別的分支版本,使用 -b 指定版本(這個是下載指定版本 android-4.0.1_r1)

repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1

在過程中會出現輸入名字和email以及顏色測試,就按照需求輸入吧
(使用 repo 指令若出現 command not found,請輸入整個路徑如 ~/bin/repo init ….)
5-3.
下載 Android Source Tree (此下載時間相當久,3m速度大概要3~4小時)
終端機輸入

repo sync

6.
建立和執行
6-1.
初始化環境,終端機輸入

source build/envsetup.sh

6-2.
選擇 Target
終端機輸入

lunch full-eng

可以自由選擇輸入的參數

Build name Device Notes
full emulator fully configured with all languages, apps, input methods
full_maguro maguro full build running on Galaxy Nexus GSM/HSPA+ (“maguro”)
full_panda panda full build running on PandaBoard (“panda”)
and the BUILDTYPE is one of the following:
Buildtype Use
user limited access; suited for production
userdebug like “user” but with root access and debuggability; preferred for debugging
eng development configuration with additional debugging tools

6-3.
編譯
終端機輸入

make -j4

(4代表行程數)

7.
錯誤處理
第一次編譯會出現 strnlen 錯誤,

搜尋發現 此篇 有提到解決方法
簡單的說就是修改 ./external/elfutils/config-compat-darwin.h 中的 strnlen 方法為
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070

static inline size_t strnlen (const char *__string, size_t __maxlen)
{
int len = 0;
while (__maxlen– && *__string++)
len++;
return len;
}

#endif
修正以後重新編譯會得到第二次錯誤

這次的錯誤是在 ld: illegal text-relocoation 開始,google發現老外有一樣的錯誤,原因眾說紛紜,有人說也許跟 Android Source 版本不同有關,這次只好選擇下載master版本
使用以下指令來取得 master 版本(原先發生錯誤版本為 Android 4.0.1_r1)

repo init -u https://android.googlesource.com/platform/manifest

一樣重複下載…(3~4個小時)

repo sync

初始化

source build/envsetup.sh

選擇 Target

lunch full-eng

編譯

make -j4

編譯完成!!!

跑模擬器
終端機輸入

emulator

基本上建立環境並不難,只是步驟稍微多了點,請先注意本地端Mac OS SDK版本,還有在
下載 Android Source Tree 以及編譯的時候通常都要2~3個小時,時間的花費相當驚人@@
下一篇會介紹 MacBook Pro 編譯 Android kernel

分類
Uncategorized

取得網頁原始碼

使用 url 來取得網頁原始碼相當簡單,也能藉此加上其他自動化的功能如自動偵測股票網站的數值等等

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class PageData {
    /**
     * 取得網頁資料
     * @return : 回傳string型態的網頁資料
     */
    public static String getPageData(String http){
        URL u = null;
        InputStream in = null;
        InputStreamReader r = null;
        BufferedReader br = null;
        StringBuffer message = null;
        try {
           u = new URL(http);
           in = u.openStream();
           r = new InputStreamReader(in, "UTF-8");
           br = new BufferedReader(r);
           String tempstr = null;
           message = new StringBuffer();
           while ((tempstr = br.readLine()) != null) {
               message.append(tempstr);
           }
        } catch (Exception e) {
           e.getStackTrace();
           System.out.println(e.getMessage());
        } finally {
           try {
              u = null;
              in.close();
              r.close();
              br.close();
           } catch (Exception e) {
           }
    }
        return message.toString();
    }
}

 
接著再根據 getPageData() 回傳的字串做處理

分類
Uncategorized

取得執行時的各種特性(System.getProperties())

System.getProperties() 可以取得各種特性

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class GetExecProperty {
    static String execSimerPath = "";
    public static void showSupplyProperties() {
        Properties pros = System.getProperties();
        Set s = pros.keySet();
        Iterator it = s.iterator();
        while (it.hasNext()) {
            String keystr = it.next().toString();
            String tempstr2 = System.getProperty(keystr);
            System.out.println("key: " + keystr + " ,value: " + tempstr2);
        }
    }
    /**
     * return path of jar
     */
    public static String getExecPath() {
        String tempstr = "";
        String local = System.getProperty("user.dir");
        System.out.println("System.getProperty user.dir: " + local);
        String[] spilttemp = local.split("\\");
        for (int i = 0; i < spilttemp.length; i++) {
            tempstr = tempstr + spilttemp[i] + "\\";
        }
        System.out.println("tempstr: " + tempstr);
        return tempstr;
    }
}

 

 
分類
Uncategorized

使用 properties 存取資料

資料的儲存方式除了database之外,還有 properties 可以選擇,優點是使用簡單,但是相對的儲存資料類型較少。

package fothers;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesExample {
    public static void main(String[] args){
        storeProperties();
        loadProperties();
        storePropertiesWithXml();
        loadPropertiesWithXml();
    }
    public static void storeProperties(){
        Properties proper = new Properties();
        proper.setProperty("key1", "value1");
        try {
            proper.store(new FileWriter("d:\testproperties\"+"testpro.properties"), "ps");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void loadProperties(){
        Properties proper = new Properties();
        try {
            proper.load(new FileReader("d:\testproperties\"+"testpro.properties"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void storePropertiesWithXml(){
        Properties proper = new Properties();
        proper.setProperty("keyxml", "valuexml");
        try {
            proper.storeToXML(new FileOutputStream("d:\testproperties\"+"testpro.xml"), "ps");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void loadPropertiesWithXml(){
        Properties proper = new Properties();
        try {
            proper.loadFromXML(new FileInputStream("d:\testproperties\"+"testpro.xml"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

分類
Uncategorized

使用 JDBC 連接資料庫(Postgresql)

使用JDBC連接資料庫的範例,先準備連結 postgresql 需要的 jar檔(postgresql-8.4-701.jdbc4.jar)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
public class ConnToPostgresql {
    //show all tables in database
    public static String COMM_SHOW_ALLTABLES = "select * from information_schema.tables" +
            " where table_schema not in ('information_schema','pg_catalog')";
    //show all databases
    public static String COMM_SHOW_ALLDATABASE = "select * from pg_database";
    //show size of databases(pg_database_size() support PostgreSQL 8.1)
    public static String COMM_SHOW_DB_SIZE = "SELECT pg_database.datname," +
            " pg_size_pretty(pg_database_size(pg_database.datname)) AS size" +
            " FROM pg_database;";
    Connection conn;
    String driver = "org.postgresql.Driver";
    String url;
    String ipNum;
    String portNum;
    String dbName;
    String userName;
    String passWord;
    public ConnToPostgresql(String ipnum,String portnum,
            String dbname,String username,String password){
        ipNum = ipnum;
        portNum = portnum;
        dbName = dbname;
        userName = username;
        passWord = password;
        try {
            Class.forName(driver);
            // String url = "jdbc:postgresql://localhost:5432/test1225";
            url = "jdbc:postgresql://" + ipNum + ":" + portNum + "/"
                    + dbName;
            try {
                if (userName == null && passWord == null) {
                    conn = DriverManager.getConnection(url);
                } else {
                    conn = DriverManager.getConnection(url, userName, passWord);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public Connection getConn(){
        return conn;
    }
}

只要建立 ConnToPostgresql 物件,在建構子中傳入帳號和密碼,就能建立和資料庫連線

分類
Uncategorized

讀取 csv 檔,輸出 arraylist

讀取 csv 檔 ,輸出成 ArrayList

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadCSVtoArrayList {
    /**
     * Read csv file
     * @param csvpath path of csv file
     * @return ArrayList, element is data of column
     */
    public static ArrayList<ArrayList> readCSVToArrayList(String csvpath) {
        ArrayList<ArrayList> dataAL = new ArrayList<ArrayList>();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(csvpath));
            reader.readLine();// read first line in csv
            String line = null;
            while ((line = reader.readLine()) != null) {
                ArrayList<String> ticketStr = new ArrayList<String>();
                String item[] = line.split(",");
                ticketStr.clear();
                for(int i=0; i<item.length; i++){
                    ticketStr.add(i, item[i]);
                }
                dataAL.add(ticketStr);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataAL;
    }
}

接著就能操作 ArrayList 中的資料,完成需要的功能