会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132837个问题

先启动服务器端、再启动客户端后 服务器端没有收到信息 但是客户端收到了反馈

package com.bjsxt.net.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端  要先启动服务器端
 */
public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("---------------服务器端已启动--------------------");
        //(1)创建ServerSocket对象
        ServerSocket server = new ServerSocket(9999);
        //(2)监听是否有客户端来请求连接
        Socket client = server.accept();
        //(3)获取信息  输入流
        InputStream is = client.getInputStream();
        //(4)反馈已收到  输出流
        OutputStream os = client.getOutputStream();
        os.write("收到了".getBytes());
        //(5)关闭流、关闭Socket
        if(os!=null){
            os.close();
        }
        if(is!=null){
            is.close();
        }
        if(client!=null){
            client.close();
        }
    }
}
package com.bjsxt.net.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端    要先启动服务器端
 */
public class Test {
    public static void main(String[] args) throws IOException {
        //(1)创建Socket对象
        Socket client = new Socket("127.0.0.1",9999);
        //(2)获取输出流 向服务器端发送信息
        OutputStream os = client.getOutputStream();
        os.write('a');
        //(3)获取输入流  获取服务器端发来的反馈
        InputStream is = client.getInputStream();
        byte[] buf = new byte[1024];  //中转站
        int len=0;  //读到的字节个数
        while((len=is.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
        //(4)关闭流
        if(is!=null){
            is.close();
        }
        if(os!=null){
            os.close();
        }
        if(client!=null){
            client.close();
        }
    }
}

第一次运行的时候 好像跳出了什么拦截信息 没注意看 顺手关了 然后发现发现运行结果如下

image.png


image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2538楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2540楼

package com.bjsxt.IO;

import java.io.*;
import java.util.function.BiFunction;

/**
 * 实现将某个目录所有内容拷贝到另一个目录下
 * BufferedInputStream  BufferedOutputStream 提高效率
 *FileInputStream  FileOutputStream  数据源是文件
 *步骤:(1)复制一个文件  写一个copyFile()
 *     (2)复制指定目录下的所有文件   用循环,是文件就复制
 *     (3)复制指定目录下的所有文件及子目录下的所有文件    递归
 */
public class testCopy {
    public static void main(String[] args) {
        File srcDir = new File("d:\\java\\源码(1)");
        File targetDir = new File("d:\\java\\copy源码(1)");
        copyDir(srcDir,targetDir);
    }


    //复制一个文件的方法
    public static void copyFile(File srcFile, File targetFile){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //(1)提高读取效率 从数据源
            bis = new BufferedInputStream(new FileInputStream(srcFile));
            //(2)提高写入效率 写到目的地
            bos = new BufferedOutputStream(new FileOutputStream(targetFile));
            //(3)边读边写
            byte[] buf = new byte[1024];  //中转站
            int len = 0;  //用于存储读到的字节个数
            while((len=bis.read(buf)) != -1){
                bos.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //(4)关闭
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    //复制指定目录下文件及其子目录下的文件
    public static void copyDir(File srcDir , File targetDir){
        if(!targetDir.exists()){
            targetDir.mkdir();    //若targetDir不存在 则创建
        }
        File[] files = srcDir.listFiles();   //获取指定目录下的所有File对象

            for(File file : files){
                if(file.isFile()){  //是文件 就复制
                    copyFile(new File(srcDir+":/"+file.getName()),new File(targetDir+":/"+file.getName()));
                }else{ //不是文件(是目录) 递归
                    copyDir(new File(srcDir+":/"+file.getName()),new File(targetDir+":/"+file.getName()));
                }
            }




        }

    }


image.png


对照老师的源码看了半天没看出哪里有问题。。如果我加上非空判断,运行出来的结果就是创建了一个空文件夹,但是里面的内容都没有复制过来



image.png

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2541楼

package com.bjsxt.IO;

import java.io.Serializable;

public class Stu implements Serializable {
    private static final long serialVersionUID = 9040001675273134854L;  //具备可序列化和反序列化的能力
    private String name;
    private int age;
    public static String schoolName;   //static 不会被序列化
    transient String pwd;    //transient 不会被序列化



    public Stu() {
    }

    public Stu(String name, int age, String pwd) {
        this.name = name;
        this.age = age;
        this.pwd = pwd;
    }

    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 String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", pwd=" + pwd +", schoolName="+schoolName+
                '}';
    }
}


Classes类

package com.bjsxt.IO;

import com.bjsxt.collection.Student;

import java.io.Serializable;
import java.util.ArrayList;

public class Classes implements Serializable {
    private static final long serialVersionUID = -7097903749347750461L;
    private String className;
    private ArrayList<com.bjsxt.IO.Stu> al;  //要将classes写入磁盘 那Student也得是可序列化和可反序列化的

    public Classes(String className, ArrayList<Stu> al) {
        this.className = className;
        this.al = al;
    }

    public Classes() {
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public ArrayList<Stu> getAl() {
        return al;
    }

    public void setAl(ArrayList<Stu> al) {
        this.al = al;
    }

    @Override
    public String toString() {
        return "classes{" +
                "className='" + className + '\'' +
                ", al=" + al +
                '}';
    }
}


读写

package com.bjsxt.IO;

import java.io.*;
import java.util.ArrayList;

public class TestObjectOutputStream3 {
    public static void main(String[] args) {
        write();
        read();
    }

    //写对象
    public static void write(){
        //创建学生对象
        ArrayList<Stu> al = new ArrayList<>();
        Stu stu1 = new Stu("zhang",20,"123456");
        Stu stu2 = new Stu("yang",22,"654321");
        Stu stu3 = new Stu("chen",23,"888888");
        al.add(stu1);
        al.add(stu2);
        al.add(stu3);

        //创建班级对象
        Classes cl = new Classes("one", al);

        //创建对象输出流
        ObjectOutputStream oos = null;
        try {
            oos = null;
            oos = new ObjectOutputStream(new FileOutputStream("d:/java/classes.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(oos!= null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //读取
    public static void read(){
        ObjectInputStream ois = null;
        try {
            ois = null;
            ois = new ObjectInputStream(new FileInputStream("d:/java/classes.txt"));
            Classes cl = (Classes)ois.readObject();
            System.out.println(cl.getClassName()+"\t"+cl.getAl());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}


运行结果

image.png



查了一下说是EOFException是意外到达文件或流的末尾抛出的异常,解决方法有点没看懂。。看不出来52行哪里有问题。。第九行是调用read()。

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2543楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2545楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2546楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2547楼

package com.bjsxt.ObjectLiu;

import java.io.*;

public class TestStudent {
    public static void main(String[] args) {
          //write();//调用写对象的方法
        read();//调用读对象的方法
    }
    public static void write(){
        ObjectOutputStream oos= null;
        try {
            //oos = null;
            oos=new ObjectOutputStream(new FileOutputStream("E:\\student.txt"));
            Student stu=new Student("marry",20,"888888");
            Student.schoolName="JN校区";
            oos.writeObject(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public static void read(){
        ObjectInputStream ois= null;
        try {
            ois = null;
            ois=new ObjectInputStream(new FileInputStream("E:\\student.txt"));
            Student stu = (Student) ois.readObject();
            System.out.println(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
//打印结果:Student{name='marry', age=20, pwd='null'}schoolName=null
//杜老师,为什么我的打印结果含有单引号,而讲课的老师的打印结果是没有单引号的,麻烦您帮忙看一下,谢谢您


JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2548楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637