Table of contents
Java Object Orientation - Exceptions in Java
Level 1: Exception handling mechanism in Java
Java object-oriented - file class
Level 2: Common operations on files
Java Object Orientation - Exceptions in Java
Level 1: Exception handling mechanism in Java
Level 2: Catching exceptions
package step2;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
/********* Begin *********/
try{
System.out.println(num1/num2);
}catch(ArithmeticException e){
System.out.println("除数不能为0");
}
/********* End *********/
}
}
Level 3: Throw an exception
package step3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Task {
/********* Begin *********/
//请在合适的部位添加代码
public static void main(String[] args) throws FileNotFoundException {
test();
}
public static void test()throws FileNotFoundException {
File file = new File("abc");
if(!file.exists()){ //判断文件是否存在
//文件不存在,则 抛出 文件不存在异常
throw new FileNotFoundException("该文件不存在");
}else{
FileInputStream fs = new FileInputStream(file);
}
}
/********* End *********/
}
Level 4: Custom exception
package step4;
import java.util.Scanner;
public class Task {
/********* Begin *********/
public static void main(String[] args) throws MyException{
Scanner sc = new Scanner(System.in);
String username = sc.next();
if(username.length()<3){
throw new MyException("用户名小于三位Exception");
}else System.out.println("用户名格式正确");
//判断用户名
}
}
class MyException extends Exception{
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
/********* End *********/
Java object-oriented - file class
Level 1: Create File
package step1;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Scanner;
public class Task {
/********* Begin *********/
public void solution()throws IOException {
File file1=new File("src/output/test.txt");
file1.createNewFile();
File file2=new File("src/output/hello.txt");
file2.createNewFile();
/********* End *********/
}
}
Level 2: Common operations on files
package step2;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Arrays;
public class Task {
public static void dcFile() throws IOException {
/********* Begin *********/
File file1=new File("src/test2");
file1.mkdirs();
File file2=new File("src/output/test2.txt");
file2.delete();
File file3=new File("src/test2/helloworld.txt");
file3.createNewFile();
File file4=new File("src/test2/step2.txt");
file4.createNewFile();
System.out.println("output目录结构为:");
File[] file5=new File("src/output").listFiles();
Arrays.sort(file5);
for(File file:file5){
System.out.println(file.getName());
}
System.out.println("test2目录结构为:");
File[] file6=new File("src/test2").listFiles();
Arrays.sort(file6);
for(File file:file6){
System.out.println(file.getName());
}
/********* End *********/
}
}
Level 3: File Viewer
package step3;
import java.io.File;
import java.util.Arrays;
public class Task {
/********** Begin **********/
String dir="+--";
String wj="--";
String level="";
public void showDirStructure(File file) {
if(file.isDirectory()){
System.out.println(level+dir+file.getName());
String level1=level;
level+=" ";
File[] files=file.listFiles();
Arrays.sort(files);
for(File file1:files){
showDirStructure(file1);
}
level=level1;
}else{
System.out.println(level+wj+file.getName());
}
}
/********** End **********/
}
Level 4: Picture Viewer
package step4;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
public class Task {
/********** Begin **********/
String dir="+--";
String wj="--";
String level="";
public void showDirStructure(File file) {
if(file.isDirectory()){
System.out.println(level+dir+file.getName());
String level1=level;
level+=" ";
File[] files=file.listFiles();
Arrays.sort(files);
for(File file1:files){
showDirStructure(file1);
}
level=level1;
}else{
String name=file.getName();
if(name.endsWith("jpg")||name.endsWith("bmp")||name.endsWith("png"))
System.out.println(level+wj+name);
}
}
/********** End **********/
}