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

package com.bjsxt.cai.test;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Calendar 类是一个抽象类,为我们提供了关于日期计算的相关功能,比如:年、月、
 * 日、时、分、秒的展示和计算。
 *
 *  GregorianCalendar(公历) 是Calendar 的一个具体子类,提供了世界上大多数国家/地区
 * 使用的标准日历系统。
 *
 * 菜鸟雷区
 * 注意月份的表示,一月是0,二月是1,以此类推,12 月是11。因为大多数人习惯于使用
 * 单词而不是使用数字来表示月份,这样程序也许更易读,父类Calendar 使用常量来表示月份:
 * JANUARY、FEBRUARY 等等。
 */
public class TestCanlendar {
    public static void main(String[] args) {
        //得到相关日期元素
        GregorianCalendar calendar =new GregorianCalendar(1995,10,26,20,20,18);

        int year =calendar.get(Calendar.YEAR);  //打印:1995
        System.out.println(year+"年");
        int month=calendar.get(Calendar.MONTH);
        System.out.println(month);      //打印 10 ,其实是11月 ,因为月份是0-11,代表1-12月;
        int day =calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(day+"日");        //打印 26

        int day2 =calendar.get(Calendar.DATE);
        System.out.println("第"+day2+"周");       //打印 第几周  26

        //星期几 这里是 :1-7,周日是1, 周一是2......周六是7;
        int date =calendar.get(Calendar.DAY_OF_WEEK);   //这周的第几天;
        System.out.println("这周的第几天:"+date);
        System.out.print("星期几 这里是 :1-7,周日是1, 周一是2......周六是7;");


        //设置日期,
        GregorianCalendar calendar2 =new GregorianCalendar();
        calendar2.set(Calendar.YEAR,2999);
        calendar2.set(Calendar.MONTH,Calendar.FEBRUARY);    //月份数:0-11
        calendar2.set(Calendar.DATE,3);
        calendar2.set(Calendar.HOUR_OF_DAY,10);
        calendar2.set(Calendar.MINUTE,20);
        calendar2.set(Calendar.SECOND,23);
        printCalendar(calendar2);

        //日期计算
        GregorianCalendar calendar3 =new GregorianCalendar(1995,10,26,20,20,18);

        calendar3.add(Calendar.MONTH,-7);   //月份减7
        calendar3.add(Calendar.DATE,7);     //增加7天
        printCalendar(calendar3);

        //日历对象和时间对象转化
        Date d =calendar3.getTime();
        GregorianCalendar calendar4 = new GregorianCalendar();
        calendar4.setTime(new Date());
    }

    private static void printCalendar(Calendar calendar) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int date = calendar.get(Calendar.DAY_OF_WEEK);  //星期几

        String week =""+((date ==0)?"日":date);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        System.out.printf("%d年%d月%d日,星期%s %d:%d:%d\n",year,month,
                day,week,hour,minute,second);


    }

}


运算截图

image.png


日历截图:

image.png


为什么星期对不上??

JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3273楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3275楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3276楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3277楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3278楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3279楼

package com.itbaizhan.li.InternetCoding;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 一对多的服务器
 */
class Msg extends Thread{
    private Socket socket;
    public Msg(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        this.sendMsg();
    }

    private void sendMsg(){
        BufferedReader br=null;
        PrintWriter pw=null;
        try {
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw=new PrintWriter(socket.getOutputStream());
//这行有疑问  while(true){
                pw.println(br.readLine()+"[ok]");
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(pw!=null){
                pw.close();
            }
        }
    }
}
public class EchoServer {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(8888);
            System.out.println("服务器已启动,正在监听。。。");
            while(true){
                Socket socket=serverSocket.accept();
                new Msg(socket).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


    在第31行中未加while循环导致客户端发送消息时,服务端返回了如下的结果图一的结果

我的理解是图三中的while循环死循环了,老师为什么在一对多的服务端类中加了while循环后就不会这样了呢?



图一:

image.png


图二:

image.png



图三:

image.png


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

老师好,这个程序里面小写的file到底是什么意思。我的理解里面就是一个变量的名称,为什么在代码中进行一直使用呢(file.getName)就表示文件名,是有什么其他的意思么?(整个程序是没有错误的,正常运行的)下面是我问题的地方和我的代码

WechatIMG3.jpeg

package com.io;

import java.io.*;



public class TestCopy {

    public static void main(String[]args){
        /**File srcFile =new File("/Users/pain_/Downloads/world.docx");
        File targetFile =new File("/Users/pain_/Desktop/world.docx");
        //调用复制文件的方法
        cpoyFile(srcFile,targetFile);
         */
        File srcDir =new File("/Users/pain_/Downloads/xyyyy");
        File targetDir =new File("/Users/pain_/Desktop/xyyyy");
        copyDir(srcDir,targetDir);
    }
    public static void copyDir(File srcDir,File targetDir){
       //判断目的地文件是否存在通过exists()
        if(!targetDir.exists()){
            targetDir.mkdir();//如果目的地不存在,需要使用File类的mkdir()方法进行创建目录
        }
        //list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组
        //listFiles()方法是返回某个目录下所有文件和目录的绝对路径,返回的是File数组
        File[] files = srcDir.listFiles();
        //加强for循环
        //for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体
        for(File file:files) {
            if (file.isFile()) {

            //是文件复制,目录先不管,需要复制srcDir的目录下的指定文件的具体的文件
            copyFile(new File(srcDir + "/" + file.getName()), new File(targetDir + "/" + file.getName()));
        }else{
            //继续调用该方法,使用递归实现
            copyDir(new File(srcDir + "/" + file.getName()), new File(targetDir + "/" + file.getName()));
        }
        }

    }

    public static void copyFile(File srcFile, File targetFile){
        //提高读取效率,从数据源
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFile));
            //提高读取效率,写到目的地
            bos = new BufferedOutputStream(new FileOutputStream(targetFile));
            //边读边写
            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 {
            //关闭
            if(bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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