会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132440个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2536楼

package cn.itbaizhan;
//客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

    public static void main(String[] args) throws IOException {
        Socket client=new Socket("192.168.0.103",888);
        OutputStream os=client.getOutputStream();
        InputStream is=client.getInputStream();
        os.write("aaa".getBytes());
        os.flush();
        int len=0;
        byte[] bt1=new byte[1024];
        while((len=is.read(bt1))!=-1){
            System.out.println(new String(bt1,0,len));
            if(len<bt1.length){//
                break;
            }
        }
        if(is!=null){
            is.close();
        }
        if(os!=null){
            os.close();
        }
        if(client!=null){
            client.close();
        }



    }

}
========
package cn.itbaizhan;
//服务器端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(888);
        Socket client=serverSocket.accept();//监听
        InputStream is=client.getInputStream();
        byte[] bt2=new byte[1024];
        int len=0;
        while ((len=is.read(bt2))!=-1){
            System.out.println(new String(bt2,0,len));
        }
        OutputStream os=client.getOutputStream();
        os.write("bbb".getBytes());
        os.flush();
        if(os!=null){
            os.close();
        }
        if(is!=null){
            is.close();
        }
        if(client!=null){
            client.close();
        }
        if(serverSocket!=null){
            serverSocket.close();
        }

    }
}

blob.png

blob.png

老师,为什么我这里服务器端能读数据,客户端读不了呢

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

老师晚上好!我用老师的方法了,代码如下,但是读出来还是乱码,!老师能否帮我分析一下!

package com.bjsxt;

import java.io.*;

public class DataInputStream01 {
    public static void main(String[] args) throws IOException {
     //  write();
       read();
    }
    public static void read() throws IOException {
        //读数据的方法
        //(1)找数据源
        FileInputStream fis = new FileInputStream("D://baizhan//data.txt");
        //(2)提高读取速率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //(3)处理java的基本数据类型和字符串
        DataInputStream dis = new DataInputStream(bis);
     //   DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream("D:\\baizhan\\data.txt")));
        //(4)读数据--(读数据的顺醋要与写的顺序一致。)
        dis.readInt();
        dis.readDouble();
        dis.readChar();
        dis.readUTF();
        //(5)关闭
        dis.close();
    }
    public static void write() throws IOException{
        //(1)目的地
        FileOutputStream fos = new FileOutputStream("D:\\baizhan\\data.txt");
        //缓冲流提高写入速度
        BufferedOutputStream bos  =new BufferedOutputStream(fos);
        //(3)数据流,增加对java基本数据类型和String的处理
        DataOutputStream  dos  = new DataOutputStream(bos);
        //(4)写入数据
        dos.writeInt(98);
        dos.writeDouble(13.5);
        dos.writeChar('d');
        dos.writeUTF("hello world!");
        //(5)关闭流
        if (dos!=null){
            dos.close();
        }
    }
}


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

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

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 基础深化和提高/网络编程(旧) 2543楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2545楼

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 流技术(旧) 2546楼

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 流技术(旧) 2548楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2550楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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