Javaphua Blog

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            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"))

          //將字串符轉(zhuǎn)換成日期類型
          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");

          //在數(shù)據(jù)庫中將字符串轉(zhuǎn)成日期存入數(shù)據(jù)庫
          birth=to_date(?,'YYYY-MM-DD HH24:MI:SS')

          //發(fā)送SQL語句,執(zhí)行存儲過程
          java.sql.CallableStatement cs = con.prepareCall("{call addAge}")

          //更新時使用數(shù)據(jù)庫的系統(tǒng)時間(sysdate)
          "update student set birth=sysdate"

          //IO的幾種常用通信方法---------------采用對象方式進行傳輸數(shù)據(jù)-------------------------
          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();

          --------------------------------------------------------------
          //給按鈕添加事件 按鈕觸發(fā)數(shù)字自動滾動

          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());

          //在剪切時候,它在一個內(nèi)部類
          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();
          ?}

          }

          //設(shè)置窗口關(guān)閉時自動退出
          this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

          ------------------------------------------在lesson中有一個鼠標(biāo)事件還沒有看通,有點復(fù)雜(day16)

          //給按鈕添加事件的一種方法
          ? ?//按鈕調(diào)用
          ?button.addActionListener(new count2());
          ?//事件類繼承監(jiān)聽器
          ?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作為工功能類構(gòu)造的參數(shù)可很方便的在窗體顯示計算結(jié)果

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

          如面板上有多個控件時,可用以下方法將其加入面板中
          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 如何進行文件傳輸?shù)?/p>

          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");
          ?????//對緩存進行編碼設(shè)置后,再進行解析
          ? ??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();
          ? ??
          ? }
          -------------------------------???關(guān)于java.nio.buff中的一些緩沖區(qū)的管理代碼還沒有看懂??


          幾種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(
          ?????????//是否在文件后進行追加數(shù)據(jù)(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---------------------------從控制臺讀取數(shù)據(jù)
          java.io.BufferedReader r? = new java.io.BufferedReader(
          ??????new java.io.InputStreamReader(System.in));
          ??????
          ??String s=r.readLine();
          ??System.out.println (s);

          6-------------------------------向文件寫入數(shù)字
          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);
          ??? ?}
          ???}
          ??}

          ------------------從控制臺向文件輸入數(shù)據(jù)

          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);//可以使輸入轉(zhuǎn)向
          ? ?// System.setOut(out);//可以使輸入轉(zhuǎn)向
          ? ? while(true){
          ? ? ?out.println(in.readLine());
          ? ? ?out.flush();

          ---------------------添加多的一個“\”表示轉(zhuǎn)義符
          java.io.File f2 = new java.io.File("c:\\test\\a\\b\\c");

          實現(xiàn)文件類型的過濾方法-------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)將數(shù)組轉(zhuǎn)成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));
          ?}
          -------------系統(tǒng)屬性的操作

          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;
          ??}
          ---------------------------------------------------------------------------------猜數(shù)字游戲代碼
          class GuessGame{
          ?
          ?public static? void main(String[] args){
          ??outer:
          ??while(true){
          ????
          ????int num =1?+ (int)(Math.random()*100);
          ????int num1=20 ;
          ????System.out.println ("輸入數(shù)字"+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 ("請輸入正確的數(shù)字[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;
          ???? ???}
          ?????}

          ????}
          ??}?
          ??
          ??
          }
          ---------------------------------------------------------------------從隨機數(shù)據(jù)取出與輸入相等的數(shù)據(jù)

          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;
          ??}
          -----------------------------------------------------------------------多維數(shù)組的賦值------

          ??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 正數(shù)移位 0011---》 0001? 為1(正數(shù)移位在前面加0)
          -3>>1 負數(shù)移位 1011 -》1100--》1101 移位得 1110 --》1101--》1010 為-2(負數(shù)移位在前面加1)
          -3>>>1? (這種移位以后再看???????????????未懂)
          -

          -----------描述注釋文檔----------------javadoc -d 文件絕對路徑 -author -version private/public/protected

          /**
          ?*
          ?*描述學(xué)生信息
          ?*@author 阮衛(wèi)
          ?*@version 1.1
          ?*@docroot e:\lesson\day02\indexz
          ?*/

          /**
          *無參數(shù)據(jù)構(gòu)造函數(shù)
          */

          /**
          * 有參數(shù)據(jù)構(gòu)造函數(shù)
          *@param n? 表示姓名
          */

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

          posted on 2006-05-26 14:05 Javaphua 閱讀(361) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 青龙| 罗甸县| 鄂伦春自治旗| 东乡| 东阿县| 自治县| 米脂县| 阿克苏市| 玛多县| 平山县| 叶城县| 镇宁| 奉节县| 全椒县| 永吉县| 永靖县| 永嘉县| 彩票| 南召县| 万荣县| 成武县| 大荔县| 六安市| 巫溪县| 卓尼县| 开鲁县| 宁化县| 张掖市| 读书| 依安县| 牡丹江市| 寿光市| 南康市| 兖州市| 禹州市| 丽水市| 涿州市| 开远市| 上饶市| 垣曲县| 察雅县|