(java笔记整理)互斥锁和释放锁|FileInputStream流和FileOutputStream流和文件拷贝

119 阅读3分钟

互斥锁

package cc;

public class www {
    public static void main(String[] args) {
        SellTicket03 sellTicket03 = new SellTicket03();
        new Thread(sellTicket03).start();
        new Thread(sellTicket03).start();
        new Thread(sellTicket03).start();
    }
}

//实现接口方式 使用synchronized实现线程同步
class SellTicket03 implements Runnable {
    private boolean loop = true;//控制run变量
    private static int tiketNum = 100;
    Object object = new Object();
    //同步方法的锁为当前类本身
    //1. public synchronized static void m1() {} 锁是加在 SellTicket03.class//有static 锁在当前类.class
    //2. 如果在静态方法中,实现一个同步代码块.
    public synchronized static void m(){

    }
    public static void m2(){
        synchronized (SellTicket03.class){
            System.out.println("m2");
        }
    }

    //1.public synchronized void sell(){}就是一个同步方法
    //无static 默认锁在this对象
    //3.也可以在代码块上写synchronized 同步代码块 互斥锁还是在this对象
    public /*synchronized*/ void sell() {//同步方法 在同一时刻 只能有一个线程来执行sell()
        synchronized (/*this*/object) {
            if (tiketNum <= 0) {
                System.out.println("售票结束");
                loop = false;
                return;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + (--tiketNum));
        }
    }

    @Override
    public void run() {
        while (loop) {
            sell(); //sell()是一个同步方法
        }
    }
}
//使用 Thread 方式
    class SellTicket01 extends Thread{
    private static int tiketNum = 10;
//    public void m1(){
//        synchronized (this){
//            System.out.println("hello");
//        }
//    }
}

释放锁

下面操作会释放锁 下面操作不会释放锁

使用 FileInputStream 读取 hello.txt 文件,并将文件内容显示到控制台

package cc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class wwwwwwww {
    public static void main(String[] args) {

    }
    public void readFileInputSteam_() throws IOException {
        String filePath = "D:\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;//把定义放在外面扩大作用域
        try {
            //创建FileInputStream对象用于读取文件
            fileInputStream = new FileInputStream(filePath);
            while ((readData = fileInputStream.read())!= -1){
                System.out.println((char)readData);
            }

        } catch (IOException e) {//异常处理 IO流
            e.printStackTrace();
        } 
        finally {
            //关闭文件流
            fileInputStream.close();
        }
    }
}

使用 FileOutputStream 在 a.txt 文件,中写入 “hello,world”. 如果文件不存在,会创建文件(注意:前提是目录已经存在.)

package cc;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ggggg {
    public static void main(String[] args) {

    }
    @Test
    public void writeFile() throws IOException {
        //创建FileInputStream对象
        String filePath = "D:\a.txt";
        int writeData = 0;
        FileOutputStream fileOutputStream = null;//放到外面扩大作用域
        try {
            //得到 FileOutputStream 对象
            //1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容

            fileOutputStream = new FileOutputStream(filePath);
            //写入一个字节
            fileOutputStream.write('H');
            //写入字符串
            String str = "hello,world";
            //str.getBytes()可以把字符串-》字节数组
            fileOutputStream.write(str.getBytes());
            /*
write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off 的指定字节数组写入此文件输出流
*/
            fileOutputStream.write(str.getBytes(),0,str.length());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            fileOutputStream.close();
        }
    }
}

文件拷贝

这段代码写的太不容易了昨晚开始写,头晕乎乎的,写的乱七八糟的,今早上课堂上又写,还是头晕乎乎的,重复的跟着韩老师写了一遍又一遍才写出来。

package cc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class vvvvvv {
    public static void main(String[] args) throws IOException {
        //创建FileInputStream对象
        String srcfilePath = "D:\aa.BMP";
        String destfilePath = "D:\aa2.BMP";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcfilePath);
            fileOutputStream = new FileOutputStream(destfilePath);
            //定义一个字节数组 提高读取效果
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1) {
                //读取到后就写入文件 通过fileInputStream 一边读一边写
                fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法
            }
            System.out.println("拷贝成功");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭输入流和输出流 释放资源
            if (fileInputStream != null) {//输入流不为空
                fileInputStream.close();//关闭输入流
            }
            if (fileOutputStream != null) {//输出流不为空
                fileOutputStream.close();//关闭输出流
            }
        }
    }
}