Java流操作与网络类学习笔记

流操作与网络类

序列化与反序列化

  • 通过对象流的使用来实现
  1. ObjectInputStream 和 ObjectOutputStream
  2. 作用:用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
  3. 要想一个java对象是可序列化的,需要满足相应的要求。
    1. 需要实现接口:Serializable
    2. 当前类提供一个全局常量:serialVersionUID
    3. 除了当前Person类需要实现Serializable接口之外,还必须保证其内部所有属性也必须是可序列化的。(默认情况下,基本数据类型可序列化)
  4. 序列化机制:
    对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种
    二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。
    当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。

注:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

  • 可以序列化的自定义类
// 需要实现Serializable接口
public class Person implements Serializable{
    // 序列化UID,必须要自己定义
    private static final long serialVersionUID = 475463534532L;    

    private String name;    // 官方类,本身就是个可以序列化的类
    private int age;
    private int id;
    private Account acct;    // 内部自定义类

    public Person(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public Person(String name, int age, int id, Account acct) {
        this.name = name;
        this.age = age;
        this.id = id;
        this.acct = acct;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                ", acct=" + acct +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {

        this.name = name;
        this.age = age;
    }

    public Person() {

    }
}

class Account implements Serializable{
    public static final long serialVersionUID = 4754534532L;
    private double balance;

    @Override
    public String toString() {
        return "Account{" +
                "balance=" + balance +
                '}';
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public Account(double balance) {

        this.balance = balance;
    }
}
  • 序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
@Test
public void testObjectOutputStream(){
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream("object.dat"));

        oos.writeObject(new String("哇哈哈111"));
        oos.flush();//刷新操作

        oos.writeObject(new Person("哦呵呵",23,1001,new Account(5000)));
        oos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(oos != null){
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
  • 反序列化:将磁盘文件中的对象还原为内存中的一个java对象
@Test
public void testObjectInputStream(){
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream("object.dat"));

        Object obj = ois.readObject();
        String str = (String) obj;
        Person p = (Person) ois.readObject();

        System.out.println(str);
        System.out.println(p);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        if(ois != null){
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

java.nio.file工具类

jdk 7.0 时,引入了 Path、Paths、Files三个类。Path为了简化文件操作的File类的升级版本,Paths与Files是工具类

Path,Paths类

  • 实例化Path
    static Path get(String first, String … more) : 用于将多个字符串串连成路径
    static Path get(URI uri): 返回指定uri对应的Path路径
@Test
public void test1() {
    Path path1 = Paths.get("d:\\nio\\hello.txt");//new File(String filepath)

    Path path2 = Paths.get("d:\\", "nio\\hello.txt");//new File(String parent,String filename);

    System.out.println(path1);
    System.out.println(path2);

    Path path3 = Paths.get("d:\\", "nio");
    System.out.println(path3);
}
  • Path中的常用方法
方法描述
boolean startsWith(String path)判断是否以 path 路径开始
boolean endsWith(String path)判断是否以 path 路径结束
boolean isAbsolute()判断是否是绝对路径
Path getParent()返回Path对象包含整个路径,不包含 Path 对象指定的文件路径
Path getRoot()返回调用 Path 对象的根路径
Path getFileName()返回与调用 Path 对象关联的文件名
int getNameCount()返回Path 根目录后面元素的数量
Path getName(int idx)返回指定索引位置 idx 的路径名称
Path toAbsolutePath()作为绝对路径返回调用 Path 对象
Path resolve(Path p)合并两个路径,返回合并后的路径对应的Path对象
File toFile()将Path转化为File类的对象
@Test
public void test2() {
    Path path1 = Paths.get("d:\\", "nio\\nio1\\nio2\\hello.txt");
    Path path2 = Paths.get("hello.txt");

    System.out.println(path1);    // d:\nio\nio1\nio2\hello.txt
    System.out.println(path1.startsWith("d:\\nio"));    // true
    System.out.println(path1.endsWith("hello.txt"));    // true
    System.out.println(path1.isAbsolute()); // true
    System.out.println(path2.isAbsolute()); // false
    System.out.println(path1.getParent());  // d:\nio\nio1\nio2                        
    System.out.println(path2.getParent());  // null  
    System.out.println(path1.getRoot());    // d:\    
    System.out.println(path2.getRoot());    // null
    System.out.println(path1.getFileName()); // hello.txt
    System.out.println(path2.getFileName()); // hello.txt
    for (int i = 0; i < path1.getNameCount(); i++) {
        System.out.print(path1.getName(i) + " "); // nio nio1 nio2 hello.txt
    }
    System.out.println(path1.toAbsolutePath()); // d:\nio\nio1\nio2\hello.txt
    System.out.println(path2.toAbsolutePath()); // 当前工程下hello.txt的绝对路径
    Path path3 = Paths.get("d:\\", "nio");
    Path path4 = Paths.get("nioo\\hi.txt");
    path3 = path3.resolve(path4);    
    System.out.println(path3);    // d:\nio\nioo\hi.txt

    File file = path1.toFile();//Path--->File的转换//		File toFile(): 将Path转化为File类的对象
    Path newPath = file.toPath();//File--->Path的转换
}

Files类使用

  • Files常用方法
方法描述
Path copy(Path src, Path dest, CopyOption … how)文件的复制
Path createDirectory(Path path, FileAttribute<?> … attr)创建一个目录
Path createFile(Path path, FileAttribute<?> … arr)创建一个文件
void delete(Path path)删除一个文件/目录,如果不存在,执行报错
void deleteIfExists(Path path)Path对应的文件/目录如果存在,执行删除.如果不存在,正常执行结束
Path move(Path src, Path dest, CopyOption…how)将 src 移动到 dest 位置
long size(Path path)返回 path 指定文件的大小
boolean exists(Path path, LinkOption … opts)判断文件是否存在
boolean isDirectory(Path path, LinkOption … opts)判断是否是目录
boolean isRegularFile(Path path, LinkOption … opts)判断是否是文件
boolean isHidden(Path path)判断是否是隐藏文件
boolean isReadable(Path path)判断文件是否可读
boolean isWritable(Path path)判断文件是否可写
boolean notExists(Path path, LinkOption … opts)判断文件是否不存在
InputStream newInputStream(Path path, OpenOption…how)获取 InputStream 对象
OutputStream newOutputStream(Path path, OpenOption…how)获取 OutputStream 对象
SeekableByteChannel newByteChannel(Path path, OpenOption…how)获取与指定文件的连接,how 指定打开方式。
DirectoryStream newDirectoryStream(Path path) 打开 path 指定的目录

创建流

  • 文件权限
    StandardOpenOption.READ:表示对应的Channel是可读的。
    StandardOpenOption.WRITE:表示对应的Channel是可写的。
    StandardOpenOption.CREATE:如果要写出的文件不存在,则创建。如果存在,忽略
    StandardOpenOption.CREATE_NEW:如果要写出的文件不存在,则创建。如果存在,抛异常
@Test
public void test3() throws IOException{
	Path path1 = Paths.get("d:\\", "hello.txt");

	InputStream inputStream = Files.newInputStream(path1, StandardOpenOption.READ);

	OutputStream outputStream = Files.newOutputStream(path1, StandardOpenOption.WRITE,StandardOpenOption.CREATE);

	SeekableByteChannel channel = Files.newByteChannel(path1, StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);

	Path path2 = Paths.get("e:\\lib");
	DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path2);
	Iterator<Path> iterator = directoryStream.iterator();
	while(iterator.hasNext()){
		System.out.println(iterator.next());
	}
}

网络类

  • InetAddress类常用方法
方法描述方式
public static InetAddress getByName(String host)根据填入获取Inet类
public static InetAddress getLocalHost()获取本地ip
public String getHostName()获取域名
public String getHostAddress()获取IP地址

TCP实例

public class TCPTest3 {

    /*
        这里涉及到的异常,应该使用try-catch-finally处理
         */
    @Test
    public void client() throws IOException {
        //1.创建Socket对象,指明服务器端的ip和端口号
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
        //2.获取一个输出流,用于输出数据
        OutputStream os = socket.getOutputStream();
        //3.读取需要发送的文件
        FileInputStream fis = new FileInputStream(new File("test.txt"));
        //4.进行文件发送
        byte[] buffer = new byte[1024];
        int len;
        while((len = fis.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        //关闭数据的输出
        socket.shutdownOutput();

        //5.接收来自于服务器端的数据,并显示到控制台上
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bufferr = new byte[20];
        int len1;
        while((len1 = is.read(buffer)) != -1){
            baos.write(buffer,0,len1);
        }

        System.out.println(baos.toString());

        //6.关闭资源
        fis.close();
        os.close();
        socket.close();
        baos.close();
    }

    /*
    这里涉及到的异常,应该使用try-catch-finally处理
     */
    @Test
    public void server() throws IOException {
        //1.创建服务器端的ServerSocket,指明自己的端口号
        ServerSocket ss = new ServerSocket(9090);
        //2.调用accept()表示接收来自于客户端的socket
        Socket socket = ss.accept();
        //3.获取输入流
        InputStream is = socket.getInputStream();
        //4.接收数据输出到本地文件
        FileOutputStream fos = new FileOutputStream(new File("text2.txt"));
        //5.读取输入流中的数据
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }

        System.out.println("文件传输完成");

        //6.服务器端给予客户端反馈
        OutputStream os = socket.getOutputStream();
        os.write("文件已接收".getBytes());

        //7.关闭资源
        fos.close();
        is.close();
        socket.close();
        ss.close();
        os.close();
    }
}

UDP实例

public class UDPTest {

    //发送端
    @Test
    public void sender() throws IOException {

        DatagramSocket socket = new DatagramSocket();
        String str = "UDP方式发送数据";
        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
        socket.send(packet);
        socket.close();
    }
    //接收端
    @Test
    public void receiver() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

URL实例

  • 可以在本地安装一个tomcat做服务器,模拟从服务器下载文件
public class URLTest1 {

    public static void main(String[] args) {

        HttpURLConnection urlConnection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            URL url = new URL("http://localhost:8080/examples/test.txt");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            is = urlConnection.getInputStream();
            fos = new FileOutputStream("test2.txt");

            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }

            System.out.println("下载完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(urlConnection != null){
                urlConnection.disconnect();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java是一种广泛使用的编程语言,有简单、面向对象、跨平台等特点。下面是Java学习的一些重要知识点和学习笔记: 1. Java基础: - 数据型:Java提供了多种数据型,包括基本数据型和引用数据型。 - 控制程:学习如何使用条件语句(if-else、switch)、循环语句(for、while)等控制程序的程。 - 数组:了解如何声明、初始化和操作数组。 - 方法学习如何定义和调用方法,以及方法的参数和返回值。 - 和对象:理解和对象的概念,学习如何定义、创建对象和使用的成员变量和方法。 - 继承和多态:掌握继承和多态的概念,了解如何使用继承创建子,并实现方法的重写和多态的应用。 2. 面向对象编程: - 封装:学习如何使用访问修饰符(public、private、protected)来控制的成员的访问权限。 - 继承:了解继承的概念和作用学习如何使用extends关键字创建子。 - 多态:理解多态的概念和实现方式,学习如何使用父引用指向子对象,实现方法的动态绑定。 3. 异常处理: - 异常的分:了解异常的分(Checked Exception和Unchecked Exception)和常见的异常型。 - 异常处理机制:学习如何使用try-catch语句捕获和处理异常,以及使用throws关键字声明方法可能抛出的异常。 4. 输入输出: - 文件操作学习如何读写文件,包括使用File、字节和字符等。 - 序列化:了解对象的序列化和反序列化,学习如何将对象保存到文件或网络中。 5. 集合框架: - 学习Java提供的集合框架,包括List、Set、Map等常用的集合,以及它们的特点和用法。 6. 多线程编程: - 学习如何创建和管理线程,了解线程同步和线程间通信的方法。 7. 数据库连接: - 学习如何使用Java连接数据库,执行SQL语句,进行数据的增删改查操作。 以上是Java学习的一些重要知识点和学习笔记,希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值