String filename = "path/file.txt";
File file = new File(filename);
FileInputStream fileinput = new FileInputStream(file.getAbsolutePath());
int x = fileinput.available();
byte b[] = new byte[x];
fileinput.read(b);
String string = new String(b);
System.out.println(string);
开始实习以后, 真的没什么自己的时间了. 学到了不少东西, 但还是老样子, 自己摸索自己成长. 长篇的理论知识整理确实重要, 但这些代码片段, 在需要的时候也是实实在在能帮上忙的东西.
闲的时间少了, 估计以后这类东西会比较多吧...
#include <X11/Xlib.h>
在绘制图形界面之前, 第一步要连接到一个Display. 就好比画画之前先要找到一张桌子或者画板, 你不能举着一张纸直接就往上泼墨...
// Open display
Display* display;
display = XOpenDisplay("");
if(!display){
printf("Cannot open display\n");
exit(-1);
}
之后可以通过一些函数, 获取当前Display的各种信息. 如需要, 可以保存到variable里供后边绘图时候使用.
int printScreenInfo(Display* display){
int screen_num; // 当前屏幕的编号
int screen_width; // 当前屏幕的宽度
int screen_height; // 当前屏幕的高度
Window root_window; // root窗口的编号
unsigned long white_pixel; // 白色像素的编号
unsigned long black_pixel; // 黑色像素的编号
screen_num = DefaultScreen(display);
screen_width = DisplayWidth(display, screen_num);
screen_height = DisplayHeight(display, screen_num);
root_window = RootWindow(display, screen_num);
white_pixel = WhitePixel(display, screen_num);
black_pixel = BlackPixel(display, screen_num);
printf("Screen Number: \t%d\n
Width: \t%d\n
Height: \t%d\n
Root: \t%d\n
White: \t%d\n
Black: \t%d\n",
screen_num, screen_width, screen_height,
root_window, white_pixel, black_pixel);
return 0;
}
画板摆好以后, 就要在上边铺上一张用来画画的纸, 也就是一个窗口...
// Define window properties
Window window;
int x, y;
unsigned int width, height, border_width;
width = 850;
height = 470;
x = y = 50;
border_width = 0;
// Define display constants
int screen_num = DefaultScreen(display);
unsigned long white_pixel = WhitePixel(display, screen_num);
unsigned long black_pixel = BlackPixel(display, screen_num);
// Create window
window = XCreateSimpleWindow(display, RootWindow(display, screen_num),
x, y, width, height, border_width,
black_pixel, white_pixel);
// Map win to display & Flush display
XMapWindow(display, win);
XFlush(display);
万事俱备只欠画画儿了`XLib的function都挺容易看懂的...
int drawInterface(Display* display, Window window, unsigned long black_pixel, unsigned long white_pixel ){
// Define GCs for drawing
GC foregc;
foregc = XCreateGC (display, window, 0, 0);
XSetBackground( display, foregc, black_pixel );
XSetForeground( display, foregc, white_pixel );
// Start Drawing
XClearWindow(display, window);
// Door
XDrawRectangle(display, window, foregc, 1, 1, 630, 460);
XFillRectangle(display, window, foregc, 66, 66, 480, 330);
// Dashboard
XDrawRectangle(display, window, foregc, 633, 1, 215, 460);
XDrawRectangle(display, window, foregc, 650, 45, 185, 330);
// Timer
XDrawRectangle(display, window, foregc, 660, 60, 160, 65);
// And More...
// Flush display to show everything changed
XFlush(display);
}
Event Handling的部分晚点再整理
这个脚本来自小夜博客, 经我略加修改了一下.
下载: backup_to_mail.sh
很简单的一段bash script, 只需要自定义下mysql的用户名密码, 邮箱地址和要备份的目录.
然后进行下边的步骤:
首先要建立好放置备份文件的目录
mkdir /home/backup
然后安装发送邮件的程序
yum install sendmail mutt
把backup_to_mail.sh放到刚刚建立的目录中, 然后给脚本增加运行权限
chmod a+x /home/backup/backup_to_mail.sh
最后一步是设置定时运行, 输入
crontab -e
然后在vi编辑界面中加入下面这行文字
00 00 * * * /home/backup/backup_to_mail.sh
:x + enter就ok了.
其中00 00表示在每日00分, 00点的时候运行该脚本.
下边是脚本的内容, 以备附件失效和以后学习...
#!/bin/bash
MYSQL_USER=root #mysql username
MYSQL_PASS=*********** #mysql password
MAIL_TO=*******@***.** #mailling to
WEB_DATA=/home/wwwroot/ #to be backup dir
#define variables
DataBakName=Data_$(date +%Y%m%d).tar.gz
WebBakName=Web_$(date +%Y%m%d).tar.gz
OldData=Data_$(date -d -3day +%Y%m%d).tar.gz
OldWeb=Web_$(date -d -3day +%Y%m%d).tar.gz
DataSubject="Backup: gregwym.info database "$(date +%Y%m%d)
WebSubject="Backup: gregwym.info web dir "$(date +%Y%m%d)
#delete data from 3days earlier
rm -rf /home/backup/$OldData /home/backup/$OldWeb
cd /home/backup
#export database to compressed gz files
for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs`; do
(/usr/local/mysql/bin/mysqldump --single-transaction -u$MYSQL_USER -p$MYSQL_PASS ${db} | gzip -9 - > ${db}.sql.gz)
done
#compress database files
tar zcf /home/backup/$DataBakName /home/backup/*.sql.gz
rm -rf /home/backup/*.sql.gz
#mail database
echo $DataSubject | mutt -a /home/backup/$DataBakName -s "${DataSubject}" $MAIL_TO
#compress backup dir
tar zcf /home/backup/$WebBakName $WEB_DATA
#mail dir
echo $WebSubject | mutt -a /home/backup/$WebBakName -s "${WebSubject}" $MAIL_TO
echo "bye"
#END
如果不希望备份网页文件, 或者希望数据库和网页分别备份的, 可以自行拆分脚本为两个文件.
如果只是想降低备份网页文件的频率, 可以将#mail dir部分替换为:
if [ $(date +%A) = "Sunday" ]; then
echo $WebSubject | mutt -a /home/backup/$WebBakName -s "${WebSubject}" $MAIL_TO
fi
]]>找一个vector里第k大的数一直是个有意思的课题, 这次又碰到了, 自己写了一个. 记下来记下来, 以后八成用得到.
网上给的思路很多, 大家普遍认为用quicksort的algorithm是效率最好的. 原本也不怎么在乎效率问题, 完全是被240逼的啊啊啊啊啊...
vector好像并不是高效率的选择, 不过随它去吧`这次的tree实在有够恶心
#include <vector>
int kTh(vector<int> & vals, int k){
if(k == 0){ // 如果k为0, 则取中点
k = vals.size();
k = k / 2 + k % 2;
}
vector<int> v, l ,r;
int i;
v = vals;
int c, n = 0, s = v.size(), p = v[0]; // pivot = the first
while(true){
for(i = s-1; i >= 0; i--){ // partite
c = v[i];
if(c <= p) l.push_back(c);
else if(c > p) r.push_back(c);
}
s = l.size();
if(s == k) break; // find the k'th
else if(s > k) v = l; // in left
else if(s < k) { // in right
k = k - l.size();
v = r;
}
l.clear();
r.clear();
s = v.size();
p = v[0]; // re-choose pivot
}
return p;
}
欢迎大家批评指正...= =`
更新更规范的写法, 请见 CS 240复习总结之三: Sorting and Randomized Algorithms
]]>