Java异常处理和File基本操作
2018-01-29 22:22
411 查看
编译和运行时的异常处理
1.编译异常: 当编译发生异常时系统会强制你去处理 try或者抛出(throw) 2.运行异常(RuntimeException) 1)方法声明上 可以不用throw来表示 2)可以不对运行异常进行处理 3)不处理直接停止程序 处理程序可以继续运行 4)当在方法中抛出运行异常时说明发生该异常 这事需要停止程序 修改代码让程序停下来
1.异常类
class Test{ // 抛出编译异常 public void fun1() throws Exception{ throw new Exception(); } // 抛出运行时异常 public void fun2(){ throw new RuntimeException("运行异常"); } public double getArea(double r){ // 如果半径下等于0 接下去运算将没有意义 // 这时可以抛出 运行异常 来停止程序 修改代码 if(r <= 0) { throw new RuntimeException("运行时异常"); } return r * r * Math.PI; } }
1.1.测试编译异常
public class Text { public static void main(String[] args) { Test test = new Test(); try { test.fun1(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } 运行结果: null java.lang.Exception at Test.fun1(Demo01.java:39) at Text.main(Text.java:6) public class Text { public static void main(String[] args) { Test test = new Test(); try { test.fun2(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } 运行结果: 运行异常 java.lang.RuntimeException: 运行异常 at Test.fun2(Demo01.java:43) at Text.main(Text.java:6)
1.2.测试运行异常
public class Text { public static void main(String[] args) { Test test = new Test(); double area = test.getArea(0); System.out.println(area); } } 运行结果: Exception in thread "main" java.lang.RuntimeException: 运行时异常 at Test.getArea(Demo01.java:49) at Text.main(Text.java:5)
2.继承中的方法重写
1.如果父类没有抛出异常 子类不能抛出异常 2.如果子类重写父类方法中 调用了一个有异常的方法这时只能选择try...catch 处理 不能进行抛出 3.如果父类抛出异常 子类可以抛也可以不抛
3.测试父类继承的方法
class Father1 { public void fun() throws Exception { } } class Son2 extends Father1{ @Override public void fun() throws Exception { try { method(); }catch(Exception e){ System.out.println("12"); } } //带异常的类 public void method() throws RuntimeException { } }
3.File类简单操作
功能介绍:
1.操作文件 2.操作文件夹 3.操作文件的路径 4.File的静态变量成员变量在不同系统下获取的路径分隔符不同 Mac系统下 路径的分隔符为:(冒号) window下 分割符为;(分号)
String pathseparator = File.pathSeparator; System.out.println(pathseparator); 运行结果: :(冒号) windows 下 ;(分号) String separator = File.separator; System.out.println(separator); 运行结果: / windows 下 \;
3.1File类构造方法
File(String pathname) File(String parent, String child) File(File parent, String child)
功能1.打印路径
public class Text { public static void main(String[] args) { File file = new File("/Users/lanou/Deskt 4000 op/test"); System.out.println(file); // 1.判断该了路径是否存在 boolean b1 = file.exists(); System.out.println(b1); // 使用相对路径 参照当前工程根目录 File file2 = new File("src/wl.txt"); // 打印绝对路径 System.out.println(file2.getAbsolutePath()); } } 运行结果: /Users/lanou/Desktop/test true /Users/lanou/Desktop/sh-day--023/src/wl.txt
功能2.拼接路径
public class Text { public static void main(String[] args) { String parent = "/Users/lanou/Desktop/"; String child = "test"; File file = new File(parent, child); boolean b1 = file.exists();//判断该路径是否存在 System.out.println(b1); System.out.println(file); } } 运行结果: true /Users/lanou/Desktop/test
功能3.路径中转拼接
public class Text { public static void main(String[] args) { String parent = "/Users/lanou/Desktop/"; String child = "test"; File file = new File(parent); File file2 = new File(file, child); System.out.println(file2.exists()); } } 运行结果: true
4.创建功能
public boolean createNewFile() public boolean mkdir() public boolean mkdirs()
功能1.创建文件
1.文件已经存在 不会重新创建 2.改方法只能创建文件 不给后缀也是创建的文件
public class Text { public static void main(String[] args) throws IOException { //创建桌面test文件下创建kkk文件 File file = new File("/Users/lanou/Desktop/test/kkk"); boolean b1 = file.createNewFile(); System.out.println(b1); //当前路径下创建ddp.txt文件 File file2 = new File("src/ddp.txt"); boolean b2 = file2.createNewFile(); System.out.println(b2); } 运行结果; true true 目标文件创建成功
功能2.创建文件夹
1.makdir()只能创建一级文件夹 2.makdirs()可以创建多级的文件夹(只要没有就会帮你创建)
public class Text { public static void main(String[] args) throws IOException { File file = new File("/Users/lanou/Desktop/test/www/sss"); boolean b = file.mkdirs();//创建多层文件夹 File file1 = new File("/Users/lanou/Desktop/test/555");//创建单词文件夹 boolean b2 = file1.mkdir(); System.out.println(b); System.out.println(b2); } } 运行结果: true true 目标文件夹创建成功
5.删除功能
public boolean delete() 1. 删除(不能恢复)不走回收站 2. 如果文件夹下没有子文件夹可以删除
public class Text { public static void main(String[] args) throws IOException { File file = new File("/Users/lanou/Desktop/test/555"); boolean b = file.delete(); System.out.println(b); } } 运行结果: true 目标文件删除不见
6.判断功能
public boolean isDirectory()// 判断是否是文件夹 public boolean isFile()//判断是否是文件 public boolean exists()//判断文件是否存在
public class Text { public static void main(String[] args) throws IOException { File file = new File("/Users/lanou/Desktop/test/www"); boolean b = file.isDirectory();判断是否是文件夹 boolean b1 = file.isFile();判断是否是文件 System.out.println(b1); System.out.println(b); } } 运行结果: false true
8.获取功能
public boolean isDirectory()// 判断是否是文件夹 public boolean isFile()//判断是否是文件 public boolean exists()//判断文件是否存在
获取功能
public String getPath()
public String getName()
public long length()
public String getAbsolutePath()//获取绝对路径以字符串形式
public File getAbsoluteFile()//获取绝对路径 以文件对象形式
public File getParentFile()//获取父级路径
public String getParent()
public String[] list()//
public File[] listFiles()//将路径文件当成一个数组
public class Text { public static void main(String[] args) throws IOException { //1.获取文件路径(跟tostring一样 输出路径 不管文件夹是否存在) File file = new File("/Users/lanou/Desktop/test/haha.txt"); String path = file.getPath(); System.out.println(path); //2.获取文件名字 String name = file.getName(); System.out.println(name); //3.获取文件夹长度(多少字节) //英文占一个字节 中文占3个字节 long length = file.length(); System.out.println(length); //4.获取父级路径 File parentFile = file.getParentFile(); System.out.println(parentFile); } } 运行结果:: /Users/lanou/Desktop/test/haha.txt haha.txt 6 /Users/lanou/Desktop/test
public class Text { public static void main(String[] args) throws IOException { getFileName( new File("/Users/lanou/Desktop/test")); } public static void getFileName(File file) { File[] listFiles = file.listFiles(); for(File file2 : listFiles) { if (file2.isFile()){ System.out.println(file2); }else { getFileName(file2); } } } } 运行结果: /Users/lanou/Desktop/test/.DS_Store /Users/lanou/Desktop/test/haha.txt /Users/lanou/Desktop/test/kkk
相关文章推荐
- java-File基本操作:FilenameFilte…
- Java File的基本操作
- java File类的基本操作
- java RandomAccessFile类文件基本操作
- 异常处理:java.lang.UnsupportedClassVersionError: Bad version number in .class file
- Java中File的基本操作
- Java中复制文件和文件夹的操作,异常未处理,无注释-_-!
- 黑马程序员-JAVA-常见异常与基本处理
- java中的 FileWriter类 和 FileReader类的一些基本用法包括对于异常的处理
- [Java 12 IO] File 类 的几个基本操作
- HTML5本地存储localStorage、sessionStorage基本用法、遍历操作、异常处理等
- java File的基本操作实例
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常的处理方法
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常的处理方法
- java操作excel异常处理-poi处理excel异常
- 黑马程序员—异常+IO流File类+Properties+PrinWriter+合并流+切割文件+对象序列化+管道流+RandomAccessFile+操作基本数据类型的DataStre
- java File的基本操作实例
- java File基本操作
- JAVA操作MySQL tImestamp列值为0时抛出异常的处理