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

image.png

package com.bjsxt.array;

/**
 * 使用javabeen和数组存储表格信息。
 * 这个方式非常重要!!!!!!!
 */
public class Test09 {
    public static void main(String[] args) {
    emp emp0=new emp(1001,"闫小一",25,"程序员","2015-9-09");
    emp emp1=new emp(1002,"闫小二",21,"程序员","2019-9-09");
    emp emp2=new emp(1003,"闫小三",28,"程序员","2017-9-09");
    emp emp3=new emp(1004,"闫小四",24,"程序员","2013-9-09");
    emp[] emps=new emp[4];
    emps[0]=emp0;
    emps[1]=emp1;
    emps[2]=emp2;
    emps[3]=emp3;
    //遍历数组
     for(int j=0;j< emps.length;j++);

        System.out.println(emps[j]);

    }
   class emp{
        private int id;
        private String name;
        private int age;
        private String job;
        private String hiredate;


        public emp(){}
        public emp(int id, String name, int age, String job, String hiredate) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.job = job;
            this.hiredate = hiredate;
        }

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

        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 String getJob() {
            return job;
        }

        public void setJob(String job) {
            this.job = job;
        }

        public String getHiredate() {
            return hiredate;
        }

        public void setHiredate(String hiredate) {
            this.hiredate = hiredate;
        }
    }



}


JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 14761楼
WEB前端全系列/第十阶段:Nodejs编程模块/Node.js基础 14762楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-使用VSFTPD_Nginx完成商品新增 14763楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 14764楼
WEB前端全系列/第十阶段:Nodejs编程模块/Node.js基础 14765楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-使用VSFTPD_Nginx完成商品新增 14767楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 14768楼
Python 全系列/第一阶段:Python入门/编程基本概念 14769楼

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 流技术(旧) 14770楼
Python 全系列/第一阶段:Python入门/函数和内存分析 14772楼
WEB前端全系列/第五阶段:前后端交互/网络请求AJAX 14773楼
JAVA 全系列/第四阶段:网页编程和设计/HTML5(旧) 14774楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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