Wednesday, May 27, 2009

Convert an input String to InputStream

We can use java.io.ByteArrayInputStream and use string.getBytes() like this

String input = "Input Stream";
try {
ByteArrayInputStream ba = new ByteArrayInputStream(input.getBytes());

// TODO Codes //
// ...
// ...
//

} catch (Exception e) {
e.printStackTrace();
}
Somebody use this solution for parser xml ^^, have a fun !!!


Tuesday, May 26, 2009

การค้นหา file บน Linux (คำสั่ง locate) #2

คำสั่ง locate เป็นอีกหนึ่งคำสั่งที่ใช้ในการค้นหาไฟล์ซึ่งจะใช้งานได้ง่ายกว่า find และมี parameter ไม่นากนักการค้นหาไฟล์ใน locate จะไม่ได้ค้นหาไฟล์จากทุกๆ path จะมีบางที่ที่ locate ไม่ได้เข้าไปค้นหา เช่น temporary files และ home directory ของ user แต่ละคน เป็นต้น แต่ถ้าเราต้องการให้ locate ค้นหาไฟล์ที่อยู่ใน home directory ของเราเองสามารถทำได้โดยการสร้างฐานข้อมูลขึ้นมาแล้วเก็บฐานข้อมูลนั้นไว้ที่ home directory ซึ่งสามารถทำได้ดังนี้

$ cd ~
$ updatedb --output=$HOME/.tophy_locatedb --localpaths=`pwd`

คำสั่งนี้จะสร้างไฟล์ ./tophy_locatedb ซึ่งเป็นไฟล์ฐานข้อมูลของไฟล์ทั้งหมดใน home directory ของเรา

$ locate -d ~/.tophy_locatedb Calymore

คำสั่งนี้จะเป็นการค้นหาไฟล์ที่ชื่อ Claymore ในฐานข้อมูลที่เราสร้างขึ้น โดยปกติแล้วเราควรจะทำการ update ฐานข้อมูลทุกๆ วันซึ่งมักจะทำกันในช่วงเวลากลางคือด้วยการใช้คำสั่ง crontab (ไว้มีเวลาจะมาเขียนเรื่อง crontab)

$ locate Claymore

เป็นการค้นหาไฟล์ที่ชื่อว่า Claymore โดยผลลัพธ์จะแสดงเป็น full path เช่น

/home/tophy/anime/Claymore
/homt/tophy/OST/Claymore

นอกจากจะหาจากคำเต็มๆ แล้วยังใช้อักขระ '*' เข้าช่วยในการค้นหาเหมือนคำสั่ง find ได้ด้วย เช่น

$locate "Claymore*"

เป็นการหาไฟล์ใดๆ ที่ขึ้นต้นด้วยคำว่า Claymore

$locate "Claymore*" -q

จะคล้ายกับคำสั่งด้านบนแต่ว่าจะไม่แสดง error message ขึ้นมา ถ้าเทียบกับคำสั่ง find แล้วจะคล้ายกับ 2>/dev/null

$ locate "*Claymore*" -n 10

จะเหมือนกับตัวอย่างที่ 2 แต่จะแสดงผลลัพธ์ออกมาแค่ 10 รายการเท่านั้น

$ locate "*Claymore*" -i

จะหาไฟล์ที่ขึ้นต้นด้วยคำว่า Claymore โดยไม่สนใจอักขระตัวเล็กตัวใหญ่ (non-case sensitive) นอกจากตัวอย่างที่ยกมาแล้ว locate ยังมี parameter อีกตัวหนึ่งที่น่าสนใจมากคือ -r ซึ่งเป็นการใช้ regular expression มาใช้งานในการค้นหาไฟล์ เช่น

$locate -r "/Claymore.*\.avi"

เป็นคำสั่งที่ใช้ค้นหาไฟล์ที่ขึ้นต้นด้วยคำว่า Claymore และจบลงด้วยคำว่า .avi เช่น

