Java学习记录(7)

本文详细介绍了Java中的线程同步技术,包括互斥锁和synchronized关键字的使用,以及线程死锁的避免。此外,还探讨了文件操作,如创建、读取和删除文件,以及IO流的各种类型,如BufferedReader和BufferedWriter。最后,讨论了对象流的序列化和反序列化,以及TCP/IP协议和Socket网络通信的基础知识。
摘要由CSDN通过智能技术生成
Synchronized

线程同步机制

  1. 在多线程编程中,一些敏感数据不允许被多个线程同时访问,此时就使用同步访问技术,保证数据在任何同一时刻,最多有一个线程访问,以保证数据的完整性。
  2. 线程同步,即当有一个线程对内存进行操作时,其他线程都不可以对这个地址进行操作,直到该线程完成操作,其他线程才能对该内存地址操作。

互斥锁

  1. Java语言中,引入了对象互斥锁的概念,来保证共享数据的完整性
  2. 每个对象都对应于一个可称为“互斥锁”的标记,这个标记用来保证在任意时刻,只能有一个线程访问该对象。
  3. 关键字synchronized来与对象的互斥锁联系,当某个对象用synchronized修饰时,表明该对象在任意时刻只能由一个线程访问。
  4. 同步的局限性:导致程序的执行效率要降低
  5. 同步方法(非静态)的锁可以是this,也可以是其他对象(要求是同一个对象)
  6. 同步方法(静态)的锁为当前类本身

线程死锁

多个线程占用了对方的资源锁,但不肯相让,导致了死锁的发生。

释放锁

  1. 当前线程的同步方法、同步代码块停止执行
  2. 当前线程的同步方法、同步代码块遇到break,return
  3. 当前线程在同步方法、同步代码块中遇到了未处理的ERROR、Exception,导致异常结束
  4. 当前线程在同步方法、同步代码块中执行了线程对象的wait()方法,当前线程暂停并释放锁

IO流

文件的基本操作

new File(String pathname)//根据路径构建一个对象

new File(File parent,String child)//根据父目录文件+子路径构建

new File(String parent,String child)//根据父目录+子路径构建

createNewFile //创建新文件

方式1:                    

  String Pathname = "D:\\new1.txt";

            File file = new File(Pathname);

            try {

                file.createNewFile();

            } catch (IOException e) {

                e.printStackTrace();

            }

方法2

File parentFile = new File("e:\\");

            String fileName = "new2.txt";

            File file = new File(parentFile, fileName);

            try {

                file.createNewFile();

            } catch (IOException e) {

                e.printStackTrace();

            }

方法3      

 String parentFile = “e:\\”;

            String fileName = "new3.txt";

            File file = new File(parentFile, fileName);

            try {

                file.createNewFile();

            } catch (IOException e) {

                e.printStackTrace();

            }

获取文件信息:

        File file = new File("d:\\news1.txt");

        System.out.println(file.getAbsolutePath());//获取绝对路径

        System.out.println(file.getParent());;//得到父级目录

        System.out.println(file.length());//获取文件字节长度

  • 目录操作与文件删除
  • mkdir创建一级目录
  • mkdirs创建多级目录
  • delete删除空目录或文件
  • 在Java编程中,目录也被当做文件操作
IO流操作
  • IO是input/output的缩写,IO技术是十分实用的技术,用于数据传输处理。如读写文件,网络通讯等。
  • Java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行。
  • Java.io包下提供了各种流类和接口,用以获取不同种类的数据,并通过方法输入或输出数据。

FileInputStream 文件输入流

BufferedInputStream 缓冲字节输入流

ObjectInputStream 对象字节输入流

文件读入方法

