当我意识到我所能接触到的编程都是简单而繁重的工作时,我的技术性博客就从这最基础的写起。仅此纪念我所剩不多的青春与此下百无聊赖把代码敲烂的日子。语言本是单纯描述没有思想的东西,把它注入思想,你才会有价值。当我可以给予你生命的时候,你才会永远属于我。
关于流操作:
1.读取文件:
在指定目录下创建文件,路径传给File文件类,以获取文件的字节长度,存储在byte类型的缓存区中,通过创建FileInputStream类的实例,获得与file文件的连接,从而读出文件在buf缓冲区的内容,操作结束后关闭连接。
public static void test01() throws Exception{
String path="c:\\veg.txt";
File file=new File(path);
int len=(int) file.length();
byte buf[]=new byte[len];
FileInputStream fis=new FileInputStream(file);
fis.read(buf);
fis.close();
System.out.println(new String(buf,0,len));
}
2.读写文件
创建FileOutputStream,达到从程序向文件中写入内容的目的,但还需要DateOutputStream 配合使用,才能使应用程序以适当的方式将基本Java数据类型写入。FileOutputStream 执行完,创建tt.tt文件。
public static void test02() throws Exception{
String path="c:\\tt.tt";
FileOutputStream fos=new FileOutputStream(path);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeInt(45);
dos.writeUTF("hello");
dos.close();
FileInputStream fis=new FileInputStream(path);
DataInputStream dis=new DataInputStream(fis);
int x=dis.readInt();
String s=dis.readUTF();
System.out.println(x+","+s);
dis.close();
}
当想要保存以前执行的内容时,在程序中添加追加,程序改为如下:
public static void test02() throws Exception{
String path="c:\\tt.tt";
boolean append=true;
FileOutputStream fos=new FileOutputStream(path,append);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeInt(45);
dos.writeUTF("Hello world");
dos.close();
FileInputStream fis=new FileInputStream(path);
DataInputStream dis=new DataInputStream(fis);
while(true){
try{
int x=dis.readInt();
String s=dis.readUTF();
System.out.println(x+","+s);
}catch(EOFException e){
break;
}
}
dis.close();
}
关于GUI界面与监听:
1.一个简单的GUI界面,监听注册的例子:
界面上有一个JButton和JLabel,每点击JButton一次,JLabel上的数字就加一。鼠标移动划线。其中Listener必须重写所有方法而Adapter可重写其中一种或几种方法。
Graphics是一个抽象类,所以应用程序不能直接调用此构造方法。图形上下文从其他图形上下文获取,或者通过在组件上调用 getGraphics 来创建。
public class EventTest extends JFrame {
private JButton jButton;
private JLabel jLabel;
public EventTest(){
initGUI();
this.setVisible(true);
register();
}
public void initGUI(){
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jButton=new JButton("Click here to test");
this.getContentPane().add(jButton,BorderLayout.NORTH);
}
{
jLabel=new JLabel("Ready");
this.getContentPane().add(jLabel,BorderLayout.SOUTH);
jLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
this.setSize(400, 500);
this.setLocationRelativeTo(null);
this.setTitle("The Event handling");
}
public void register(){
this.jButton.addActionListener(alButton);
this.addMouseMotionListener(mml);
this.addMouseListener(ml);
}
transient ActionListener alButton=new ActionListener(){
private int count;
@Override
public void actionPerformed(ActionEvent e) {
count++;
jLabel.setText("count:"+count);
}};
transient MouseMotionListener mml=new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
int x=e.getX();
int y=e.getY();
Graphics g=EventTest.this.getGraphics();
g.drawLine(x, y, x, y);
}
public void mouseDragged(MouseEvent e){
int x=e.getX();
int y=e.getY();
Graphics g=EventTest.this.getGraphics();
g.setColor(Color.red);
g.drawLine(x, y, x, y);
}};
transient MouseListener ml=new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int x=e.getX();
int y=e.getY();
if(e.getButton()==e.BUTTON1){
Graphics g=getGraphics();
g.drawString(x+","+y, x, y);
}
else if(e.getButton()==e.BUTTON3){
initMenu();
showMenu(x,y);
}
}};
public void initMenu(){
if(this.jPupopMenu==null){
jPupopMenu=new JPopupMenu();
jMenuItemExit=new JMenuItem("exit");
this.jMenuItemExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
EventTest.this.setVisible(false);
System.exit(5);
}
});
this.jPupopMenu.add(this.jMenuItemExit);
}
}
public void showMenu(int x,int y){
this.jPupopMenu.show(this,x,y);
}
private JPopupMenu jPupopMenu;
private JMenuItem jMenuItemExit;
public static void main(String [] args){
EventTest e=new EventTest();
}
}
关于this的用法:
没什么好说的。。。
public class ThisTest {
private int x;
ThisTest(int x){
this.x=x;
}
public ThisTest getSelf(){
return this;
}
public static void main(String [] args){
ThisTest tt1=new ThisTest(5);
tt1.getSelf();
System.out.println(tt1);
ThisTest tt2=new ThisTest(6);
tt2.getSelf();
System.out.println(tt2);
}
}
关于集合量:
用Iterator 遍历集合量。
public class SetTest {
public static void test01(){
Set set=new HashSet();
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add(null);
System.out.println(set);
Iterator it=set.iterator();
while(it.hasNext()){
Object obj=it.next();
System.out.println(obj);
}
}
public static void main(String [] args){
test01();
}
}
关于线程:
1.线程最简单的例子:
public class ThreadTest{
public static void test01(){
ThreadTest tt=new ThreadTest();
Thread th=new Thread(tt.r);
Thread.currentThread().setPriority(3);
th.setPriority(6);
th.start();
for(int i=0;i<800;i++){
if(i==799){
th.interrupt();
}
System.out.println(i+"-");
}
}
public static void main(String [] args){
test01();
}
Runnable r=new Runnable(){
@Override
public void run() {
for(int i=0;i<800;i++){
System.out.println(i);
if(i==20){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
//e.printStackTrace();
System.out.println("Rabbit is interrupted");
}
}
}
}};
}
2.共用帐号问题
public class Person extends Thread{
private String name;
private Account acc;
public Person(String name,Account acc){
this.name=name;
this.acc=acc;
this.setName(name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Account acc=new Account();
Person li=new Person("li",acc);
Person lin=new Person("lin",acc);
li.start();
lin.start();
}
//线程实现run方法
public void run(){
acc.deposit(300);
acc.withdraw(300);
}
}
class Account{
private float balance;
public void deposit(float x){
synchronized(this){
for(int i=0;i<x;i++){
balance++;
System.out.println(Thread.currentThread().getName()+":"+this.balance);
}}
}
public synchronized void withdraw(float x){
for(int i=0;i<x;i++){
balance--;
System.out.println(Thread.currentThread().getName()+":"+balance);
}
}
}
3.生产者消费者:
传说,操作系统中的经典问题吧。。。实在是太讨厌OS老师了。
public class Producer extends Thread{
private int count;
private boolean finished=false;
public static void main(String [] args){
Producer pro=new Producer();
Consumer con=new Consumer(pro);
pro.start();
con.start();
}
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void run(){
while(true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
System.out.println("Bread produced:"+count);
finished=true;
synchronized(this){
this.notify();
}
}
}
}
class Consumer extends Thread{
private Producer pro;
public Consumer(Producer pro){
this.pro=pro;
}
public void run(){
while(true){
if(!pro.isFinished()){
synchronized(pro){
try {
pro.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("Consumer is eating bread "+pro.getCount());
pro.setFinished(false);
}
}
下面换一种配对方式,双向通知:
public class Producer extends Thread{
private int count;
private Consumer con;
private boolean finished=false;
public static void main(String [] args){
Producer pro=new Producer();
Consumer con=new Consumer(pro);
pro.start();
con.start();
}
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void addConsumer(Consumer con){
this.con=con;
}
public void run(){
while(true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
System.out.println("Bread produced:"+count);
finished=true;
synchronized(this){
this.notify();
}
synchronized(con){
try {
con.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class Consumer extends Thread{
private Producer pro;
public Consumer(Producer pro){
this.pro=pro;
this.pro.addConsumer(this);
}
public void run(){
while(true){
if(!pro.isFinished()){
synchronized(pro){
try {
pro.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("Consumer is eating bread "+pro.getCount());
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pro.setFinished(false);
synchronized(this){
this.notify();
}
}
}
}