Java写出程序未处理的异常类型IOException



我的程序的这一部分出现了多个错误。它基本上是从一个无组织的文件PersonnelInfo.txt中读取输入,并将其排序到EmployeeInfo类中的一个数组中(没有发布,因为我不认为问题出在那个类中,如果你们愿意,我会发布它以供参考)。

在对信息(如姓名、身份证号码、公司职务等)进行排序后,程序将其写入另一个文件OrganiZEDPersonelInfo.txt,该文件将按如下方式排序:

姓名:此处为员工姓名

ID:此处为员工ID

等等。

我得到的一个错误是在"static FileWriter writeOut…"行中,它是"Unhandled exception type IOException。我在主方法声明中放入了一个throws IOException,但它什么也没做。

接下来,在"publicStringtoString()"方法中,我收到多个错误。当我试图使其无效时,它会说返回类型与Object.toString()不兼容。当我将其保留为String并返回null时,它希望我使用try/catch,但主要错误仍然存在。

请帮忙!这让人很沮丧。。。

import java.io.*;
import java.util.Scanner;
public class HR {
static EmployeeInfo[] employee = new EmployeeInfo[4];
static FileWriter writeOut = new FileWriter("/Users/zach/Documents/workspace/Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
static BufferedWriter out = new BufferedWriter(writeOut);

public static void main(String[] args) throws IOException {
File PersonnelInfo = new File("/Users/zach/Documents/workspace/Dec. 10, 2012/src/PersonnelInfo.txt");
Scanner inputFile = new Scanner(PersonnelInfo);
int numOfEmployees = Integer.parseInt(inputFile.nextLine());
System.out.println("Number of employees: " + numOfEmployees);

for (int i = 0; i < 4; i++) {
String name = inputFile.nextLine();
String ID = inputFile.nextLine();
String title= inputFile.nextLine();
String startDate = inputFile.nextLine();
employee[i] = new EmployeeInfo(name, ID, title, startDate);
}
listEmployeeInfo();
employee.toString();
}

public String toString() {
for (EmployeeInfo i: employee) {
out.write("Name: " + i.getName());
out.write("ID: " + i.getID());
out.write("Title: " + i.getTitle());
out.write("Start Date: " + i.getStartDate());
}
return null;
}

如果重写toString()(正如您所做的,因为toSTring()是Object中的一个方法,并且您正在类中重新定义它,它像所有类一样扩展了Object),那么您应该遵守重写方法的约定,即返回对象的String表示(而不是将字段写入缓冲写入器。请用另一个名称命名此方法。

此外,您正在调用一个抛出已检查异常的方法(out.write()):IOException。因此,要么你知道如何处理这个异常,并且应该捕获它,要么你不知道如何处理它,并且方法应该声明它抛出异常:

public void writeToOut() throws IOException {
for (EmployeeInfo i: employee) {
out.write("Name: " + i.getName());
out.write("ID: " + i.getID());
out.write("Title: " + i.getTitle());
out.write("Start Date: " + i.getStartDate());
}
}

程序中存在其他编译错误。每个都带有一条错误消息、一个文件名和一个行号。请尝试理解消息,并修复错误。阅读有关异常的Java教程,了解如何处理它们。

如果在阅读本教程后仍然无法处理它们,请再问一个问题,并粘贴从编译器中得到的确切错误消息。

当您执行static void main时,您不在类HR的实例中,而是在它的静态方法中。因此,如果您想使用您编写的toString()方法,您必须执行new HR().toString()

您最好从所有字段中删除static关键字,并创建HR对象的实例,然后在该对象的Constructor中打开文件,即

public HR() {
try {
this.writeOut = new FileWriter("/Users/zach/Documents/workspace/Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
} catch (IOException e) {
// handle it, etc.
}
}
public void processFile() {
// Do the stuff
}
public static void main(String[] args) {
HR myHR = new HR().processFile();
// etc.
}

这将允许您使用toString()方法,并允许您按照自己的意愿处理异常

其他人有到String()的地址,所以我要试试FileWriter。。。

我会在一个方法而不是静态范围中填充writeOut,这样你就可以捕获异常,甚至可以尝试不同的文件名或其他东西:

FileWriter openOutputfile(String name)
{
boolean done = false;
FileWriter result = null
try {
result = new FileWriter(...);
done = true;
}
catch(IOException ex) {
// print the stack trace
// prompt for another filename
// name = read line
}
return result;
}

我建议在main中移动Writer的静态声明,原因有两个(一个原因是它们不需要是静态属性,而且IOException正在那里处理)。从toString中获取字符串,并使用编写器将字符串写入文件:

public class HR {
static EmployeeInfo[] employee = new EmployeeInfo[4];

public static void main(String[] args) throws IOException {
FileWriter writeOut = 
new FileWriter("/Users/zach/Documents/workspace/"+
"Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
BufferedWriter out = new BufferedWriter(writeOut);
File PersonnelInfo = new File("/Users/zach/Documents/workspace/"+
"Dec. 10, 2012/src/PersonnelInfo.txt");
Scanner inputFile = new Scanner(PersonnelInfo);
....
//write employee string in the file
out.write(getEmployeeString());
out.close();
}
public String getEmployeeString() {
StringBuilder stringBuilder = new StringBuilder();
for (EmployeeInfo i: employee) {
stringBuilder.append("Name: " + i.getName());
stringBuilder.append("ID: " + i.getID());
stringBuilder.append("Title: " + i.getTitle());
stringBuilder.append("Start Date: " + i.getStartDate());
}
return stringBuilder.toString();
}       
}

如果您必须将它们作为静态变量,则在静态块中进行初始化,异常处理如下:

static FileWriter writeOut;
static BufferedWriter out;
static {
try{
writeOut = 
new FileWriter("/Users/zach/Documents/workspace/"+
"Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
out = new BufferedWriter(writeOut);
}catch(IOException ioex){
ioex.printStackTrace();
}
} 

已更正为字符串()

public String toString() {
StringBuffer buf = new StringBuffer();
for (EmployeeInfo i: employee) {
buf.append("Name: ").append(i.getName());
buf.append("ID: ").append(i.getID());
// ....
}
return buf.toString();
}

主要是打印内容:

HR hr = new HR();
out.write(hr.toString());

但我会使用自己的写作方法"writeEmployees(out)"参见海报JB Nizet 的解决方案

最新更新