A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiaolongwang 中级黑马   /  2015-12-5 17:03  /  911 人查看  /  3 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

1.Exception
        A:throws和throw的区别
                throws: 用在方法申明后面,跟的是异常类名,可以跟多个异常类名,用逗号隔开
                                表示抛出异常,由该方法的调用者来处理
                                表示出现异常的一种可能性,并一不定会发生这些异常
                throw:        用在方法体内,跟的是异常对象名,只能抛出一个异常对象名
                                表示抛出异常,有方法体内的语句处理
                                执行throw则一定抛出了某种异常
                               
        B:final、finally、finalize的区别
                final: 最终的意思,可以修饰类、成员变量、成员方法
                                被修饰的类不能被继承、被修饰的变量是常量、被修饰的方法不能被重写
                finally:是异常处理的一部分,用于释放资源       
                                在执行到finally之前若JVM未停止,则finally一定会被执行
                finalize:是Object类的一个方法,用于垃圾回收

2.File
        A:构造方法
                File(String pathname):根据一个路径得到File对象
                File(String parent,String child):根据一个目录和一个子文件/目录得到File对象
                File(File parent,String child):根据一个父File对象和一个子文件/目录得到File对象
        B:常用成员方法
                a:创建功能
                        public boolean createNewFile():创建文件,如果文件存在,则不执行任何操作
                        public boolean mkdir():创建文件夹,如果该文件夹存在,则不执行创建操作
                        public boolean mkdirs():创建多级文件夹,如果父文件夹不存在,则同时创建父文件夹
                b:删除功能
                        public boolean delete():注意:文件夹为空文件夹的时候才能删除
                c:重命名/剪切功能
                        public boolean renameTo(File dest):路径相同为重命名,路径不同为重命名并剪切
                d:判断功能
                        public boolean isDirectory():判断是否为目录
                        public boolean isFile():判断是否为文件
                        public boolean exists():判断是否存在
                        public boolean canRead():判断是否可读
                        public boolean canWrite():判断是否可写
                        public boolean isHidden():判断是否隐藏
                e:获取功能
                        public String[] list():获取指定目录下所有文件和文件夹的名称数组
                        public File[] listFiles():获取指定目录下的所有文件和文件夹的File数组
                        public String getAbsolutePath():获取绝对路径
                        public String getPath():获取相对路径
                        public String getName():获取名称
                        public long length():获取长度,字节数
                        public long lastModified():获取最后一次修改的时间,毫秒值
                f:文件名称过滤器
                        public String list(FilenameFilter filter)
                        public File[] listFiles(FilenameFilter filter)
                       
        C:判断E盘中是否存在后缀名为".jpg"的文件,有就输出文件名称
                a:普通方法
                        File file = new File("E:\\");
                        File[] fileArray = file.listFiles();
                        for(File f : fileArray){
                                if(f.isFile()){
                                        if(f.getName().endsWith(".jpg")){
                                                System.out.println(f.getName());
                                        }
                                }
                        }
                       
                b:使用文件名称过滤器
                        File file = new File("E:\\");
                        String[] strArray = file.list(new FilenameFilter(){
                                public boolean accept(File dir,String name){
                                        return new File(dir,name).isFile() && name.endsWith(".jpg");
                                }       
                        });
                        for(String s : strArray){
                                System.out.println(s);
                        }
                       
        D:批量修改文件名称
                修改前:E:\评书\三国演义\三国演义_001_[评书网]_桃园三结义.avi
                修改后:E:\评书\三国演义\001_桃园三结义.avi
                //封装目录
                File folder = new File("E:\\评书\\三国演义");
                File[] fileArray = folder.listFiles();
                for(File file : fileArray){
                        //获得名称,配合索引截取字符串,拼接生成新名称
                        String oldName = file.getName();
                        int index1 = oldName.indexOf("_");
                        String str1 = oldName.substring(index1 + 1,index2 + 4);
                        ing index2 = oldName.lastIndexOf("_");
                        String str2 = oldName.substring(index2);
                        String newName = str1.concat(str2);
                        //封装新目录
                        File newFile = new File(folder,newName);
                        //重命名
                        file.renameTo(newFile);
                }
       