Claymore [raw] - 01.avi
Claymore [raw] - 02.avi

Credit : http://www.secguru.com,
http://are.berkeley.edu,
http://www.codecoffee.com/tipsforlinux/articles/20.html

ref: http://top85.spaces.live.com/blog/cns!D84A636C1EFDB724!230.entry

การค้นหา file บน Linux (คำสั่ง find) #1

การค้นหา ไฟล์ใน linux สามารถทำได้หลายวิธีด้วยกันในหัวข้อนี้จะกล่าวถึงการค้นหาไฟล์โดยใช้เครื่อง มือ find คำสั่ง find เป็นคำสั่งที่ได้รับความนิยมในการใช้งานมากเพราะว่าใช้งานง่ายแต่ว่าจริงๆ แล้วคำสั่งนี้มี parameter มากมายถ้า เราเรียนรู้การใช้งาน parameter เหล่านั้นและใช้งานได้อย่างคล่องแคล่วแล้วล่ะก็เครื่องมือ find ก็ถือได้ว่าเป็นเครื่องมือที่เยี่ยมที่สุดสำหรับการค้นหาไฟล์ เราสามารถใช้คำสั่ง find แบบง่าย ได้เช่น

$ find /home/tophy -name 'index*'
$ find /home/tophy -iname 'index*'

คำสั่งข้างบนจะเป็นการค้นหาไฟล์ที่อยู่ใน directory /home/tophy และ subdirectory
คำสั่งแรกจะใช้ parameter -name เพื่อระบุชื่อไฟล์และตามด้วย 'index*' ซึ่งหมายความว่าเป็นไฟล์ใดๆ ที่ขึ้นต้นด้วยคำว่า index
คำสั่งที่ 2 จะคล้ายกับคำสั่งแรกแต่จะใช้ parameter -iname ซึ่งเป็นการไม่สนใจตัวอักษรเล็กหรือใหญ่ (non-case sensitive) เช่น ชื่อไฟล์ที่ขึ้นต้นด้วย Index, inDex, INDEX เป็นต้น

$ find /home/tophy -name 'index*' 2>/dev/null

คำสั่งด้านบนจะคล้ายกับตัวอย่างก่อนหน้าแต่จะเพิ่มในส่วน 2>/dev/null ซึ่งหมายความว่าระหว่างการค้นหาถ้ามี error message เกิดขึ้นจะนำ error message ไปใส่ไว้ใน /dev/null ซึ่งเป็นอุปกรณ์ที่ไม่ว่าจะใส่อะไรเขาไปสิ่งนั้นก็จะหายไป ถ้าเราต้องการดู error message ทำได้โดยเปลี่ยนจาก /dev/null เป็นชื่อไฟล์ใดๆ ไว้เก็บ error message ปัญหาที่อาจะะเกิดขึ้นได้ เช่น ผู้ใช้ไม่ได้เป็น root แต่ find ในส่วนของ root

$ find -name met*

รูปแบบคำสั่งแบบนี้จะไม่ระบุ path เหมือนคำสั่งก่อนหน้าซึ่งหมายความว่าให้ค้นหาไฟล์ที่ขึ้นต้นด้วยคำว่า met จาก directory ปัจจุบันและ subdirectory จากคำสั่งข้างต้นสามารถนำมาประยุกต์ได้ เช่น

find . -print
find .
find

คำสั่งทั้ง 3 จะเป็นการแสดงรายชื่อไฟล์ทั้งหมดที่อยู่ใน directory ปัจจุบัน

$ find / -name '*.mp3' -size -5000k
$ find / -size +10000k

คำสั่งแรกจะค้นหาจากตำแหน่ง / โดยค้นหาไฟล์ที่ลงท้ายด้วย .mp3 และมีการกำหนด parameter -size เข้าไปเพื่อระบุว่าค้นหาไฟล์ที่มีขนาดน้อยกว่า 5MB (-5000k)
คำสั่งที่ 2 จะคล้ายคำสั่งแรกแต่จะเป็นการค้นหาไฟล์ใดๆ ก็ได้ที่มีขนาดไฟล์มากกว่า 10MB (+10000k)

