Javaphua Blog

BlogJava 首页 新随笔 联系 聚合 管理
  46 Posts :: 5 Stories :: 46 Comments :: 0 Trackbacks

   
//格式化日期为指定的格式
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

format.format(rs.getTimestamp("birth"))

//将字串符转换成日期类型
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 java.util.Date date = format.parse("1981-10-25 10:20:25");

//在数据库中将字符串转成日期存入数据库
birth=to_date(?,'YYYY-MM-DD HH24:MI:SS')

//发送SQL语句,执行存储过程
java.sql.CallableStatement cs = con.prepareCall("{call addAge}")

//更新时使用数据库的系统时间(sysdate)
"update student set birth=sysdate"

//IO的几种常用通信方法---------------采用对象方式进行传输数据-------------------------
java.io.BufferedReader in = new java.io.BufferedReader(
  new java.io.InputStreamReader(socket.getInputStream()//System.in));


java.io.PrintWriter out = new java.io.PrintWriter(
 new java.io.OutputStreamWriter(new java.io.BufferedOutputStream(
 socket.getOutputStream())));

java.io.ObjectOutputStream out  = new java.io.ObjectOutputStream(
  new java.io.BufferedOutputStream(new java.io.FileOutputStream("t.dat")));
out.writeObject(s);
  out.flush();
  out.close();
java.io.ObjectInputStream in = new java.io.ObjectInputStream(
  new java.io.BufferedInputStream(new java.io.FileInputStream("t.dat")));
 Student ss = (Student)in.readObject();

--------------------------------------------------------------
//给按钮添加事件 按钮触发数字自动滚动

this.button.addActionListener(
  new java.awt.event.ActionListener(){
     
  public void actionPerformed(java.awt.event.ActionEvent e){
   new Thread(){
        
    public void run(){
       while(true){
       String s=txt.getText();
       char c=s.charAt(s.length()-1);
       String cs=s.substring(0,s.length()-1);
       txt.setText(c+cs);
       try{
       Thread.sleep(1000);
       }catch(Exception y){}
      } 
    }
   }.start();
  }
 }
);

----------------------------------------

另一种添加事件的方法
button1.addActionListener( new CalculationListener());

//在剪切时候,它在一个内部类
class CalculationListener implements java.awt.event.ActionListener{
 public void actionPerformed(java.awt.event.ActionEvent e){
  javax.swing.JButton button  = (javax.swing.JButton)e.getSource();
   calc.calculate(button.getText());
  }
}

-----------------------------------------
启动线程的几种方法..
----------------------------

  R1 r = new R1();
  R1 w = new R1();
  Thread t = new Thread(r);
  Thread q = new Thread(w);
  t.start();
  q.start();

  new Thread(new W(socket)).start();

class R1 implements Runnable{
 public void run(){
  
 }
}
-----------------------------------
Thread1 t1 =new Thread1();
t1.start();

class Thread1 extends Thread{
 public void run(){
  
 }
}

命名线程://Thread t2 = new Thread(new Thread3(10),"线程名")
得到线程序号:System.out.println (t2.getPriority());


 applet框架
-----------------------------
public class TestApplet extends javax.swing.JApplet{
 public  void init(){
  super.init();
 }

}

//设置窗口关闭时自动退出
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

------------------------------------------在lesson中有一个鼠标事件还没有看通,有点复杂(day16)

//给按钮添加事件的一种方法
   //按钮调用
 button.addActionListener(new count2());
 //事件类继承监听器
 class count2 implements java.awt.event.ActionListener{
   public void actionPerformed(java.awt.event.ActionEvent e){
    counter(); 
   }
  
 }
 //方法与其分开
 public void counter(){
  String s = jt1.getText();
  String m = jt2.getText();
  int n = Integer.parseInt(m);
  int u = Integer.parseInt(s);
  int w = n+u;
 
  jt3.setText(""+w);
 }


//为了代码与窗体进行分开合作工作,因此将方法嵌入窗体中(“你中用我 我中有你”)
如:LotteryFunction lf = new LotteryFunction(this);

LotteryFunction 为功能类

将this作为工功能类构造的参数可很方便的在窗体显示计算结果

------------------------------------------------------------------------------

如面板上有多个控件时,可用以下方法将其加入面板中
class MyPanel extends javax.swing.JPanel{
   javax.swing.JTextField[] texts = new javax.swing.JTextField[7];
   
   MyPanel(){
    this.setLayout(new java.awt.GridLayout(1,7,3,3));
    
    for(int i=0;i<texts.length;i++){
     texts[i] = new javax.swing.JTextField();
     texts[i].setBackground(java.awt.Color.green);
     this.add(texts[i]);
    }
   }
}

------------------------------------------------------------
排序的代码:

static int t;
int[] r ={8,9,7,6,5,4,3};
for(int i=0;i<r.length;i++){
 for(int j=1;j<r.length-i;j++){
  if(r[j]<r[j-1]){
    t=r[j];
    r[j]=r[j-1];
    r[j-1]=t; 
  }
 }
}

--------------------------

如何给文件进行锁定


public static void lock()throws Exception{
    java.io.FileOutputStream out = new java.io.FileOutputStream("c:\\TestIO.txt",true);
    java.nio.channels.FileChannel f = out.getChannel();
    
    java.nio.channels.FileLock lock = f.tryLock();
    
    if (lock!=null){
     System.out.println ("正在锁定");
     Thread.sleep(100000);
     System.out.println ("解除完毕");
     lock.release(); 
    }
 }

------------------------------
java.nio 如何进行文件传输的

java.io.FileInputStream in  = new java.io.FileInputStream("TestNIO.java");
   java.nio.channels.FileChannel ch = in.getChannel();
   java.nio.ByteBuffer buff  = java.nio.ByteBuffer.allocate(1024);
   while((ch.read(buff))!=-1){
    buff.flip();
    java.nio.charset.Charset ca = java.nio.charset.Charset.forName("gb2312");
     //对缓存进行编码设置后,再进行解析
    java.nio.CharBuffer cb = ca.decode(buff);
    System.out.println (cb);
    buff.clear();
  }

---------------文件传输
public static void copyfile()throws Exception{
    java.io.FileInputStream in  = new java.io.FileInputStream("TestNIO.java");
    java.nio.channels.FileChannel ch = in.getChannel();
    
    java.io.FileOutputStream out = new java.io.FileOutputStream("c:\\TestIO.java");
    java.nio.channels.FileChannel outch = out.getChannel();
    
    java.nio.ByteBuffer buff = java.nio.ByteBuffer.allocate(32);
    
    while((ch.read(buff))!=-1){
     buff.flip();
     outch.write(buff);
     buff.clear();
     
    }
    in.close();
    out.close();
    
  }
-------------------------------???关于java.nio.buff中的一些缓冲区的管理代码还没有看懂??


几种IO通信方法
1---------
java.io.BufferedInputStream in =new java.io.BufferedInputStream(
         new java.io.FileInputStream("TestIO.java"));
   
java.io.BufferedOutputStream out =new java.io.BufferedOutputStream(
         new java.io.FileOutputStream("c:\\t.txt"));

byte[] b =new byte[32];
  
  int len= 0 ;
  while((len=in.read(b))!=-1){
   out.write(b,0,len);
     
  }
  out.flush();
  in.close();
  out.close();

2-----------

java.io.FileReader f =new java.io.FileReader("c:\\t.txt");
  char[] c = new char[32];
  int len=0;
  while((len=f.read(c))!=-1){
   System.out.println (new String(c,0,len));
  }
  
 f.close();

3------------------
java.io.BufferedReader r= new java.io.BufferedReader(new java.io.FileReader("TestIO.java"));
 
java.io.PrintWriter w= new java.io.PrintWriter(new java.io.BufferedWriter(new java.io.FileWriter("c:\\t.txt")));
 
 
 String line=null;
 
 while((line =r.readLine())!=null){
  w.println(line);
  
 }
  w.flush();
  r.close();
  w.close();

4-----------------------
java.io.PrintWriter p = new java.io.PrintWriter(new java.io.BufferedWriter(
         //是否在文件后进行追加数据(true)
     new java.io.FileWriter("c:\\t.txt",true)));
  p.print("12");
  p.flush();

5------------------------
java.io.FileOutputStream out  = new java.io.FileOutputStream("c:\\t.txt");
   
   java.io.OutputStreamWriter write = new java.io.OutputStreamWriter(out,"UTF-8");
   
   write.write("2222222222");
   
   write.close();
5---------------------------从控制台读取数据
java.io.BufferedReader r  = new java.io.BufferedReader(
      new java.io.InputStreamReader(System.in));
      
  String s=r.readLine();
  System.out.println (s);

6-------------------------------向文件写入数字
int i = 0;
  java.io.DataOutputStream out  =new java.io.DataOutputStream(
      new java.io.BufferedOutputStream(new java.io.FileOutputStream("c:\\t.txt"))); 
  
  out.writeInt(1);
  out.writeChar('a');
  out.writeInt(2);
  out.writeChar('b');
  //out.writeInt(4);
  out.flush();
  out.close();
  
  java.io.DataInputStream in =new java.io.DataInputStream(
      new java.io.BufferedInputStream(new java.io.FileInputStream("c:\\t.txt")));
  while(i!=-1){
   System.out.println (i=in.readInt());
   //System.out.println (in.readDouble());
   System.out.println (in.readChar()); 
  } 

7---------------------------------未看懂的代码????????????
java.io.RandomAccessFile r = new java.io.RandomAccessFile("c:\\t.txt","r");
   
   System.out.println (r.readInt());
   r.seek(0);
   System.out.println (r.readInt());
   r.seek(r.length()-8);
   System.out.println (r.readDouble());
   r.seek(r.length()-11);
   System.out.println (r.readUTF());  

//压缩解压文件的代码--还未看

 public static void ZipFile()throws Exception{
   java.io.BufferedInputStream b = new java.io.BufferedInputStream(
    new java.io.FileInputStream("c:\\t.txt"));
    
   java.util.zip.ZipOutputStream z = new java.util.zip.ZipOutputStream(
    new java.io.BufferedOutputStream(new java.io.BufferedOutputStream(
     new java.io.FileOutputStream("c:\\t.zip"))));
     
   z.putNextEntry(new java.util.zip.ZipEntry("test.txt"));
   
   byte[] t = new byte[32];
   int len = 0;
   while((len=b.read(t))!=-1){
    z.write(t,0,len);//z.write(t);
   }
   
   z.flush();
   b.close();
   z.close();
 } 

----------
public static void unzip()throws Exception{
   java.util.zip.ZipFile  zip = new java.util.zip.ZipFile("c:\\t.zip");
   
   java.util.Enumeration enu = zip.entries();
   
   while(enu.hasMoreElements()){
     java.util.zip.ZipEntry entry = (java.util.zip.ZipEntry)enu.nextElement();
     java.io.InputStream in = zip.getInputStream(entry);
     
     java.io.BufferedOutputStream out = new java.io.BufferedOutputStream(
       new java.io.FileOutputStream("c:\\test.txt"/*+entry.getName()*/));
       
     byte[] b = new byte[32];
     int len =0;
     while((len=in.read(b))!=-1){
       out.write(b,0,len);
     }
   }
  }

------------------从控制台向文件输入数据

java.io.BufferedReader  in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    java.io.PrintStream out =new java.io.PrintStream(new java.io.BufferedOutputStream(new java.io.FileOutputStream("test.log")));
   // System.setErr(out);//可以使输入转向
   // System.setOut(out);//可以使输入转向
    while(true){
     out.println(in.readLine());
     out.flush();

---------------------添加多的一个“\”表示转义符
java.io.File f2 = new java.io.File("c:\\test\\a\\b\\c");

实现文件类型的过滤方法-------1

String[] list = f2.list();
 System.out.println (java.util.Arrays.asList(list));
 list = f2.list(
  new java.io.FilenameFilter(){
   public boolean accept(java.io.File dir,String name){
    return name.endsWith(".java");
   } 
  }
 );

----------------------------2( 多个过滤方式)
String[] list = f2.list();
  list =f2.list(
  new java.io.FilenameFilter(){
   public boolean accept(java.io.File dir,String names){
      
    boolean java= names.endsWith(".java");
    boolean txt = names.endsWith(".txt");
    return java || txt;
   }
  } 
 );

//String[] list = f2.list();
java.util.Arrays.asList(list)将数组转成ArrayList

--------------------------------------------------删除有文件的目录实用代码

public static void delDir(java.io.File dir){
 if(dir.isFile()){
  dir.delete();
 }else{
  java.io.File[] fis = dir.listFiles();
   for(int i=0;i<fis.length;i++){
    delDir(fis[i]);
   }
  dir.delete();
 }
}
------------------------------------------------------计算文件夹大小代码

 System.out.println ("文件大小 "+s/1024+"  kB"+" = "+(float)s/1024/1024+"M");


 public  static double len(java.io.File dir){
  double s=0,len=0;
   if(dir.isFile()){
    s=dir.length();
   }else{
    java.io.File[] file = dir.listFiles();
    for(int i=0;i<file.length;i++){
     len+=len(file[i]);
    }
   }
   return len+s;
 }

--------------------------------------------------------几种集合类的使用

java.util.ArraryList list=new java.util.ArraryList();
     list.add("abc");
  list.size();
  list.iterator();

java.util.HashSet set = new java.util.HashSet();
  for(int i=0;i<10;i++){
   set.add("abc"+i);
  }
  
java.util.Iterator it = set.iterator();
  while(it.hasNext()){
   System.out.println (it.next());
  }
 
java.util.HashMap map = new java.util.HashMap();
  map.put(new Integer(1),"002");
  map.put(new Integer(4),"001");
  System.out.println (map.get(new Integer(1)));
  System.out.println (map.get(new Integer(4)));
  
java.util.Set keys = map.keySet();//返回一个SET集合
  it=keys.iterator();
 while(it.hasNext()){
  Object o = it.next();
   System.out.println (o+":"+map.get(o));
 }
-------------系统属性的操作

java.util.Properties p = new java.util.Properties();
  FileInputStream f = new FileInputStream("c:\\a.properties");
  
  p.load(f);
  System.out.println (p.get("test.author"));
  System.out.println (p.get("test.version"));
  
  p.setProperty("test.author","llldiadsfsa");
  p.setProperty("test.address","meimei");
  p.setProperty("test.version","10.0");
  
  java.io.FileOutputStream out = new FileOutputStream("c:\\a.properties");
  p.save(out,"ruanwei");
  
  //p.store(out,"test");
  out.close();
---------------------------------------------------------------未懂代码:以后再看????////
 String str1 = args[0];
  String str2 = args[1];                 
  
  if("int".equals(System.getProperty("precition"))){
   int i1 = Integer.parseInt(str1);
   int i2 = Integer.parseInt(str2);
   System.out.println ("i1"+"/"+"i2"+"="+divide(i1,i2));
  }else if("double".equals(System.getProperty("precition"))){
   double d1 = Double.parseDouble(str1);
   double d2 = Double.parseDouble(str2);
   System.out.println ("d1"+"/"+"d2"+"="+divide(d1,d2));
  }else if("你好".equals(System.getProperty("precition"))){
   System.out.println ("你好");
  }
 
 
 }
  public static int divide(int i1,int i2){
   return i1/i2;
  }
  public static double divide(double d1,double d2){
   return d1/d2;
  }
---------------------------------------------------------------------------------猜数字游戏代码
class GuessGame{
 
 public static  void main(String[] args){
  outer:
  while(true){
    
    int num =1 + (int)(Math.random()*100);
    int num1=20 ;
    System.out.println ("输入数字"+num);
     
       inner:
       while(num!=num1){
        
        java.io.BufferedReader in = new java.io.BufferedReader(
          new java.io.InputStreamReader(System.in));

        try{
       num1 = Integer.parseInt(in.readLine());
           if(num1>100 || num1<1){
            throw new java.lang.NumberFormatException();
          } 
          
       }catch(java.io.IOException e){
          System.out.println ("io error"+e);
        }catch(java.lang.NumberFormatException e2){
         System.out.println ("请输入正确的数字[1-100]");
         continue ;
        }
         if(num>num1){
          System.out.println ("is too small");
         }else if(num<num1){
         System.out.println ("is too large");
        }else{
         System.out.println ("ok");
         
         System.out.print ("还玩么? (y/n)");
         
          try{
           if("y".equalsIgnoreCase(in.readLine())){
            continue outer;
           }
          }catch(java.io.IOException e){}
            break outer;
        }
     }

    }
  } 
  
  
}
---------------------------------------------------------------------从随机数据取出与输入相等的数据

static char[] getText(String s){
    int w = s.length();
    char[] cc =new char[w];
    
    int  index=0;
    while(true){
     char  c = (char)(Math.random()*65536);
     if(c==s.charAt(index)){
      cc[index]=c;
      index++;
      if (index>=w){
       break;
        
      } 
     }
    }
   return cc;
  }
-----------------------------------------------------------------------多维数组的赋值------

  int[][] in= new int[5][];
  in[0]  = new int[]{0,1,2,3};
  in[1] = new  int[]{1,2};
--------------------------------------------------------覆盖垃圾收集方法
public void finalize(){
 System.out.println ("000");
}
-----------------------------------------------------移位)

3>>1 正数移位 0011---》 0001  为1(正数移位在前面加0)
-3>>1 负数移位 1011 -》1100--》1101 移位得 1110 --》1101--》1010 为-2(负数移位在前面加1)
-3>>>1  (这种移位以后再看???????????????未懂)
-

-----------描述注释文档----------------javadoc -d 文件绝对路径 -author -version private/public/protected

/**
 *
 *描述学生信息
 *@author 阮卫
 *@version 1.1
 *@docroot e:\lesson\day02\indexz
 */

/**
*无参数据构造函数
*/

/**
* 有参数据构造函数
*@param n  表示姓名
*/

/**
*方法说明
*/
-----------------------------------------------
Integer.MIN_VALUE
Short.MAX_VALUE
-----------------------------------
//在一常量池中引用的是同一地址
    q="sss";
    w="sss";

posted on 2006-05-26 14:05 Javaphua 阅读(353) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: