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

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

相关文章
相关标签/搜索