$ find /home/tophy -amin -10 -name '*.c'
$ find /home/tophy -atime -2 -name '*.c'
$ find /home/tophy -mmin -10 -name '*.c'
$ find /home/tophy -mtime -2 -name '*.c'

คำสั่งแรกจะค้นหาไฟล์จากตำแหน่ง /home/tophy ชื่อไฟล์ที่ลงท้ายด้วย .c ที่ access ไปเมื่อ 10 นาทีที่แล้ว
คำสั่งที่ 2 คล้ายกับคำสั่งแรกจะต่างที่เวลาที่ access จะเป็นช่วง (2-1)*24-2*24 ชม. นั่นก็คือ 24-48 ชม.
คำสั่งที่ 3 คล้ายกับคำสั่งแรกแต่จะค้นหาไฟล์ที่มีการ modified เมื่อ 10 นาทีที่แล้ว
คำสั่งที่ 4 คล้ายกับคำสั่งแรกแต่จะค้นหาไฟล์ที่มีการ modified เป็นช่วง (2-1)*24-2*24 ชม. นั่นก็คือ 24-48 ชม.
ระบบการนับทั้ง atime และ mtime ถ้าเราใส่ค่า 0 หรือ -1 จะหมายถึงเวลาภายใน 24 ชม.

$ find / -name 'Claymore*' -and -size +10000k
$ find / -size +10000k ! -name "Claymore*"
$ find / -name 'Claymore*' -or -size +10000k

คำสั่งทั้ง 3 เป็นการประยุกต์ใช้ boolean operator มาช่วยในการค้นหาไฟล์
คำสั่งแรก ค้นหาไฟล์เริ่มที่ตำแหน่ง / ที่ขึ้นต้นด้วยคำว่า Claymore และขนาดไฟล์มากกว่า 10MB
คำสั่งที่ 2 ค้นหาไฟล์เริ่มที่ตำแหน่ง / ที่มีขนาดไฟล์มากกว่า 10MB และต้องเป็นไฟล์ที่ไม่ได้ขึ้นต้นด้วยคำว่า Claymore
คำสั่งที่ 3 ค้นหาไฟล์เริ่มที่ตำแหน่ง / ที่ขึ้นต้นด้วยคำว่า Claymore หรือไฟล์ใดๆ ที่มีขนาดมากกว่า 10MB

$ find / - name 'Claymore*' -exec ls -l

รูปแบบคำสั่งแบบสุดท้ายจะมีความหลากหลายมากขึ้นประสิทธิภาพของการค้นหาจะขึ้นอยู่กับการนำไปประยุกต์ของผู้ใช้ parameter ของคำสั่งนี้คือ -exec ซึ่งค่าตามหลังจะเป็นคำสั่งที่ทำงานหลังจากคำสั่ง find ด้านหน้าทำงานเสร็จสิ้น ในตัวอย่างจะเป็นการหาไฟล์ที่ขึ้นต้นด้วยคำว่า Claymore เริ่มที่ตำแหน่ง / จากนั้นนำผลลัพธ์ที่ได้มาแสดงผลโดยโชว์รายละเอียดของไฟล์ด้วยคำสั่ง ls -l
นอกจากวิธีการจากตัวอย่างทั้งหมดแล้วการใช้ find ยังมี parameter อีกหลายตัวที่มีความซับซ้อนและต้องทำความเข้าใจอีกมาก เช่น

-regex ใช้ regular expression ช่วยในการค้นหา
-iregex เหมือนกับคำสั่งด้านบนแต่เป็น non-case sensitive
-empty ค้นหาไฟล์หรือ directory ที่ว่างเปล่า
-type ค้นหาตามประเภทของไฟล์
-user ค้นหาไฟล์ที่มีเจ้าของเป็น user ที่ต้องการ
-group ค้นหาไฟล์ตาม group ที่ต้องการ

