Category Archives: Chỉ dẫn lập trình

Try catch finally (Java)

try {
    int i = 1/0;
} catch (ArithmeticException ae) {
    System.out.println(“Divide by zero”);
} catch (Exception e) {
    System.out.println(“Exception”);
} finally {
    System.out.println(“Done”);
}

Ouput:

Divide by zero
Done

Explain:
– Gặp ngoại lệ trong try thì tìm đến catch, ngoại lệ hợp với catch nào thì làm theo khối lệnh trong catch đó và bỏ qua các catch còn lại khác.
– Khối lệnh trong finally luôn được thực hiện. (Nếu không có catch mà gặp ngoại lệ thì chỉ lệnh đầu tiên trong khối finally được thực hiện, sau đó java sẽ báo lỗi ngoại lệ!)

VBScript: Split file by line

Problem: You have a big file with many many line. For doing your task, you have to split it to small files. Each file contains about M lines.

Solution: Create new file with extension is VBS (example split.vbs). Fill to it following code
‘Language: VBScript
‘Operating System: Windows XP, Windwos Vista
‘Author: Phung Van Huy (Y!M: huyphungvan – GoogleTalk: huyphungvan)
‘Website: http://huypv.net/blog/
‘##################################################################

Const M = 1000
Set objFSO = CreateObject(“Scripting.Filesystemobject”)
Set objTS = objFSO.OpenTextFile(“big.txt“)
intFile = 1
intLine = 1

While Not objTs.AtEndOfStream
    strLine = objTS.ReadLine
    If intLine Mod M = 1 Then
        If IsObject(objTS2) Then objTS2.Close
        Set objTS2 = objFSO.OpenTextFile(“small/” & intFile & “.txt“, 8, 1)
        intFile = intFile + 1
    End If
  
    If intLine Mod M = 0 Then
        intLine = 0
        objTS2.Write strLine
    Else
        objTS2.WriteLine strLine
    End If
    intLine = intLine + 1
Wend

objTS2.Close
Set objTs = Nothing : Set objTS2 = Nothing : Set objFSO = Nothing
Msgbox “Split complete”, 0, “PVH”

Save!
Now, create new directory small if it doesn’t exist
Double click to split.vbs you have just create above. (If not work, you can run: cmd – cscript split.vs)

Example Using Output Buffer in PHP

Create 2 PHP files to compare difference. I like it!
– simple.php
<?php
    set_time_limit(0);
    for ($i = 0; $i < 100000; $i++) {
        echo “$i. This is line $i <br>”;
        sleep(2);
    }
?>
– buffer.php
<?php
    set_time_limit(0);
    for ($i = 0; $i < 100000; $i++) {
        ob_flush();
        echo “$i. This is line $i <br>”;
        sleep(2);
    }
?>

Encoding UTF8 – BOM

Using some text editors to edit text file, you will see Add Unicode Signature (BOM) checkbox when open Save (As) File Dialogue with encoding UTF-8. So, what happend if you check (uncheck) this box?

The difference between 2 types!
I am using XVI32 to open 2 files, one is saved with Encoding UTF-8 and add BOM, other is saved without BOM.
The differences are 3 first bytes! UTF-8 BOM has EF BB BF chr(237) chr(187) chr(191)

Following is delBOM.c. (Dev-cpp)
#include <windows.h>
#include <stdio.h>

int main(int argn, char * argv[]) {
    FILE *fp;
   
    if (argn < 2) {
        MessageBox(0, “Where is second argument?”, “PVH”, 0);
        return 0;
    }
    int iFile;
for (iFile = 1; iFile < argn; iFile++) {
    fp = fopen(argv[iFile], “r”);
    if (!fp) {
        MessageBox(0, “Read file failed”, “PVH”, 0);
        continue;
    }
   
    char szString[65535];
    int nRead = fread(szString, sizeof(char), 65535, fp);
    //printf(“%d “, nRead);
    int iStart = 0;
    int iFinish = nRead;
    if(szString[0] == -17 && szString[1]==-69 && szString[2]==-65) {
        printf(“BOM will be removed”);
        iStart = 3;
    }
    rewind(fp);
    fclose(fp);
    if (iStart == 0) continue;
    fp = fopen(argv[iFile], “w+”); int i;
    for (i = iStart; i < iFinish; i++) {
        fputc(szString[i], fp);
    }
    fclose(fp);
}
    return 1;
}

Source code Hello World

Language C (helloworld.c)
int main(int argn, char *argv) {
    print(“Hello world”);
}

Language C++ (helloworld.cpp) p stands for plus
#include <iostream>
using namespace std;
int main(int argn, char * argv) {
    cout << “Hello world”;
}

Language PHP (helloworld.php)
<?php
    echo “Hello world”;
?>

Language Java (HelloWorld.java)
public class HelloWorld {
    public static void main(String[] args) {
        System.out.print(“Hello world”);
    }
}