fanjialong 12 horas atrás
pai
commit
d36ade07aa

+ 11 - 0
java-base-project10/day21/day21.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 7 - 0
java-base-project10/day21/src/com/sf/_01_异常入门/demo1/MathUtils.java

@@ -0,0 +1,7 @@
+package com.sf._01_异常入门.demo1;
+
+public class MathUtils {
+    public static int divide(Integer a, Integer b){
+        return a/b;
+    }
+}

+ 51 - 0
java-base-project10/day21/src/com/sf/_01_异常入门/demo1/Student.java

@@ -0,0 +1,51 @@
+package com.sf._01_异常入门.demo1;
+
+public class Student {
+    private String name;
+    private int age;
+
+
+    public Student() {
+    }
+
+    public Student(String name, int age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public int getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public String toString() {
+        return "Student{name = " + name + ", age = " + age + "}";
+    }
+}

+ 64 - 0
java-base-project10/day21/src/com/sf/_01_异常入门/demo1/Test.java

@@ -0,0 +1,64 @@
+package com.sf._01_异常入门.demo1;
+
+import java.util.Scanner;
+
+public class Test {
+
+    /**
+     * 需求: 要求大家设计一个工具类叫MathUtils
+     * 这里有一个方法divide(Integer a , Integer b) a/b;
+     *
+     * 在main 方法在控制台当中录入a 和b
+     * 调用divide 方法,  这个方法前后都有代码 , 验证a b 都为1 的情况 和 a =1 b =0 等0的情况
+     * 要提前处理异常
+     * @param args
+     */
+    public static void main(String[] args) {
+        /**
+         * try{
+         *     可以能会出现异常代码
+         * }catch(异常类型 e){
+         *     当出现了对应异常类型 解决方案
+         * }
+         */
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入第一个数字");
+        int num1 = scanner.nextInt();
+        System.out.println("请录入第二个数字");
+        int num2 = scanner.nextInt();
+
+        try {
+            System.out.println("前面正常代码");
+            System.out.println("前面正常代码");
+            System.out.println("前面正常代码");
+            MathUtils.divide(num1, num2);
+            // 创建一个Student stu= null     stu.getAge();
+            Student student = null;
+            System.out.println(student.getName());
+            System.out.println("后续正常代码");
+            System.out.println("后续正常代码");
+            // 只能够去处理除数不为0 的异常 没有办法处理其他异常
+//        }catch (ArithmeticException e){
+//            System.out.println("除数不能为0 请重新输入");
+//        }catch (NullPointerException e){
+//            System.out.println("数据不能为空");
+//        }
+        }catch (Exception e){
+            /**
+             * toString : 打印异常类型: 异常原因
+             */
+//            System.out.println(e.toString())
+//            ;
+            /**
+             * getMessage(); 打印异常原因
+             */
+//            System.out.println(e.getMessage());
+            e.printStackTrace();
+            System.out.println("出现了异常");
+        }
+
+
+        System.out.println("后续代码123");
+
+    }
+}

+ 51 - 0
java-base-project10/day21/src/com/sf/_01_异常入门/demo2/LoginMessage.java

@@ -0,0 +1,51 @@
+package com.sf._01_异常入门.demo2;
+
+public class LoginMessage {
+    private boolean success;
+    private String msg;
+
+
+    public LoginMessage() {
+    }
+
+    public LoginMessage(boolean success, String msg) {
+        this.success = success;
+        this.msg = msg;
+    }
+
+    /**
+     * 获取
+     * @return success
+     */
+    public boolean isSuccess() {
+        return success;
+    }
+
+    /**
+     * 设置
+     * @param success
+     */
+    public void setSuccess(boolean success) {
+        this.success = success;
+    }
+
+    /**
+     * 获取
+     * @return msg
+     */
+    public String getMsg() {
+        return msg;
+    }
+
+    /**
+     * 设置
+     * @param msg
+     */
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public String toString() {
+        return "LoginMessage{success = " + success + ", msg = " + msg + "}";
+    }
+}

+ 39 - 0
java-base-project10/day21/src/com/sf/_01_异常入门/demo2/Test.java

@@ -0,0 +1,39 @@
+package com.sf._01_异常入门.demo2;
+
+public class Test {
+    /**
+     * LoginMessage这是个javaBean 类
+     * success(boolean)   message(String)
+     *
+     * 做登录
+     * String username = admin;
+     * String password = 123123;
+     * 账号等于admin  密码等于123  登录成功 穿管LoginMessage(true,登录成功)
+     * username.equalas("admin")&& password.equals("123")
+     * 账号不等于admin 密码不等于123 返回 LoginMessage(false,登录失败)
+     *
+     * 如果代码出现了异常  LoginMessage(false, 登录失败原因)
+     * 并且要出现异常在控制台当中打印出来 e.printstack..
+     */
+
+    public static void main(String[] args) {
+        String username = null;
+        String password ="123123";
+        try{
+            login(username,password);
+        }catch (Exception e){
+            System.out.println(new LoginMessage(false,"账号密码不能为空"));
+            e.printStackTrace();
+        }
+
+    }
+    public static void login(String username,String password){
+
+        if(username.equals("admin") &&password.equals("123")){
+            System.out.println(new LoginMessage(true,"登录成功"));
+        }else{
+            System.out.println(new LoginMessage(false,"登录失败"));
+        }
+    }
+
+}

+ 41 - 0
java-base-project10/day21/src/com/sf/_02_finally/Test.java

@@ -0,0 +1,41 @@
+package com.sf._02_finally;
+
+public class Test {
+    /**
+     * try{
+     *
+     * }catch (异常类型 e ){
+     *
+     * }finally{
+     *     表示最终一定会执行的
+     * }
+     *
+     * 什么情况下会用到finally , 一般在后续实际开发中,像关闭资源操作会用finally
+     */
+
+    public static void main(String[] args) {
+        /**
+         * 在try 当中写一个int num1=1/0 int num= 1/1  , try 后面写一行代码
+         * 在catch 当中捕捉异常, 打印异常信息
+         * 在finally 当中打印代码一定执行
+         *
+         * 就算try 当中有return finally代码也一定会执行
+         *
+         * try 当中有return finally 中也有return  返回是哪里return
+         */
+
+        System.out.println(method());
+    }
+
+    public static int method(){
+        try{
+            int num = 1/1;
+            return num;
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            System.out.println("最终一定执行的代码");
+            return 123;
+        }
+    }
+}

+ 29 - 0
java-base-project10/day21/src/com/sf/_03_throws/Test.java

@@ -0,0 +1,29 @@
+package com.sf._03_throws;
+
+public class Test {
+    public static void main(String[] args) throws ArithmeticException {
+        method1(1,0);
+    }
+
+
+//    public static void method1(int num, int num1){
+//        try{
+//            method(num,num1);
+//        }catch (Exception e){
+//            e.printStackTrace();
+//        }
+//        System.out.println("后续代码");
+//
+//    }
+
+    public static void method1(int num, int num1) throws ArithmeticException{
+        method(num,num1);
+    }
+
+    /**
+     * 方法在调用的时候可能会出现除数不为0 的异常
+     */
+    public static void method(int num  , int num1) throws NullPointerException{
+        System.out.println(num / num1);
+    }
+}

+ 20 - 0
java-base-project10/day21/src/com/sf/_03_throws/Test1.java

@@ -0,0 +1,20 @@
+package com.sf._03_throws;
+
+public class Test1 {
+    /**
+     * 定义一个打印数组元素的方法,方法声明抛出异常
+     */
+    public static void printArr(int[] arr) throws NullPointerException {
+        for (int i : arr) {
+            System.out.println(i);
+        }
+    }
+
+    public static void main(String[] args) throws NullPointerException {
+        int[] arr = null;
+        printArr(arr);
+
+        System.out.println("后续代码");
+
+    }
+}

+ 32 - 0
java-base-project10/day21/src/com/sf/_04_throw/Test.java

@@ -0,0 +1,32 @@
+package com.sf._04_throw;
+
+public class Test {
+    /**
+     * throw 在方法内部当中抛出异常
+     * 通常情况是用做方法参数校验
+     * @param args
+     */
+    public static void main(String[] args)    {
+//        try{
+//            login(null,null);
+//            System.out.println("登录成功");
+//        }catch (Exception e){
+//            System.out.println(e.getMessage());
+//            e.printStackTrace();
+//        }
+        printArr1(null);
+    }
+
+
+
+    public static void printArr1(int[] arr){
+
+//        if(arr == null){
+//            throw new IOException("xx");
+//        }
+        for (int i : arr) {
+            System.out.println(i);
+        }
+    }
+
+}

+ 43 - 0
java-base-project10/day21/src/com/sf/_05_异常分类/Test.java

@@ -0,0 +1,43 @@
+package com.sf._05_异常分类;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+public class Test {
+
+    public static void main(String[] args) {
+        /**
+         * 当我们在调用一个方法的时候
+         * 这个方法的内部如果抛出了编译时异常我们就必须要进行处理
+         * 如果抛出的运行时异常就可以不做处理
+         *
+         * 什么异常是编译时期异常什么异常是运行时异常
+         */
+        method1(null);
+
+        try {
+
+            method2(null);
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 编译时期异常
+     * FileNotFoundException
+     */
+
+    public static void method2(Integer num) throws FileNotFoundException {
+        FileInputStream fileInputStream = new FileInputStream("");
+    }
+    /**
+     * 运行时期异常
+     * FileNotFoundException
+     */
+    public static void method1(Integer num)  throws RuntimeException{
+        if(num == null){
+            throw new RuntimeException("运行是异常");
+        }
+    }
+}

+ 15 - 0
java-base-project10/day21/src/com/sf/_06_自定义异常/LogicException.java

@@ -0,0 +1,15 @@
+package com.sf._06_自定义异常;
+
+/**
+ * 让自己写的类去继承Exception / RuntimeException
+ * 继承Exception 编译时期异常
+ * 继承RuntimeException 运行时期异常
+ */
+public class LogicException extends RuntimeException{
+    public LogicException() {
+    }
+
+    public LogicException(String message) {
+        super(message);
+    }
+}

+ 52 - 0
java-base-project10/day21/src/com/sf/_06_自定义异常/Test.java

@@ -0,0 +1,52 @@
+package com.sf._06_自定义异常;
+
+public class Test {
+    public static void main(String[] args) {
+        String username= "admin";
+        String password = "123";
+        String code = "abc1";
+        try{
+            login(username,password,code);
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * String username  String password  String code;
+     *
+     * 需要再login 抛出一LogicException ,在主方法使用trycatch 处理捕捉异常 打印信息
+     *
+     * 后续在main 调用login()
+     * 如果用户名为 提醒用户名不能为空
+     * 如果密码为空提醒用户密码不能为空
+     * 如果验证码为空提醒验证码不能为空
+     * 如果输入验证码不能为 abc1 ,就提醒验证码错误
+     * 如果账号不能admin, 密码不为123 就提心他账号密码错误
+     * 如果比较成功提醒 登录成功
+     */
+    public static void login(String username,String password,String code){
+        // 1 判断账号是否为空
+        if(username == null){
+            throw new LogicException("账号不能为空");
+        }
+        // 2 判断密码是否为空
+        if(password == null){
+            throw new LogicException("密码不能为空");
+        }
+        // 3 验证码是否为空
+        if(code == null){
+            throw new LogicException("验证码不能为空");
+        }
+        // 4 判断验证码是否为abc1
+        if(!code.equals("abc1")){
+            throw new LogicException("验证码错误");
+        }
+        // 5 判断账号是否为admin 密码是否为123
+        if("admin".equals(username) && "123".equals(password)){
+            System.out.println("登录成功");
+        }else{
+            throw new LogicException("账号密码错误");
+        }
+    }
+}

+ 83 - 0
java-base-project10/day21/src/com/sf/_07_file/Test.java

@@ -0,0 +1,83 @@
+package com.sf._07_file;
+
+import java.io.File;
+import java.io.IOException;
+
+public class Test {
+
+    public static void main(String[] args) throws IOException {
+        /**
+         * File file = new File("创建文件路径");
+          */
+//        File file = new File("a.txt");
+//        /**
+//         * 创建文件
+//         * 创建文件时候你目录一定要存在如果不存在报错
+//         * createNewFile();
+//         */
+//        file.createNewFile();
+
+        File file1 = new File("D:\\abc");
+        /**
+         * list 查看目录当中内容(文件和目录的)
+         */
+//        String[] list = file1.list();
+//        System.out.println(Arrays.toString(list));
+        /**
+         * listFile 查看目录当中内容(文件和目录的)
+         */
+        File[] files = file1.listFiles();
+        for (File file : files) {
+            System.out.println(file);
+        }
+
+//        /**
+//         * 创建目录
+//         * file.mkdirs
+//         */
+//        file1.mkdirs();
+
+        /**
+         * 删除文件/目录
+         */
+//        file1.delete();
+
+        /**
+         * 获取文件的名字
+         * getName()
+//         */
+//        System.out.println(file.getName());
+//
+        /**
+         * 获取文件的路径
+         * getPath();
+         */
+//        System.out.println(file.getPath());
+//
+//        /**
+//         * 获取上级目录
+//         * getParentFile();
+//         */
+//        System.out.println(file.getParentFile());
+        /**
+         * 获取绝对路径
+         * getAbsolutePath();
+         */
+//        System.out.println(file.getAbsolutePath());
+
+
+        /**
+         * 判断文件或者目录是否存在
+         * exists():
+         */
+//        System.out.println(file.exists());
+
+        /**
+         * isFile(); 是否是一个文件
+         * isDirectory(); 是否是一个目录
+         */
+//        System.out.println(file.isFile());
+//        System.out.println(file.isDirectory());
+
+    }
+}

+ 44 - 0
java-base-project10/day21/src/com/sf/_07_file/Test1.java

@@ -0,0 +1,44 @@
+package com.sf._07_file;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Scanner;
+
+public class Test1 {
+    /**
+     * 需求1
+     * 在控制台当中录入文件名字
+     * 要求在D盘下面abc1 目录去创建出来响应文件
+     * 判断文件是否存在,如果不存在就创建
+     * 如果存在就不创建
+     *
+     * 需求2
+     * 现在在abc目录中有 a 目录  b目录    a.txt b.txt
+     * a 目录中里面有c.txt  d.txt  b目录中f.txt g.txt
+     * 现在要在控制台当中打印出来abc 目中中所有的文件名
+     *
+     *
+     *
+     * 需求3 查询制定目录下有几个txt 文件
+     */
+
+    public static void main(String[] args) throws IOException {
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入文件名字");
+        String fileName = scanner.next();
+        String baseDir = "D:\\abc1\\";
+        // 要创建目录
+        File file1 = new File(baseDir);
+        // 要创建文件
+        File file = new File(baseDir+fileName);
+        // 判断文件是否存在
+        if(!file.exists()){
+            //创建目录
+            // d:\abc1\a.txt   getName a.txt  indexOf a.txt
+            file1.mkdirs();
+//            //创建文件
+            file.createNewFile();
+        }
+
+    }
+}

+ 47 - 0
java-base-project10/day21/src/com/sf/_07_file/Test2.java

@@ -0,0 +1,47 @@
+package com.sf._07_file;
+
+import java.io.File;
+
+public class Test2 {
+    private static Integer count =0;
+    public static void main(String[] args) {
+        File file = new File("D:\\abc");
+        queryFile(file);
+        System.out.println("一共有"+ count+"个txt文件");
+    }
+
+    /**
+     * 第一次调用 file("D:\\abc")
+     * 第二次调用 file(D: \abc\a)
+     * @param file
+     */
+    public static void queryFile(File file){
+        // 1 调用file 的listFile 方法列举file 当中所有内容
+        File[] files = file.listFiles();
+        for (File f : files) {
+            // 判断当前f 是一个文件还是目录
+            // 如果是一个文件直接打印文件名字
+            if(f.isFile()){
+                String ext = getFileExt(f.getName());
+                if("txt".equals(ext)){
+                    //如果后缀是txt 类型说明他就是文件
+                    count++;
+                }
+                System.out.println("文件名字为:"+ f.getName());
+            }
+            // 如果是目录需要去再次去列举目录的内容
+            if(f.isDirectory()){
+//                System.out.println(f.getAbsolutePath());
+                // D: \abc\a
+                File ff = new File(f.getAbsolutePath());
+                queryFile(ff);
+            }
+        }
+    }
+    public static String getFileExt(String fileName){
+        // a.txt   a.jpg  a.zip
+        int index = fileName.lastIndexOf(".");
+        return fileName.substring(index+1);
+
+    }
+}