3.递归
        A:不死神兔问题(第二是个月兔子的对数?)
                a:操作数组实现
                        int[] arr = new int[20];
                        int[0] = 1;
                        int[1] = 1;
                        for(int x = 2;x < arr.length; x++){
                                arr[x] = arr[x -1] + arr[x - 2];
                        }
                        int result = arr[19];
                       
                b:普通实现
                        int a = 1;
                        int b = 1;
                        for(int x = 0;x < 18; x++){
                                int temp = a;
                                a = b;
                                b = temp + a;
                        }
                        int result = b;
                       
                c:递归实现
                        public atatic int getNum(int i){
                                if(i == 1 || i == 2){
                                        return 1;
                                }else{
                                        return getNum(n - 1) + getNum(n - 2);
                                }
                        }
               
        B:输出指定目录(E:\Java)下指定后缀名文件(.java)的绝对路径
                File folder = new File("E:\\JavaSE");
                getAllJavaFilePaths(folder);
               
                public static void getAllJavaFilePaths(File folder){
                        File[] fileArray = folder.listFiles();
                        for(File file : fileArray){
                                if(file.isDirectory()){
                                        getAllJavaFilePaths(file);
                                }else{
                                        if(file.getName().endsWith(".java")){
                                                System.out.println(file.getAbsolutePath());
                                        }
                                }
                        }
                       
                }
               
        C:删除非空目录
                File folder = new File("demo");
                deleteFolder(folder);
               
                private static void deleteFolder(File folder){
                        File[] fileArray = folder.listFiles();
                        if(fileArray != null){
                                for(File file : fileArray){
                                        if(file.isDirectory()){
                                                deleteFolder(file);
                                        }else{
                                                System.out.println(file.getName());
                                                file.delete();
                                        }
                                }
                                System.out.prinltn(folder.getName());
                                folder.delete();
                        }
                       
                }
       
4.字节流
        A:创建字节输出流对象做了几件事情
                a:调用系统功能创建文件
                b:创建字节输出流对象
                c:把字节输出流对象指向这个文件
               
        B:字节流复制音频的四种方式
                method("E:\\abc.mp3","D:\\abc.mp3");
               
                a:基本字节流一次读写一个字节
                public static void method(String srcString,String destString) throws IOException {
                        FileInputStream fis = new FileInputStream(srcString);
                        FileOutputStream fos = new FileOutputStream(destString);
                        int by = 0;
                        while((by = fis.read()) != -1){
                                fos.write(by);
                        }
                        fos.close();
                        fis.close();
                }
               
                b:基本字节流一次读写一个字节数组
                public static void method(String srcString,String destString) throws IOException {       
                        FileInputStream fis = new FileInputStream(srcString);
                        FileOutputStream fos = new FileOutputStream(destString);
                        byte[] bys = new byte[1024];
                        int len = 0;
                        while((len = fis.read(bys)) != -1){
                                fos.write(byte,0,len);
                        }
                        fos.close();
                        fis.close();
                }
               
                c:高效字节流一次读写一个字节
                public static void method(String srcString,String destString) throws IOException {
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
                        int by = 0;
                        while((by = bis.read()) != -1){
                                bos.write(by);
                        }
                        bos.close();
                        bis.close();
                }
               
                d:高效字节流一次读写一个字节数组
                public static void methed(String srcString,String destString) throws IOException {
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
                        byte[] bys = new byte[1024];
                        int len = 0;
                        while((len = bis.read(bys)) != -1){
                                bos.write(bys,0,len);
                        }
                        bos.close();
                        bis.close();
                }
               
5.字符流
        A:字节流复制文本文件的5中方式
                method("E:\\aaa.txt","D:\\bbb.txt");
               
                a:基本字符流一次读写一个字符
                private static void method(String srcString,String destString) throws IOException{
                        FileReader fr = new FileReader(srcString);
                        FileWriter fw = new FileWriter(destString);
                        int ch = 0;
                        while((ch = fr.read()) != -1){
                                fw.write(ch);
                        }
                        fw.close();
                        fr.close();
                }
               
                b:基本字符流一次读写一个字符数组
                private static void method(String srcString,String destString) throws IOException {
                        FileReaer fr = new FileReader(srcString);
                        FileWriter fw = new FileWriter(destString);
                        char[] chs = new char[1024];
                        int len = 0;
                        while((len = fr.read(chs)) ! = -1){
                                fw.write(chs,0,len);
                        }
                        fw.close();
                        fr.close();
                }
               
                c:字符缓冲流一次读写一个字符
                private static void method(String srcString,String destString) throws IOException{
                        BufferedReader br = new BufferedReader(new FileReader(srcString));
                        BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
                        int ch = 0;
                        while((ch = br.read()) != -1){
                                bw.write(ch);
                        }
                        bw.close();
                        br.close();
                }
               
                d:字符缓冲流一次读写一个字符数组
                private static void method(String srcString,String destString) throws IOException{
                        BufferedReader br = new BufferedReader(new FileReader(srcString));
                        bufferedWriter bw = new BufferedWriter(new FileWriter(destString));
                        char[] chs = new char[1024];
                        int len = 0;
                        while((len = br.read(chs)) != -1){
                                be.write(chs,0,len);
                        }
                        bw.close();
                        br.close();
                }
               
                e:字符缓冲流一次读写一个字符串(一行)
                private static void method(String srcString,String destString) throws IOException{
                        BufferedReader br = new BufferedReader(new FileReader(srcString));
                        BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
                        String line = null;
                        while((line = br.readLine()) != null){
                                bw.write(line);
                                bw.newLine();
                                bw.flush();
                        }
                        bw.close();
                        br.close();
                }

3 个回复

倒序浏览
谢谢分享
回复 使用道具 举报
学习了
回复 使用道具 举报
学习他人经验,助自我成长!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马