从 java 中不存在的文本文件中读取?
Read from a text file that does not exist in java?
我有一些奇怪的问题,但我希望得到解决。
我在 java 中创建了一个构造函数,它在创建来自构造函数 class 的对象时从文本文件中读取。
如果此文件存在,那么它会读取其数据,即使它是空的。
但我希望有解决方案来处理(文件不存在)错误,因为这会使我的程序崩溃。
这是我的代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Inventory{
public Inventory(){
Scanner x = null;
try{
x = new Scanner(new File("C:\Users\فاطمة\Downloads\products.txt"));
}
catch(Exception e)
{
System.out.println("No current products.");
}
while(x.hasNext())
{
String a = x.next(); // id
String b = x.next(); // product name
String c = x.next(); // product type
String d = x.next(); // product brand
int e = x.nextInt(); // product quantity
int f = x.nextInt(); // production day
int g = x.nextInt(); // production month
int h = x.nextInt(); // production year
int i = x.nextInt(); // expiry day
int j = x.nextInt(); // expiry month
int k = x.nextInt(); // expiry year
String l = x.next(); // company name
String m = x.next(); // supplier name
String n = x.next(); // supplier address
String p = x.next(); // supplier phone number
}
}
}
查看代码中的逻辑流。即使 file/scanner 行抛出错误(即未找到文件),您仍会尝试读取它。
您正在寻找的解决方案是
Scanner x = null;
try {
x = new Scanner(new File("C:\Users\فاطمة\Downloads\products.txt"));
while(x.hasNext()) {
String a = x.next(); // id
String b = x.next(); // product name
String c = x.next(); // product type
String d = x.next(); // product brand
int e = x.nextInt(); // product quantity
int f = x.nextInt(); // production day
int g = x.nextInt(); // production month
int h = x.nextInt(); // production year
int i = x.nextInt(); // expiry day
int j = x.nextInt(); // expiry month
int k = x.nextInt(); // expiry year
String l = x.next(); // company name
String m = x.next(); // supplier name
String n = x.next(); // supplier address
String p = x.next(); // supplier phone number
}
} catch(Exception e) {
System.out.println("No current products.");
}
你应该做的不是捕获 Exception
而是捕获一个特定的错误,比如 FileNotFoundException
或者,如果你想捕获所有错误,那么保持原样,但我建议你检查错误并使用
之类的内容向控制台提供更多详细错误
...
} catch (Exception e) {
if (e instanceof FileNotFoundException) {
System.out.println("could not find the file...");
} else {
System.out.println("something else went wrong");
}
}
请记住,如果 while 循环在 try ... catch
块内,读取文件时抛出的错误也会被捕获。
捕获多种类型错误的更明确的方法是
...
} catch (FileNotFoundException e) {
System.out.println("file not found");
} catch (Exception e) {
System.out.println("something else went wrong");
} ...
我有一些奇怪的问题,但我希望得到解决。
我在 java 中创建了一个构造函数,它在创建来自构造函数 class 的对象时从文本文件中读取。
如果此文件存在,那么它会读取其数据,即使它是空的。
但我希望有解决方案来处理(文件不存在)错误,因为这会使我的程序崩溃。
这是我的代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Inventory{
public Inventory(){
Scanner x = null;
try{
x = new Scanner(new File("C:\Users\فاطمة\Downloads\products.txt"));
}
catch(Exception e)
{
System.out.println("No current products.");
}
while(x.hasNext())
{
String a = x.next(); // id
String b = x.next(); // product name
String c = x.next(); // product type
String d = x.next(); // product brand
int e = x.nextInt(); // product quantity
int f = x.nextInt(); // production day
int g = x.nextInt(); // production month
int h = x.nextInt(); // production year
int i = x.nextInt(); // expiry day
int j = x.nextInt(); // expiry month
int k = x.nextInt(); // expiry year
String l = x.next(); // company name
String m = x.next(); // supplier name
String n = x.next(); // supplier address
String p = x.next(); // supplier phone number
}
}
}
查看代码中的逻辑流。即使 file/scanner 行抛出错误(即未找到文件),您仍会尝试读取它。
您正在寻找的解决方案是
Scanner x = null;
try {
x = new Scanner(new File("C:\Users\فاطمة\Downloads\products.txt"));
while(x.hasNext()) {
String a = x.next(); // id
String b = x.next(); // product name
String c = x.next(); // product type
String d = x.next(); // product brand
int e = x.nextInt(); // product quantity
int f = x.nextInt(); // production day
int g = x.nextInt(); // production month
int h = x.nextInt(); // production year
int i = x.nextInt(); // expiry day
int j = x.nextInt(); // expiry month
int k = x.nextInt(); // expiry year
String l = x.next(); // company name
String m = x.next(); // supplier name
String n = x.next(); // supplier address
String p = x.next(); // supplier phone number
}
} catch(Exception e) {
System.out.println("No current products.");
}
你应该做的不是捕获 Exception
而是捕获一个特定的错误,比如 FileNotFoundException
或者,如果你想捕获所有错误,那么保持原样,但我建议你检查错误并使用
...
} catch (Exception e) {
if (e instanceof FileNotFoundException) {
System.out.println("could not find the file...");
} else {
System.out.println("something else went wrong");
}
}
请记住,如果 while 循环在 try ... catch
块内,读取文件时抛出的错误也会被捕获。
捕获多种类型错误的更明确的方法是
...
} catch (FileNotFoundException e) {
System.out.println("file not found");
} catch (Exception e) {
System.out.println("something else went wrong");
} ...