credit:http://www.codecoffee.com/tipsforlinux/articles/21.html,
http://www.secguru.com

ref: http://top85.spaces.live.com/blog/cns!D84A636C1EFDB724!229.entry

StringTokenizer how to

StringTokenizer stn = new StringTokenizer(string_text);
while (stn.hasMoreTokens()) {
String s = (String) stn.nextElement();
System.out.println(s);
}

Saturday, May 23, 2009

การ init ค่าใน Array เริ่มต้นของ java

เราทราบกันว่า การสร้าง array เป็นอย่างไรแล้ว ... แต่ ถ้าเราต้องการสร้าง array ขนาด 100 สมาชิก และอยากให้แต่ละสมาชิกมีค่า 0 หรือ 1 ทั้งหมดเราควรทำอย่างไร?

วิธีแรก ... กำปั้นทุบดิน ...

เราก็สร้าง array มา 100 ตัว ... จากนั้นก็วน loop ใส่ค่า 1 ให้กับทุกสมาชิก ดังนี้

int[] ex = new int[100];
for (int i = 0; i < ex.length; i++) {
ex[i] = 1;
}

อืม ... มันก็ง่ายดีครับ ... แต่มันคงไม่ดีแน่ ... ผมเลยหาๆ ดูใน api java (อันที่จริงเคาะ ctrl + space bar ใน ide มากกว่า) ก็ไปเจอ static method ใน Arrays ครับ จึงเป็นที่มาของวิธีที่สอง

วิธีที่สอง ... ใช้ Arrays.fill(Obj[], Obj_value);

ครับ ... เราสามารถใช้คำสั่ง Arrays.fill(ตัวแปรที่เป็น array อยู่แล้ว, ค่าที่ต้องการ) ซึ่งในที่นี้ผมให้เป็น 1

int[] ex = new int[100];
Arrays.fill(ex, 1);

เรียบร้อยครับ ... :) ก็เป็นการใช้สิ่งที่มีอยู่ให้เกิดประโยชน์ครับ :D

Wednesday, May 20, 2009

PS Unsharp Mask Value

Unsharp Mask PhotoShop Values:

Portrait

- Amount = 150
- Radial = 1
- Threshold = 10

Landscape
- Amount = 65
- Radial = 1
- Threshold = 2

General
- Amount = 85
- Radial = 2
- Threshold = 4

Monday, May 18, 2009

How to Add Google Analytics to Blogger (Blogspot)

Yesterday we talked about Google Analytics, Google's free service that lets you track the performance of your websites and blogs from one simple, easy-to-use interface.

Grab Your Google Analytics Code Block

  1. Login to Google Analytics at http://google.com/analytics/. The main Settings page loads.
  2. Click on Add Website Profile. A form displays.
  3. Select Add a Profile for a New Domain.
  4. Enter the URL of your site or blog.
  5. Select your country and time zone. Click Finish.
  6. Analytics provides you with a code block - a swatch of HTML - to add to your site's pages.
  7. Highlight the code block and then copy it by selecting Edit > Copy or Ctrl-C or Command-C.

Add the Google Analytics Code Block to Your Blogger Blog

  1. Login to http://www.blogger.com/. The Dashboard loads.
  2. Under the blog you want to add Analytics tracking to, click on Layout or Template.
  3. Click on Edit HTML. An editing screen for your blog template's HTML displays. Don't freak out. Just scroll to the bottom.
  4. Look for the end of the template. It'll look like:



</div>
<!-- end outer-wrapper -->
(Google Analytics Code Block is going to go here!!!)
</body>
</html>

  1. Put your cursor right before that tag.
  2. Paste the Google Analytics Code Block by selecting Edit > Paste, Ctrl -V or Command-V.
  3. Click Save Changes.

You have now added the Google Analytics Code Block to Your Blogger Blog.

