RSS
 

Archive for October, 2009

Java File IO

29 Oct

File object is not file

import java.io.*;

class MakeFile {
	public static void main(String[] args) {
		try {
			File directory = new File("d");
			File file = new File(directory, "f");

			// directory.mkdir();

		if(!file.exists()) {
			file.createNewFile();
		}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

原始的題目沒有line 9,
因此line 12執行的時候,
因為directory這個變數根本就沒有create,
就會在runtime丟出

java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at MakeFile.main(MakeFile.java:12)

File IO sample code:

import java.io.*;
import java.util.*;

public class CopyTextFile {

    public static void main(String args[]) {
        //... Get two file names from use.
        System.out.println(
				"Enter a filepath to copy from, and one to copy to.");
        Scanner in = new Scanner(System.in);

        //... Create File objects.
        File inFile  = new File(in.next());  // File to read from.
        File outFile = new File(in.next());  // File to write to

        //... Enclose in try..catch because of possible io exceptions.
        try {
            copyFile(inFile, outFile);

        } catch (IOException e) {
            System.err.println(e);
            System.exit(1);
        }
    }

    // Uses BufferedReader for file input.
    public static void copyFile(File fromFile, File toFile)
			throws IOException {
        BufferedReader reader =
			new BufferedReader(new FileReader(fromFile));
        BufferedWriter writer =
			new BufferedWriter(new FileWriter(toFile));

        //... Loop as long as there are input lines.
        String line = null;
        while ((line=reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();   // Write system dependent end of line.
        }

        //... Close reader and writer.
        reader.close();  // Close to unlock.
        writer.close();  // Close to unlock and flush to disk.
    }

    // Uses Scanner for file input.
    public static void copyFile2(File fromFile, File toFile)
			throws IOException {
        Scanner freader = new Scanner(fromFile);
        BufferedWriter writer =
			new BufferedWriter(new FileWriter(toFile));

        //... Loop as long as there are input lines.
        String line = null;
        while (freader.hasNextLine()) {
            line = freader.nextLine();
            writer.write(line);
            writer.newLine();   // Write system dependent end of line.
        }

        //... Close reader and writer.
        freader.close();  // Close to unlock.
        writer.close();  // Close to unlock and flush to disk.
    }
}
 
 

Debian

29 Oct

Chinese in Debian

VirtualBox Install Guest Additions on Debian

# 在root權限下
# 將系統更新到最新
apt-get update ;apt-get upgrade
# 安裝所需套件 build-essential & module-assistant
apt-get install build-essential module-assistant
# 初始化系統編譯核心之設定
m-a prepare
# 開啟Guest Additions所在資料夾(通常在 /media/cdrom 路徑不同請自行修改)
cd /media/cdrom
# 執行安裝(請選適合版本)
./VBoxLinuxAdditions-x86.run

Reference

 
 

Bash

27 Oct

History

  • ! – Starts a history substitution.
  • !! – Refers to the last command.
  • !n – Refers to the n-th command line.
  • !-n – Refers to the current command line minus n.
  • !string – Refers to the most recent command starting with string.
  • !?string? – Refers to the most recent command containing string (the ending ? is optional).
  • ?string1?string2 – Quick substitution. Repeats the last command, replacing string1 with string2.
  • !# – Refers to the entire command line typed so far.

Keyboard Shortcuts

Ctrl

  • Ctrl + a – Jump to the start of the line
  • ”Ctrl + b – Move back a char”
  • Ctrl + c – Sends the signal SIGINT to the current task, which aborts and close it.
  • Ctrl + d – Delete
  • Ctrl + e – Jump to the end of the line
  • ”Ctrl + f – Move forward a char”
  • Ctrl + g – Escape from history searching mode
  • Ctrl + h – Backspace
  • Ctrl + i – Tab
  • Ctrl + j – Linefeed
  • Ctrl + k – Cut to the end of line
  • Ctrl + l – Clear the screen
  • Ctrl + m – Enter
  • ”Ctrl + n – Fetch the next command from the history list”
  • Ctrl + o – Executes the found command from research.
  • ”Ctrl + p – Fetch the previous command from the history list”
  • Ctrl + q – XON
  • Ctrl + r – Search the history backwards
  • Ctrl + s – XOFF
  • Ctrl + t – Swaps the order that the last two character appear in
  • Ctrl + u – Cut to the beginning of line
  • Ctrl + v – Do not interpret the next thing
  • Ctrl + w – Cut the word before the cursor
  • Ctrl + xx – Move between EOL and current cursor position
  • Ctrl + y – Adds the clipboard content from the cursor position.
  • Ctrl + z – sends the signal SIGTSTP to the current task, which suspends it.

Alt

  • Alt + < – Move to the first line in the history
  • Alt + > – Move to the last line in the history
  • Alt + ? – Show current completion list
  • Alt + * – Insert all possible completions
  • Alt + / – Attempt to complete filename
  • Alt + . – Yank last argument to previous command
  • Alt + b – Move backward
  • Alt + c – Capitalize the word
  • Alt + d – Delete word
  • Alt + f – Move forward
  • Alt + l – Make word lowercase
  • Alt + n – Search the history forwards non-incremental
  • Alt + p – Search the history backwards non-incremental
  • Alt + r – Recall command
  • Alt + t – Move words around
  • Alt + u – Make word uppercase
  • Alt + back-space – Delete backward from cursor

Tab

  • Here “2T” means Press TAB twice
  • 2T – All available commands(common)
  • (string)2T – All available commands starting with (string)
  • /2T – Entire directory structure including Hidden one
  • 2T – Only Sub Dirs inside including Hidden one
  • *2T – Only Sub Dirs inside without Hidden one
  • ~2T – All Present Users on system from “/etc/passwd”
  • $2T – All Sys variables
  • @2T – Entries from “/etc/hosts”

Reference

Tips

Reload .bashrc

source ~/.bashrc
 
 

C Integer Type

23 Oct
#include <stdio.h>
#include <limits.h>

volatile int char_min = CHAR_MIN;

int main(void)
{
	// Only in C99 compilers
	printf("Size of Boolean type is %d byte(s)\n\n", (int)sizeof(_Bool));

	printf("Number of bits in a character: %d\n", CHAR_BIT);
	printf("Size of character types is %d byte\n", (int)sizeof(char));
	printf("Signed char min: %d max: %d\n", SCHAR_MIN, SCHAR_MAX);
	printf("Unsigned char min: 0 max: %u\n", (unsigned int)UCHAR_MAX);

	printf("Default char is ");
	if (char_min < 0)
		printf("signed\n\n");
	else if (char_min == 0)
		printf("unsigned\n\n");
	else
		printf("non-standard\n\n");

	printf("Size of short int types is %d bytes\n", (int)sizeof(short));
	printf("Signed short min: %d max: %d\n", SHRT_MIN, SHRT_MAX);
	printf("Unsigned short min: 0 max: %u\n\n", (unsigned int)USHRT_MAX);

	printf("Size of int types is %d bytes\n", (int)sizeof(int));
	printf("Signed int min: %d max: %d\n", INT_MIN, INT_MAX);
	printf("Unsigned int min: 0 max: %u\n\n", (unsigned int)UINT_MAX);

	printf("Size of long int types is %d bytes\n", (int)sizeof(long));
	printf("Signed long min: %ld max: %ld\n", LONG_MIN, LONG_MAX);
	printf("Unsigned long min: 0 max: %lu\n\n", ULONG_MAX);

	// Only in C99 compilers
	printf("Size of long long types is %d bytes\n",
			(int)sizeof(long long));
	printf("Signed long long min: %lld max: %lld\n",
			LLONG_MIN, LLONG_MAX);
	printf("Unsigned long long min: 0 max: %llu\n", ULLONG_MAX);

	return 0;
}
 
 

我不是山寨版!- 我最愛的喜劇影集The Office(US) part II

19 Oct

前情提要

上一篇:辦公室”瘋”雲! – 我最愛的喜劇影集The Office(US)

我們提到The Office(因為以後都是談美國版,所以將來就不標明是哪個國家的版本了)

在美國播出之後獲得了很大的成功。

但是,其中的過程並不是一帆風順的…

繼續閱讀

 
No Comments

Posted in Drama

 

辦公室”瘋”雲! – 我最愛的喜劇影集The Office(US)

19 Oct

因為這是我最愛的影集,

只用一篇可能介紹不完,

所以只好多寫幾篇了:)

故事要從2001年的英國BBC開始說起…

繼續閱讀

 
No Comments

Posted in Drama

 

只剩三天的時間了! – 我聽Three Days Grace。

17 Oct

“If you had three days to change something in your life, could you do it?”

如果給你三天的時間,改變你生命裡的一件事,你會做嗎?

來自加拿大的樂團,Three Days Grace命名的創意,就是來自這個問題。

繼續閱讀

 
No Comments

Posted in Rock

 

Apache Ant

13 Oct

Bookmarks

Example of a build.xml

<code Xml>
<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
</code>

Notes

Java task redirect output (to avoid ant’s prefix)

<java class="project.entry" output="dev/tty"/>

Note: only on unix-like systems.
Note: you can type “tty” or “w” for the name of the terminal you are using. (http://www.cyberciti.biz/faq/linux-unix-find-tty-name/)

Postpone decisions

<target id="XXX" if="OOO" />
<!-- this target will run if OOO is set -->
<target id="XXX" unless="OOO" />
<!-- this target will not run if OOO is set -->

Flow of control

Reference: click here

 
 

Algorithm

12 Oct

Sorting Algorithm

 
 

Anti-pattern Catagory

07 Oct
Organizational anti-patterns
Analysis paralysis
After exceedingly long phases of project planning, requirements gathering, program design and data modeling, with little or no extra value created by those steps.
Project management anti-patterns
Death March
Everyone knows that the project is going to be a disaster – except the CEO. However, the truth remains hidden and the project is artificially kept alive until the Day Zero finally comes (“Big Bang”).
Software design anti-patterns
Abstraction inversion
Object-oriented design anti-patterns
Call super
Programming anti-patterns
Accidental complexity
Action at a distance
Unexpected interaction between widely separated parts of a system
Methodological anti-patterns
Copy and paste programming
Golden hammer
Assuming the favorite solution is universal applicable
Configuration management anti-patterns
Dependency hell
Problems with versions of required products
Other anti-patterns
Garbage In, Garbage Out
Java error stacktrack dump

Organizational anti-patterns

Analysis paralysis

After exceedingly long phases of project planning, requirements gathering, program design and data modeling, with little or no extra value created by those steps.

Project management anti-patterns

Death March

Everyone knows that the project is going to be a disaster – except the CEO. However, the truth remains hidden and the project is artificially kept alive until the Day Zero finally comes (“Big Bang”).

Software design anti-patterns

Abstraction inversion

Object-oriented design anti-patterns

Call super

Programming anti-patterns

Accidental complexity

Action at a distance

Unexpected interaction between widely separated parts of a system

Methodological anti-patterns

Copy and paste programming

Golden hammer

Assuming the favorite solution is universal applicable

Configuration management anti-patterns

Dependency hell

Problems with versions of required products

Other anti-patterns

Garbage In, Garbage Out

Java error stacktrack dump