public void read() {

        int cnt = 0;

        String pathFile = "d:\\anT.txt";

        FileInputStream fileInputStream = null;//因为如果在try代码块中定义文件,则无法在finally中调用该对象对文件进行关闭,所以在try外部先定义一个空对象,再对该对象进行赋值,从而达到扩大对象作用域的效果。

        try {

            fileInputStream = new FileInputStream(pathFile);

            while ((cnt = fileInputStream.read()) != -1) {

                System.out.printf("%c",(char)cnt);

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                fileInputStream.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

}

读入中文

byte[] bytes = new byte[30];//一次读入30个字节





        String pathFile = "d:\\anT.txt";

        FileInputStream fileInputStream = null;

        try {

            fileInputStream = new FileInputStream(pathFile);

            while ((fileInputStream.read(bytes)) != -1) {

//                System.out.printf("%c",(char)cnt);

                System.out.print(new String(bytes,0, bytes.length));

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                fileInputStream.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

写入一个文件

new FileOutputStream(pathname,true)//以追加的方式写入文件

String pathName = "D:\\tan.txt";

        FileOutputStream fileOutputStream = null;

        try {

            //得到一个对象

            fileOutputStream = new FileOutputStream(pathName);//如果文件不存在则自动创建一个文件

//            System.out.println("文件建立成功");

             String str = "遇事不决 可问春风";

             fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));



        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                fileOutputStream.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

 

fileOutputStream = new FileOutputStream(pathName,true);

//            System.out.println("文件建立成功");

             String str = "春风不语 既随本心";

             fileOutputStream.write(str.getBytes(StandardCharsets.UTF_8));

文件复制

public void Copy(){

        FileInputStream fileInputStream = null;

        FileOutputStream fileOutputStream = null;



        String src = "D:\\SpiderMan.png";

        String dest = "E:\\SpiderMan.png";



        try {

            fileInputStream = new FileInputStream(src);

            fileOutputStream = new FileOutputStream(dest);



            int readLine = 0;

            byte[] buf = new byte[1024];



            while((readLine = fileInputStream.read(buf))!= -1)

            {

                fileOutputStream.write(buf,0,readLine);//一定要使用这种方法

            }



        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            if(fileInputStream != null){

                try {

                    fileInputStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            if(fileOutputStream != null){

                try {

                    fileOutputStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

    }





FileReader和FileWriter是字符流,即按字符来操作IO

FileReader操作方法:

  1. new FileReader(File/String)
  2. read:每次读取单个字符,返回该字符,如果到文件末尾返回-1.
  3. read(char[]):批量读取多个字符到数组,返回读到的字符数,如果到文件末尾返回-1。

相关API:

  1. new String(char[]):将char[]转化为String
  2. new String(char[],off,len):将char[]的指定部分转化为String

FileWriter常用方法:

  1. new FileWriter(File/String):覆盖模式,相当于流的指针在顶端
  2. new FileWriter(File/String,true):追加模式,相当于流的指针在尾端
  3. writer(int):写入单个字符
  4. write(char[]):写入指定数组
  5. write(char[],off,len):写入字符串的指定部分
  6. write(String):写入整个字符串
  7. write(String,off,len):写入字符串的指定部分

相关API:

String类,toCharArray:将String转化成char[].

注意:

FileWriter使用后必须要close()或者flash(),否则写入不到指定文件。

FileReader:



public void Story(){

        String pathName = "D:\\小二,上酒!.txt";

        FileReader fileReader = null;



        int data = 0;

        try {



            fileReader = new FileReader(pathName);

            //读取是循环读取

            while((data = fileReader.read())!= -1)

            {

                System.out.print((char)data);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }finally {

           if(fileReader != null)

           {

               try {

                   fileReader.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

           }

        }

}



方法二(字符数组更快速)

public void Story(){

        String pathName = "D:\\小二,上酒!.txt";

        FileReader fileReader = null;



        int readLen = 0;

        char[] buf = new char[9];

        try {



            fileReader = new FileReader(pathName);

            //读取是循环读取

            while((readLen = fileReader.read(buf))!= -1)

            {

                System.out.print(new String(buf,0,readLen));

            }

        } catch (IOException e) {

            e.printStackTrace();

        }finally {

           if(fileReader != null)

           {

               try {

                   fileReader.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

           }

        }

    }

写入文件操作

String pathName = "D:\\小二,上酒!.txt";





        Scanner index = new Scanner(System.in);



        String story = index.next();



        FileWriter fileWriter = null;

        try {

            fileWriter = new FileWriter(pathName,true);

            fileWriter.write(story);

        } catch (IOException e) {

            e.printStackTrace();

        }finally {

            //一定要关闭流或者刷新流才能真正将数据保存进文件

            try {

                fileWriter.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

节点流

节点流可以从一个特定的数据源读写数据,如FileReader、FIleWriter。

处理流(也叫包装流)是“连接”已存在的流(节点流或处理流)只上,为程序提供更为强大的读写功能,如BufferedReader、BufferedWriter。

BufferedReader类中,有属性Reader,即可以封装一个节点流,该节点流可以是任意的,只要是Reader子类

节点流和处理流的区别和联系:

  1. 节点流是底层流/低级流,直接跟数据源相接。
  2. 处理流包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出。
  3. 处理流(也叫包装流)对节点流进行包装,使用修饰器设计模式,不会直接与数据源相连。

处理流的功能主要体现在以下两个方面:

  1. 性能的提高:主要以增加缓冲的方式来提高输入输出的效率。
  2. 操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便。

BufferedReader、BufferedWriter是字符处理流,是按照字符来读取数据的。

关闭时,只需要关闭外层流即可。

BufferedReader读取:

BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\小二,上酒!.txt"));



        String line;

        while((line = bufferedReader.readLine())!= null)

        {

            System.out.println(line);

        }



        bufferedReader.close();//关闭外层流

BufferedWriter写入文件

String pathName = "D:\\小二,上酒!.txt";



        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(pathName,true));

        String text;

        Scanner index = new Scanner(System.in);

        text = index.next();

        //        bufferedWriter.write("\n");

        bufferedWriter.newLine();//插入一个换行符,且次换行符与系统相关

        bufferedWriter.write(text);

        bufferedWriter.close();

BufferedReader和BufferedWriter关于文件复制的操作与FileReader和FIleWriter是相似的(本质上是使用的FIle)

BufferedReader src = new BufferedReader(new FileReader("E:\\风吹过那年盛夏.docx"));

        BufferedWriter dest = new BufferedWriter(new FileWriter("D:\\风吹过那年盛夏.docx"));



        String line;

        while((line = src.readLine()) != null){

            dest.write(line);

        }



        if(src != null)

            src.close();

        if(dest != null)

            dest.close();

不过这里的这个docx文件虽然产生了移动,但是好像并没有权限从新复制到的盘中打开。

BufferedReader和BufferedWriter不要去操作视频,声音,doc,pdf等二进制文件可能造成文件损坏

Buffed字节处理流

Buffed字节处理复制二进制文件操作

BufferedInputStream src = new BufferedInputStream(new FileInputStream("E:\\风吹过那年盛夏.docx"));

        BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream("D:\\风吹过那年盛夏.docx"));



        int readline = 0;

        byte[] buf = new byte[1024];



        while((readline = src.read(buf))!= -1)

        {

            dest.write(buf,0,readline);

        }



        if(src == null)

            src.close();

        if(dest == null)

            dest.close();

对象流

ObjectInputStream与ObjectOutputStream

序列化和反序列化

  1. 序列化就是在保存数据时,保存数据的值和数据类型
  2. 反序列化就是在恢复数据时,恢复数据的值和数据类型
  3. 需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:

       Serializable //这是一个接口标记(一般用这个,接口内无方法需要实现)

       Externalizable(该接口内有两个方法需要实现)

序列化操作

public class ObjectOutputStream_ {

    public static void main(String[] args) throws IOException  {

        //序列化后保存的文件格式不是纯文本,而是按照它自己的格式

        String pathName = "D:\\Hello.dat";

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(pathName));



        oos.write(20040506);//自动装箱

        oos.writeUTF("遇事不决 可问春风");

        oos.writeObject(new Sword("陈平安",21));//如果要将一个对象序列化,则必须将它的类实现Serializable接口

        oos.close();

    }

}



class Sword implements Serializable{

    String name;

    int age;



    public Sword(String name, int age) {

        this.name = name;

        this.age = age;

}

反序列化操作

public static void main(String[] args) throws Exception {

        String pathName = "D:\\Hello.dat";

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(pathName));



        //读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致

        System.out.println(ois.readInt());

        System.out.println(ois.readUTF());

        Object o = ois.readObject();

        System.out.println(o);



        ois.close();

    }

}

class Sword implements Serializable{

    String name;

    int age;



    public Sword(String name, int age) {

        this.name = name;

        this.age = age;

    }



    @Override

    public String toString() {

        return "Sword{" +

                "name='" + name + '\'' +

                ", age=" + age +

                '}';

    }

}

序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性

序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员

序列化对象时,要求里面属性的类型也需要实现序列化接口

序列化具备可继承性,也就是如果某类已经实现了序列化,则它所有的子类也已经默认实现序列化

转换流:

InputStreamReader和OutputStreamWriter

网络

  • 两台设备之间通过网络完成数据传输
  • 网络通信:将数据通过网络从一台设备传输到另一台设备
  • 两台或多台设备通过一定物理设备连接起来构成了网络
  • 局域网:覆盖范围最小,仅仅覆盖一个教室或一个机房
  • 地域网:覆盖范围较大,可以覆盖一座城市
  • 广域网:覆盖范围最大,可以覆盖全国,甚至全球,万维网是广域网的代表
  • IP地址
  • 用于唯一标识网络中的每台计算机
  • 查看IP地址:ipconfig
  • IP地址的表现形式:点分十进制:xx.xx.xx.xx
  • 每一个十进制数的范围0~255
  • ip地址的组成:网络地址+主机地址,例如192.168.16.69
  • iIPv6是互联网工程任务组设计的用于替代IPv4的下一代IP协议,其地址数量号称可以为全世界的每一粒沙子编上一个地址。
  • 由于IPv4最大的问题在于网络地址资源有限,严重制约了互联网的应用和发展。IPv6的使用,不仅能解决网络地址资源数量的问题,而且也解决了多种接入设备连入互联网的障碍。

域名:为了方便记忆,解决记IP的困难

概念:将IP地址映射成域名

端口号:

概念:用于标识计算机上某个特定的网络程序

表示形式:以整数范围,范围0~65535

0~1024已经被占用,比如ssh 22,ftp 21,smtp 25,http 80    

tomcat 8080

mysql 3306

oracle 1521

sqlserver 1433

网络通讯协议

TCP/IP传输协议,即传输控制/网络协议,也叫作网络通讯协议。它是在网络的使用中的最基本的通信协议。TCP/IP传输协议对互联网中各部分进行通信的标准和方法进行了规定。并且,TCP/IP传输协议是保证网络数据信息及时、完整传输的两个重要的协议。TCP/IP传输协议是严格来说是一个四层的体系结构,应用层、传输层、网络层和数据链路层都包含其中。

在网络编程中数据的组织形式就是一种协议

TCP和UDP

TCP协议 传输控制协议

  1. 使用TCP协议前,须先建立TCP连接,形成传输数据通道
  2. 传输前,采用“三次握手”方式,是可靠的
  3. TCP协议进行通信的两个应用进程:客户端、服务端
  4. 在连接中可进行大数据量的传输
  5. 传输完毕,需释放已建立的连接,效率低

       UDP协议  用户数据协议

  1. 将数据、源、目的封装成数据包,不需要建立连接
  2. 每个数据包的大小限制在64k以内,不适合传输大量数据
  3. 因无需连接,故是不可靠的
  4. 发送数据结束时无需释放资源(因为不是面向连接的),速度快

      

InetAddress类

  1. 获取本机InetAdress对象getLocalHost
  2. 根据指定主机名/域名获取ip地址对象getByName
  3. 获取InetAddress对象的地址getHostAddress
public static void main (String[] args)  throws UnknownHostException {

        InetAddress inetAddress = InetAddress.getLocalHost();

        System.out.println(inetAddress);

        System.out.println(InetAddress.getByName("www.4399.com"));

}

socket

  1. 套接字(socket)开发网络应用程序被广泛采用,以至于成为事实上的标准。
  2. 通信的两端都要有socket,是两台机器通信的端点
  3. 网络通信其实就是socket之间的通信
  4. socket允许程序把网络连接当成一个流,数据在两个socket间通过IO传输
  5. 一般主动发起通信的应用程序属客户端,等待通信请求的为服务端

TCP网络通信编程

服务器上通信:

ServerSocket serverSocket = new ServerSocket(9999);//这里要确保该端口未被使用



        System.out.println("等待端口连接");

        Socket socket = serverSocket.accept();

        System.out.println("连接成功");





        InputStream inputStream = socket.getInputStream();



        byte[] buf = new byte[1024];

        int readline = 0;

        while((readline = inputStream.read(buf))!= -1)

        System.out.println(new String(buf,0,readline));



        inputStream.close();

        socket.close();





  Socket socket = new Socket(InetAddress.getLocalHost(), 9999);

        System.out.println("连接成功");

        System.out.println(socket.getClass());





        Scanner index = new Scanner(System.in);



        String buf = index.next();



        OutputStream outputStream = socket.getOutputStream();



        outputStream.write(new String(buf).getBytes(StandardCharsets.UTF_8));





        outputStream.close();

        socket.close();

更新后的代码:

package socket;



import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.nio.charset.StandardCharsets;

import java.util.Scanner;



public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(9999);//这里要确保该端口未被使用



        System.out.println("等待端口连接");

        Socket socket = serverSocket.accept();

        System.out.println("连接成功");



        Scanner index = new Scanner(System.in);



        InputStream inputStream = socket.getInputStream();



        byte[] buf = new byte[1024];

        int readline = 0;

        while((readline = inputStream.read(buf))!= -1)

        System.out.println(new String(buf,0,readline));



        socket.shutdownInput();



        String str = index.next();

        OutputStream outputStream = socket.getOutputStream();

        outputStream.write(new String(str).getBytes(StandardCharsets.UTF_8));



        outputStream.close();

        inputStream.close();

        socket.close();

    }

}

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.InetAddress;

import java.net.Socket;

import java.nio.charset.StandardCharsets;

import java.util.Scanner;



public class Client {

    public static void main(String[] args) throws IOException {





        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);

        System.out.println("连接成功");

        System.out.println(socket.getClass());





        Scanner index = new Scanner(System.in);



        String buff = index.next();



        OutputStream outputStream = socket.getOutputStream();



        outputStream.write(new String(buff).getBytes(StandardCharsets.UTF_8));



        socket.shutdownOutput();



        InputStream inputStream = socket.getInputStream();





        byte[] buf = new byte[1024];



        int readline = 0;

        while((readline = inputStream.read(buf))!= -1) {

            System.out.println(new String(buf, 0, readline));

        }





        inputStream.close();

        outputStream.close();

        socket.close();

    }

}

加入了shutdown方法用以结束输入输出,便于第二次重新读入或输入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值