Check Your Work

  1. To ensure that you have successfully added the Google Analytics Code Block to your Blogger blog, go back to http://google.com/analytics/.
  2. Next to your blog's URL it will say either Receiving Data (you were successful) or Tracking Not Installed (something is amiss).
  3. If it said Tracking Not Installed, click on Check Status. Google then checks your blog for the Analytics Code Block and reports back if it find it or not.
  4. If not, try re-pasting the Code Block in.


reference: http://andywibbels.com/2007/01/how-to-add-google-analytics-to-your-blogger-blog/

Sunday, May 17, 2009

Uninstall Windows Lives Messenger 2009

Many people found the trouble of windows live messenger 2009, because they can not uninstall it from them computer.

Today, I try with myself to uninstall my windows live messenger 2009 by using windows xp.

While I open Add/Remove programs, I did not find Windows Live Messenger ... but I found:

- Windows Live Essentials <-- Remove this one
- Windows Live OneCare safety scanner
- Windows Live Sign-in Assistant
- Windows Live Upload Tool

Then, I try to uninstall Windows Live Essentials that has Windows Live Messenger inside it.

And I succeed to remove it :)

If you can not remove it yet, please visit this site for more information:

http://www.bynatureonline.com/forums/viewtopic.php?f=12&t=478

Saturday, May 16, 2009

Friday, May 15, 2009

Enable Track Change in MS-Word 2003

1. Tools > Track Change or Ctrl + Shift _ E

2. Click Enable on Track Changes icon like this:

Now, you can edit the document and can see track change. :)

Wednesday, May 13, 2009

SMF RSS Feeder

You can access the feeder like rss, rss2 from SMF by using any feed reader.

Before using the feed, make sure you've enabled the feeds:

Admin -> News and Newsletters -> Settings
Check the box next to "Enable XML/RSS News".
The "Maximum post length" value determines how many characters of a post are output in the feeds. The default value (255) is fine for now

The SMF provide rss by using this example:

http://server/smf/index.php?type=rss;action=.xml;sa=news;limit=10


...

My site : http://sites.google.com/site/zamachita/miscellaneous/smfrssfeed

reference: http://www.simplemachines.org/community/index.php?topic=25009.0

Transfer file between server using SCP

Someone said that 'this is very easy'

Someone like me said, I can't remember this ... so ... I have to post it

$ scp file_name user@server:/path/to/directory


And, you can transfer file from between another server like this command:

$ scp user_a@server_a:/path/to/file_name user_b@server_b:/path/to/directory

For more information ... use google :P

Creating .tar.gz file

Several ways to create .tar.gz file

But, I use this way to create .tar.gz file ...

$ tar -pczf name_of_your_archive.tar.gz /path/to/directory

And untar, I use the popular way :D

$ tar -xvfz name_of_your_archive.tar.gz

:)

Reboot Linux Command

This is the command for reboot (restart) linux or unix.

# shutdown -r now

Reading and Writing from a URL

I didn't like to remember a lot of code in my memory ... so ... sometime I forget it T_T.

Then, I'd like to have some place that I can store my code like this :)

...

import java.net.*;
import java.io.*;

public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
}

ref: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html


...

If you'd like to write a file together, use this way ...

...


import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class URLReaderAndWriter {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

FileOutputStream fout = new FileOutputStream("test.txt");

String inputLine;

while ((inputLine = in.readLine()) != null) {
fout.write(inputLine.getBytes());
fout.write('\n'); // for new line if you want

}

fout.close();
in.close();
}
}

Setting java proxy

I had many problems with internet proxy. And I forgot every time when I want to create a file that access pass proxy.

This code is a method to set proxy:

private static void setProxy() {
Properties systemSettings = System.getProperties();
systemSettings.put("proxySet", "true");
systemSettings.put("http.proxyHost", "server_host");
systemSettings.put("http.proxyPort", "server_port");
}

And with the java command, you can use this to pass the proxy
$ java -DproxyHost="server_host" -DproxyPort="server_port" pkg.JavaClass



Have a fun