案例 汇合到文件 数据排序改进版


//在学生类里增加办法public int getSum(){    return this.chinese + this.math+this.english;}//比拟器排序 匿名外部类TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){    @Override    public int compare(Student o1, Student o2){        //从高到低 2-1        int num = s2.getSum()-s1.getSum();        //主要条件        int num2= num==0?s1.getChinese()-s2.getChinese():num;        int num3= num2==0?s1.getMath()-s2.getMath():num2;        int num4 = num3==0?s1.getName().compareTo(s2.getName()):num3;        renturn num4;    }});//键盘录入学生数据for(int i = 0;i<5;i++){    Scanner sc = new Scanner(System.in);    sout("请录入第“+(i+1)+"个学生信息");    sout("姓名:");    String name = sc.nextLine();    sout("语文问题:");    int chinese = sc.nextLine();    sout("数学问题:");    int math = sc.nextLine();    sout("英语问题:");    int enligsh = sc.nextLine();    //创立学生对象,把键盘录入的数据赋值给对应的学生对象    Student s = new Student();    s.setName(name);    s.setChinese(chinese);    s.setMath(math);    s.setEnligsh(enligsh);    //把学生对象增加到汇合种    ts.add(s);}//目的地创立字符缓冲输入流对象BufferedWriter bw = new BufferedWriter(new FileWriter(fileName:"myCharStream\\ts.txt"));//遍历汇合,失去每一个学生对象for(Student s ;ts){    //把学生对象的数据拼接成指定格局的字符串    StringBuilder sb = new StringBuilder();    sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnligsh()).append(",").append(s.getSum());     //调用字符缓冲输入流对象的办法写数据    bw.write(sb.toString());//不蕴含换行符 所有数据都在同一行    bw.newwLine();    bw.flush();}//开释资源bw.close();


案例 复制单级文件夹

单级文件夹就是该文件夹外部只有文件没有文件夹
需要:把"E:\itcast"这个文件夹复制到模块目录下

//创立数据源目录File对象,门路是"E:\\itcast"File srcFolder = mew File(pathname:"E:\\itcast");//获取数据源目录File对象名称String srcFolderName = srcFolder.getName();//创立目的地目录File对象,门路是模块名+itcast组成(myCharStream\\itcast)File destFolder = mew File(parent:"myCharStream",srcFolderName);//判断目的地目录对应的FIle是否存在,如果不存在就创立if(!destFolder.exists()){    destFolder.mkdir();}//获取数据源所有目录下的File数组File[] listFiles = srcFolder.listFiles();//遍历File数组,失去每一个File对象,该File对象,其实就是数据源文件for(File srcFile : listFiles){    //获取数据源文件File对象名称(mn.jpg)    String srcFileName = srcFile.getName();    //创立目的地File对象,路径名是个目的地目录加上文件名    File destFile = mew File(destFolder,srcFileName);    //复制文件    copyFile(srcFile,destFile);//办法}//字节缓冲流复制文件private static void copyFile(File srcFile,File destFile) throws IOException{    //封装好数据源文件和目的地文件    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));    byte[] bys = new byte[1024];    int len;    while((len=bis.read(bys))!=-1){        bos.write(bys.off:0,len);       }    bos.close();    bis.close();}

案例 复制多级文件夹

多级用递归办法

//创立数据源File对象,门路是"E:\\itcast"File src = mew File(pathname:"E:\\itcast");//创立目的地File对象,门路是F:\\File destFile = new File((pathname:"F:\\");//写办法实现文件夹的复制,参数为数据源File对象和目的地File对象copyFolder(srcFile,destFile);//办法//写办法实现文件夹的复制private static void copyFolder(File srcFile,File destFile) throws IOException{    //判断数据源File是否是目录 如果是目录执行上面    if(srcFile.isDirectory()){        //首先再目的地目录下创立和数据源File名称一样的目录        //获取数据源文件File对象名称(mn.jpg)        String srcFileName = srcFile.getName();        File newFolder = new File(destFile,srcFileName);//创立目的地目录 拼接起来F:\\itcast        //门路封装好就创立        if(!newFolder.exists()){            newFolder.mkdir();        }        //获取数据源File下所有文件或者目录的File数组        File[] listArray = srcFolder.listFiles();        //遍历File数组,失去每一个File对象,        for(File file : listArray){            //这个File可能是文件也可能是目录 把该File作为数据源File对象,递归调用复制文件夹的办法            copyFolder(file,newFolder);          }    }else{        //不是目录阐明是文件间接写入字节流 间接复制        File newFile = new File(destFile,srcFile.getName());//是文件要放在对应的目的地目录下并前面加上文件名字 封装一下目的地的目的地文件 目的地目录加数据源文件名称        copyFile(srcFile,newFile); //调用复制文件办法      }}//字节缓冲流复制文件private static void copyFile(File srcFile,File destFile) throws IOException{    //封装好数据源文件和目的地文件    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));    byte[] bys = new byte[1024];    int len;    while((len=bis.read(bys))!=-1){        bos.write(bys.off:0,len);       }    bos.close();    bis.close();}

复制文件的异样解决


在办法外部进行异样解决

JDK7的改良计划


JDK9的改良计划