03 Dec 2013
public class IODemo {
public static void main(String[] args) {
try {
// 1a.Reading input by lines
BufferedReader in = new BufferedReader(new FileReader(args[0]));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "\n";
}
in.close();
// 1b.Reading standard input
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter a line: ");
System.out.println(stdin.readLine());
// 2.Input from memory
StringReader in2 = new StringReader(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.print((char) c);
}
// 3. Formatted memory input
try {
DataInputStream in3 = new DataInputStream(
new StringBufferInputStream(s2));
while (true) {
System.out.print((char) in3.readByte());
}
} catch (EOFException e) {
System.out.println("End of stream");
}
// 4. Line number reading & file output
try {
LineNumberReader li = new LineNumberReader(new StringReader(s2));
BufferedReader in4 = new BufferedReader(li);
PrintWriter out1 = new PrintWriter(new BufferedWriter(
new FileWriter("IODemo.out")));
while ((s = in4.readLine()) != null) {
out1.println("Line " + li.getLineNumber() + " " + s);
}
out1.close();
} catch (EOFException e) {
System.out.println("End of stream");
}
// 5. Storing & recovering data
try {
DataOutputStream out2 = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(
"Data.txt")));
out2.writeDouble(3.1415926);
out2.writeBytes("It is a pi");
out2.close();
DataInputStream in5 = new DataInputStream(
new BufferedInputStream(new FileInputStream("Data.txt")));
BufferedReader in5br = new BufferedReader(
new InputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
} catch (EOFException e) {
System.out.println("End of stream");
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found: " + args[1]);
} catch (IOException e) {
System.out.println("IO Exception");
}
}
}
01 Dec 2013
在使用kpw阅读书籍时,如果碰到书籍中有错误,会出现死机、白屏现象。这种情况没有必要进行刷机这么复杂的处理。正确的处理方式如下:
长按电源键20秒以上,然后等待系统自动重启。重启成功后,删除出现问题的书籍即可。
20 Nov 2013
在《Java编程思想-网络版》一书中看到一段有意思的代码。
先来看一段Java实现C/C++枚举效果的非类型安全的版本:
public interface Months {
int JANUARY = 1,
FEBRUARY = 2,
MARCH = 3,
APRIL = 4,
... ...;
}
上面的代码实际上是在利用Java中interface的成员变量会自动成为public static final的事实,这样我们可以使用Months.JANUARY
的方法来引用这些值。但是这个方法的缺陷是非类型安全的,因为我们最终获得的其实是个int。这种方法带来的好处只是减少了硬编码。
接下来看看Bruce Eckel
提供的类型安全的版本:
public final class Month2 {
private String name;
private Month2(String nm) {
name = nm;
}
public String toString() {
return name;
}
public final static Month2
JAN = new Month2("January"),
FEB = new Month2("February"),
MAR = new Month2("March"),
APR = new Month2("April"),
MAY = new Month2("May"),
JUN = new Month2("June"),
JUL = new Month2("July"),
AUG = new Month2("August"),
SEP = new Month2("September"),
OCT = new Month2("October"),
NOV = new Month2("November"),
DEC = new Month2("December");
public final static Month2[] month = {
//这里使用2个JAN是为了填充0号位置,这样数组下标可以跟月份一一对应
JAN, JAN, FEB, MAR, APR, MAY,
JUN, JUL, AUG, SEP, OCT, NOV,
DEC
};
public static void main(String[] args) {
Month2 m = Month2.JAN;
System.out.println(m);
m = Month2.month[12];
System.out.println(m);
System.out.println(m == Month2.DEC);
System.out.println(m.equals(Month2.DEC));
}
}
ref: 《Java编程思想-网络版》
14 Nov 2013
在C++项目中使用ffmpeg时报错:
$ /usr/local/include/libavutil/common.h:170:47: 错误: ‘UINT64_C’在此作用域中尚未声明
解决方法:
extern "C"{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
# include <stdint.h>
#endif
}
13 Nov 2013
安装git服务器
github实在是个好东西,奈何不花钱就不能创建私用仓库,只能在自己的vps上搭建git服务器了。
所有在git服务器上的操作默认都是root用户
安装必备的软件
$ apt-get install git-core openssh-server openssh-client
增加git用户
$ useradd -m git -s /bin/bash
$ passwd git
建立git仓库的保存位置
$ mkdir /home/git/repo
$ chown git:git /home/git/repo/
$ chmod 700 /home/git/repo/
安装和配置gitosis
$ apt-get install python-setuptools
$ cd /tmp #把gitosis的源代码clone到/tmp目录下
$ git clone git://github.com/res0nat0r/gitosis.git
$ python setup.py install #安装gitosis
初始化gitosis
以下步骤在git管理员的机器上操作:
$ ssh-keygen -t rsa #生成ssh-key
$ scp ~/.ssh/id_rsa.pub [email protected]:/tmp #上传上一步中生成的key
回到服务器上操作:
$ chmod a+r /tmp/id_rsa.pub
$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub #初始化gitosis
如果上面的步骤都准确,shell会输出如下:
Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/
使用gitosis管理git仓库
为了能使用gitosis管理项目,需要先在服务器上更改一下权限:
$ chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
以下步骤在git管理员的机器上操作:
$ cd /tmp
$ sudo git clone [email protected]:/home/git/repositories/gitosis-admin.git
在 gitosis-admin/keydir目录下可以添加所有用户的key,其中的gitosis.conf可以配置各项目和用户的权限。
测试
在git服务器上新建一个test代码库
$ su - git #切换到git用户
$ cd repositories/gitosis-admin.git/
$ mkdir test.git
$ cd test.git
$ git init --bare #创建一个裸仓库,裸仓库是不能被clone的,必须要有用户提交一个初始版本后才能被clone
以下步骤在git管理员的机器上操作:
$ cd /tmp/gitosis-admin.git/
在gitosis.conf中添加test项目配置,只有有相应权限的用户才能操作对应的仓库:
[group test]
members = [email protected]
writable = test
更改后需要提交到git服务器:
$ sudo git add .
$ sudo git commit -am "add test project"
$ sudo git push
提交一个新项目
以下步骤在git用户的机器上操作:
$ #随意创建一个项目
$ mkdir /tmp/test
$ git init
$ cd /tmp/test
$ echo "test" > testfile
$ git add .
$ git commit -am "test"
$ git remote add origin [email protected]:~/repositories/test.git #提交到上面创建的test.git仓库
$ git push origin master #现在test.git仓库中就有个一个版本,其他有权限的用户可以clone该项目了
clone项目
以下步骤在git用户的机器上操作:
$ git clone [email protected]:/home/git/repositories/test.git