Skip to content

Commit

Permalink
feat: add exception handling for input format mismatch and missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZHow1024 committed Nov 27, 2024
1 parent 068d278 commit 36ee14f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
67 changes: 50 additions & 17 deletions src/ui/Application.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ui;

import pojo.entity.DataTrace;
import pojo.entity.FileHeader;
import pojo.entity.SEGY;
import util.SEGYUtils;

import java.io.File;
import java.util.Map;
import java.util.Scanner;

/**
Expand All @@ -23,40 +26,58 @@ public static void show(String[] args) {
if (args.length != 0) {
switch (args[0]) {
case "-head", "head" -> {
System.out.println("正在解析中...");
System.out.println(SEGYUtils.parseFileHeader(args[1]));
FileHeader fileHeader = null;
try {
fileHeader = SEGYUtils.parseFileHeader(args[1]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("格式错误,输入 -help 查看提示");
}
if (fileHeader != null)
System.out.println(fileHeader);
System.exit(0);
}
case "-body", "body" -> {
long num = 1;
try {
num = Long.parseLong(args[1]);
} catch (NumberFormatException e) {
segy = SEGYUtils.parseSEGY(args[2]);
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
System.out.println("格式错误,输入 -help 查看提示");
System.exit(0);
;
} catch (UnsupportedOperationException e) {
System.out.println("数据采样格式编码错误,请检查");
}
if (segy != null) {
System.out.println("\n@@@第 " + num + " 个数据道@@@");
System.out.println(segy.getDataBody().get(num));
}
segy = SEGYUtils.parseSEGY(args[2]);
System.out.println("\n@@@第 " + num + " 个数据道@@@");
System.out.println(segy.getDataBody().get(num));
System.exit(0);
}
case "-all", "all" -> {
long num;
long num = 1;
try {
num = Long.parseLong(args[1]);
segy = SEGYUtils.parseSEGY(args[2]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("格式错误,输入 -help 查看提示");
} catch (NumberFormatException e) {
System.out.println("输入格式错误,输入 -help 查看提示");
break;
}
segy = SEGYUtils.parseSEGY(args[2]);
System.out.println(segy.getFileHeader());
System.out.println("\n@@@第 " + num + " 个数据道@@@");
System.out.println(segy.getDataBody().get(num));
if (segy != null) {
System.out.println(segy.getFileHeader());
System.out.println("\n@@@第 " + num + " 个数据道@@@");
System.out.println(segy.getDataBody().get(num));
}
System.exit(0);
}
case "-i", "i" -> {
filePath = args[1];
try {
filePath = args[1];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("输入格式错误,输入 -help 查看提示");
System.exit(0);
}
}
case "-help", "help" -> {
System.out.println("--- ZSEGY 1.0.1 使用说明 ---");
Expand Down Expand Up @@ -109,12 +130,24 @@ public static void show(String[] args) {
}
}
case "1" -> {
System.out.println(segy.getFileHeader());
if (segy != null)
System.out.println(segy.getFileHeader());
else
System.out.println("文件头为空");
}
case "2" -> {
System.out.print("数据道数:");
long num = scanner.nextLong();
System.out.println(segy.getDataBody().get(num));
if (segy != null) {
Map<Long, DataTrace> dataBody = segy.getDataBody();
if (dataBody == null) {
System.out.println("数据体为空");
} else {
System.out.print("数据道数:");
long num = scanner.nextLong();
System.out.println(dataBody.get(num));
}
} else {
System.out.println("数据体为空");
}
}
case "3" -> {
System.out.println("程序退出...");
Expand Down
5 changes: 5 additions & 0 deletions src/util/SEGYUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static SEGY parseSEGY(File file) {
* @return SEGY
*/
public static SEGY parseSEGY(String filePath) {
if (!new File(filePath).exists()) {
System.out.println("文件不存在");
return null;
}

SEGY segy = new SEGY();

System.out.println("正在解析中...");
Expand Down

0 comments on commit 36ee14f

Please sign in to comment.