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

public class Test {
	public static void main(String[] args) {
//		File srcFile = new File("D:\\java10086\\test1.txt");
//		File targetFile = new File("E:\\test1.txt");
//		copyFile(srcFile, targetFile);
		File srcDir = new File("D\\java10086");
		File targetDir = new File("E\\java10086");
		copyDir(srcDir,targetDir);
	}
	public static void copyDir(File srcDir,File targetDir) {
		if (!targetDir.exists()) {
			targetDir.mkdir();//如果目的地的目录不存在,则需要使用File类方法进行创建目录
		}
		File[] files = srcDir.listFiles();//获取指定目录下的所有File对象
		for (File file : files) {
			if (file.isFile()) {
				copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}
			else {
				copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}
		}
	}
	public static void copyFile(File srcFile,File targetFile) {
		//(1)提高读写效率,从数据源
		BufferedInputStream bis =null;
		//(2)提高写入效率,写到目的地
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			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 (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//(4)关闭
			if (bos!=null) {
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bis!=null) {
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		
	}
}

出现空指针异常的错误。不知道哪里错了?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1538楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1542楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 1543楼

老师为什么实用if-else返回的结果是这样的

package com.bjsxt.thread.prosumer2;

/** 
 * <p>Title: TestProducer</p>  
 * <p>Description: 生产者模式</p>  
 * @author xiaoding
 * @date Jul 20, 2020  
 * @version 1.0 
 */
public class TestProducer implements Runnable{
    //功能属性
    private TestSupermarket supermarket;    //超市
    //构造方法
    public TestProducer(TestSupermarket supermarket) {
        this.supermarket = supermarket;
    }
    
    //定义线程体
    @Override
    public void run() {
        for (int i = 0;i<10;i++) {
            if (i % 2 != 0) {
                supermarket.set("旺仔", "小馒头");
            }else {
                supermarket.set("康师傅", "方便面");
            }
        }
    }
}

public class TestSupermarket {
    // 功能属性    
    private String brand;    //品牌
    private String name;    //名称
    private boolean flag;    //开关判断是否需要唤醒线程
    
    //构造方法
    public TestSupermarket() {}
    public TestSupermarket(String brand,String name) {
        this.brand = brand;
        this.name = name;
    }
    
    //get、set
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    //定义一个存储的方法
    public synchronized void set(String brand,String name) {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else {
            this.setBrand(brand);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.setName(name);
            System.out.println("=========生产者产生了:==========" + this.getBrand() + this.getName());
            this.notify();
            flag = true;
        }    
    }
    //定义一个取值的方法
    public synchronized void get() {
        if (flag) {
            System.out.println("消费者取走了:=============" + this.getBrand() + this.getName());
            this.notify();
            flag = false;
        }else {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

public class TestConsumer implements Runnable{
    //功能属性
    private TestSupermarket supermarket;    //超市
    //构造方法
    public TestConsumer(TestSupermarket supermarket) {
        this.supermarket = supermarket;
    }
    
    //定义线程体
    @Override
    public void run() {
        for (int i = 0;i<10;i++) {
            supermarket.get();
        }
    }
}

public class Test {
    public static void main(String[] args) {
        //创建超市类
        TestSupermarket sup = new TestSupermarket();
        //创建生产者类
        TestConsumer p = new TestConsumer(sup);
        //创建消费者类
        TestProducer p2 = new TestProducer(sup);
        
        //创建代理角色线程类,并启动线程
        new Thread(p).start();
        new Thread(p2).start();
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1544楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 1545楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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