java學習第二階段3day

 

讀寫java的基本數據類型
                         DataInputStream
                         DataOutputStreamjava


對象流dom

                        ObjectInputStream
                        ObjectOutputStreamide

-----------------------------
public class ObjectOutputStream1 {線程

 public static void main(String[] args) throws Exception {
  OutputStream os = new FileOutputStream("d:\\dd.txt");
  ObjectOutputStream oos = new ObjectOutputStream(os);指針

  oos.writeObject("牛凡是一個大大");
  
 }orm

}
-----------------------------對象


File 類繼承

         建立一個文件對象
                   File file = new File("d:\\a.txt")//存放路徑接口

                 //判斷文件是否存在
                 if(file.exists())進程


             //判斷file是不是文件夾
               if(file.isDirectory)

            //判斷file是不是文件
               if(file.isFile)

          //建立一個文件夾
                   file.mkdirs();

               //建立一個文件
                   file.createNewFile();

              //獲取文件 或 文件夾的名字
                     file.getName();

        //把當前目錄下的文件 或 文件夾名字 裝在一個File[]裏
                   File[] files = file.listFiles();
        //能夠利用加強for循環 打印File[]裏面的 文件 或 文件夾
             for(File f : files){
                System.out.println(f);}

 

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

SimpleDateFormat -- > 轉換時間格式類
                   //打印當前時間
                   SimpleDateFormat sdf = new SimpleDateFormat();
                   String str = sdf.format(new Date());


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

RadomAcessFile -->  隨機訪問文件的 讀取 和 寫入

                
public class RadomAcessFile1 {

 public static void main(String[] args) throws Exception {
  File file = new File("d:\\ax.txt");
//RandomAccessFile 自己帶一個指針 讀 或 寫 指針都會 移動
  RandomAccessFile raf = new RandomAccessFile(file,"rw");
  raf.writeInt(44);
 
  raf.writeChar('d');
 
  raf.writeUTF("好麻煩的樣子");
 
  raf.writeDouble(33.33);

  raf.seek(0);//把指針調到0的位置

  int i = raf.readInt();
  char c = raf.readChar();
  String str = raf.readUTF();
  double d = raf.readDouble();

  System.out.println("i:" + i + "c:" + c + "str:" + str + "d:" + d);
                raf.skipBytes();//skipBytes 跳過多少個字節

}

------------------------------------------------------------------
                                 線程

進程 --》 一個程序 至少有一個進程 
線程 --》 一個進程 至少有一個線程
            

建立一個線程對象 Thread thread = new Thread();

                 thread.MAX_PRIORITY  //設置線程爲最高優先級
                 thread.sleep(long l) //設置線程休眠時間(睡覺)
                 thread.state() //線程的狀態


一個線程 的建立
-----------------------
public class MyThread {
 public static void main(String[] args) {
//             建立一個線程的對象
  MyThread1 mythread = new MyThread1();
 // 啓動線程 (線程準備就緒)
  mythread.start();
 }

}

//線程的第一種 實現方式 繼承 Thread
//裏面放 要該線程實現的功能 或者 作的事情
class MyThread1 extends Thread{
 public void run(){
  for(int i = 0;i<10;i++){
   System.out.println("run"+i);
  }
 }
}

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

public class MyRunnable {

 public static void main(String[] args) {
//建立一個線程對象
  Runnable1 runb1 = new Runnable1();
    //把對象傳到Thread裏
  Thread thread = new Thread(runb1);
  thread.start();
  //線程main
  for(int i = 0;i<10;i++){
//Thread.currentThread 獲取當前線程 
   System.out.println(Thread.currentThread().getName() + "-" + i);
  }
 }
}
//線程第二種實現方法 實現接口 Runnable
class Runnable1 implements Runnable{

 @Override
//run 裏面 不能放參數 或者 把run改爲其餘名字
//由於run 是實現線程的基本
 public void run() {
  for(int i = 0;i<10;i++){
   System.out.println(Thread.currentThread().getName() + "-" + i);
  }
  
 }
 
}

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


利用屬性來控制 線程的執行


public class ThreaDaemon {

 public static void main(String[] args) {
  MyThread2 mythread = new MyThread2();
  Thread thread = new Thread(mythread);
  thread.start();
  for(int i = 0;i<100;i++){
   System.out.println(Thread.currentThread().getName() + ":"+ i);

   try {
    Thread.sleep(3);
    //當num大於等於 6時 讓 終止 MyThread2 線程
    if(MyThread2.num >= 6){
     MyThread2.flag = false;
    }
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 
}
class MyThread2 implements  Runnable{
 static int num = 1;
 static boolean flag = true;
 @Override
 public void run() {
//利用flag 來控制 該線程的 執行
        for(int i = 0;i<100 && flag;i++){
      num++;
  System.out.println(Thread.currentThread().getName()+":"+i);
      try {
      Thread.sleep(3);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
        }
   
  }
 }

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

利用 isDaemon 把線程 設置爲守護線程 來控制線程的執行


public class Daemon2 {

 public static void main(String[] args) {
  MyThread3 m3 = new MyThread3();
  //Thread thread = new Thread();
  m3.setDaemon(true);//設置該線程爲守護線程(前臺線程執行結束 後臺線程也隨之結束)
  m3.start();
  for(int i = 0;i<10;i++){
   System.out.println(Thread.currentThread().getName() + ":" + i);
   try {
    Thread.sleep(10);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}
class MyThread3 extends Thread{
 @Override
 public void run() {
  for(int i = 0;i<100;i++){
   try {
    Thread.sleep(20);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName() + ":" + i);
  }
 }
}

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

相關文章
相關標籤/搜索