葛瑞斯肯相關資訊
聯絡我 : x831617@gmail.com
臉書粉專 : 葛瑞斯肯樂活筆記

目前分類:Java 學習筆記 (62)

瀏覽方式: 標題列表 簡短摘要

最近要使用glassfish,所以抓了最新的Eclipse EE版本如下:

1.jpg

文章標籤

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

最近使用 Java 來產生為一的識別碼,想到的就是Java 內建的 UUID 功能,如下程式碼所示:

import java.util.UUID;
public class UUIDTest {
public static void main(String args[]) {
UUID uuid = UUID.randomUUID();
System.out.println("UUID: " + uuid);
}
}
=Console=
UUID: ddccf1b8-3841-4714-a12d-881ae3fe7ad5
view raw UUID hosted with ❤ by GitHub

文章標籤

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

undefined

以下紀錄使用 Regular Expression 過濾標點符號的方式:

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

undefined

以下介紹如何把 Eclipse 的專案放到 Github 上。

葛瑞斯肯 發表在 痞客邦 留言(1) 人氣()

JExcel 讀取與產生 excel 教學

先來記錄如何讀取excel

文章標籤

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

下述紀錄使用 HttpURLConnection 連接雲端服務的方式:

public static void main(String args[]){
try{
// HttpURLConnection
String trueQuestion = "";
String query = "{\"question\":\""+ trueQuestion + "\"} ";
String url = "POST URL";
URL endpoint = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) endpoint.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Content-Type", "application/json");
DataOutputStream outputStream = new DataOutputStream(httpConnection.getOutputStream());
outputStream.write(query.toString().getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = "";
while( (line = br.readLine()) != null ) {
System.out.println(line);
}
}catch(Exception e){
e.printStackTrace();
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

文章標籤

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

下述紀錄使用 Httpclient 進行 Serivce POST 的行為:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public static void main(String[] args) {
HttpClient httpclient = new DefaultHttpClient();
String postRequest = "POST format";
try {
String url = "Service URL";
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type", "application/json");
// Request body
StringEntity reqEntity = new StringEntity(postRequest,"UTF-8");
request.setEntity(reqEntity);
System.out.println(request.toString());
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
String temp = EntityUtils.toString(entity);
System.out.println(temp);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

undefined

有時候在包jar的時候會需要存取jar中自己的檔案,以下紀錄如何存取jar中的檔案:

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

最近又開始寫使用隨機整數的方式,以下是簡單的 Sample code,可以取得隨機的整數:

public class test {
public static void main(String args[]){
Random rand = new Random();
int i = rand.nextInt(); //int類別的隨機整數
i = rand.nextInt(100); //0~100 以內的隨機整數
i = (int)(Math.random()*100); //0~100 以內的隨機整數
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

 

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

Java 中使用 HashMap 是一個常見的用法,但是為了維持加入物件的順序,就會選擇使用 LinkedHashMap,以下介紹如何取得 LinkedHashMap 第一個與最後一個元件的方法:

package test;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class test {
public static void main(String args[]){
LinkedHashMap<String,String> list = new LinkedHashMap<String,String>();
list.put("1", "1");
list.put("2", "2");
list.put("3", "3");
list.put("4", "4");
list.put("5", "5");
for(Map.Entry<String, String> str:list.entrySet()){
System.out.println(str.getKey() + " " + str.getValue());
}
if(list.size()>0){
String key = list.entrySet().iterator().next().getKey();
System.out.println("First element: " + key + " " + list.get(key));
String finalKey = "";
Iterator<Entry<String, String>> iter = list.entrySet().iterator();
while(iter.hasNext())
finalKey = iter.next().getKey();
System.out.println("Final element: " + finalKey + " " + list.get(finalKey));
}
Set<Entry<String,String>> setOfList = list.entrySet();
Entry<String,String> [] entry = new Entry[list.size()];
setOfList.toArray(entry);
System.out.println("First element: " + entry[0].getKey() + " " + entry[0].getValue());
System.out.println("Final element: " + entry[list.size()-1].getKey() + " " + entry[list.size()-1].getValue());
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

 

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

在撰寫一個類別的時候,有時候會把裡面的變數與函數變成一個內部類別來使用,方便管理或者方便變成另一個不同的執行緒。

如同下述的程式碼

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

有時候在進行文字處理的時候,是需要辨識文字的語系來進行不同的判斷。

Language Detection 這個功能已有一個接近完善的 Java API,其主要網址如下:

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

最近常用 HashMap 裡面物件的排序,特此紀錄一下,程式碼如下:

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Test {
public int a = 0;
public static void main(String args[]){
Test ds1 = new Test();
ds1.a = 6;
Test ds2 = new Test();
ds2.a = 5;
Test ds3 = new Test();
ds3.a = 7;
HashMap<String,Test> map = new HashMap<String,Test>();
map.put("ds1", ds1);
map.put("ds2", ds2);
map.put("ds3", ds3);
List<Map.Entry<String, Test>> list = new LinkedList<Map.Entry<String, Test>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Test>>(){
public int compare(Map.Entry<String, Test> o1, Map.Entry<String, Test> o2) {
// TODO Auto-generated method stub
return o1.getValue().a-o2.getValue().a;
}
});
for(int i=0;i<list.size();i++)
System.out.println(list.get(i).getKey() + " " + list.get(i).getValue().a);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

 

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

有時候迴圈會有好幾個,但是又想裡面的條件達成後就直接中斷多個迴圈,做法可用下列的方式命名迴圈與中斷迴圈,下述的程式碼描述的就是用一個無窮的while迴圈,裡面搭配一個for迴圈,並且在值大於5的時候結束這個無窮的while迴圈。

 

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

程式目標: 抓取電腦當下的時間

程式碼如下:

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

當大型專案開發時,有時候需要記錄 class 產生出來的物件,該物件為大型的資料結構,但是用找不到現有的檔案格式紀錄時,就可以考慮使用序列化方式 (Serialization) 記錄,但是該 class 必須實作序列化(implements serializable),如果沒有的話就會出現下列的錯誤:

Exception in thread "main" java.io.NotSerializableException: Test.person
文章標籤

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

Java本身是一個物件導向程式語言,實作類別是家常便飯,接下來要說明的是如果把物件(Object)印出來,會觸發的函數功能。

下述的程式建立了一個 class,並且用這個 class 建立一個物件 person,並且灌入 person 建構子需要的變數。

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

【Java】 記憶體量測與執行時間量測

本文節錄自下述的連結,並且加入適當的翻譯與註解:

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

undefined

使用多線程來跑程式的時候,有時候會遇到資源共享的問題造成記憶體或其他資料的非同步或是不對稱。

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

最近弄 Eclipse project,因為產生出太多的 workspace,看了很礙眼,想要把它完全抹除掉。

像下圖這樣,希望能夠點到 Switch Workspace的時候裡面是空的。

葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()

1 234