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

问题描述:

我正在使用此程序的这一部分获得多个erros。它的基本功能是从无组织文件PersonnelInfo.txt中读取输入,并将其分类到EmployeeInfo类中的一个数组中(不张贴,因为我不相信问题在于该类,如果你们想要的话,我会张贴以供参考)。Java写出程序未处理的异常类型IOException

排序后的信息(如姓名,身份证号,标题,公司等)的程序写入该到另一个文件,ORGANIZEDPersonnelInfo.txt,将被归类为这样的:

名称:员工姓名这里

ID:这里的员工ID

一个错误,我得到的是“静态的FileWriter WriteOut的......”行,这是“未处理的异常类型为IOException我把罚球主方法中的IOException d但它没有做任何事情。

接下来,在“public String toString()”方法中,我收到多个错误。当我尝试使其无效时,它表示返回类型与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()

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()); 

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

+0

其写入文件,但我得到[LEmployeeInfo; @ 18872380 我太会选择直接写入文件,但对于这个项目,我们需要使用的方法。 – Zach

+0

Hr hr = new Hr(); out.write(hr.toString()); – AlexWien

+0

但现在它变得有点丑陋。 – AlexWien

如果您覆盖toString()(因为toSTring()是来自Object的一个方法,并且在您的类中进行了重新定义,所有类都会扩展Object),所以您应该遵守重写方法的合约,即返回对象的字符串表示形式(而不是将字段写入缓冲写入器。用另一个名字命名这个方法。

此外,您正在调用引发检查异常的方法(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 tutorial about exceptions了解如何处理它们。

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

我会建议您在您的主内移动您的静态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(); 
     } 
    } 
+0

非常感谢,这个解决方案让程序写入文件,但现在它写了[LEmployeeInfo; @ 39617189,所以我需要弄清楚发生了什么。 – Zach

+0

@Zach它从我身边的简单错误。而不是'out.write(employee.String());'(这是调用toString方法在雇员类)它应该只是'out.write(toString());',但这看起来不好,因此我建议将方法名从'toString()'改为'getEmployeeString()'并使用'out.write(getEmployeeString());'。我更新了答案。 –

当您执行static void main时,您不在HR类的实例中,而是处于静态方法中。所以,如果你想使用你写的toString()方法,你必须做new HR().toString()

你会好起来的所有字段删除static关键字和创建HR对象的实例,然后打开文件中该对象的构造函数,即

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()方法,让你来处理异常,你想要的方式。

别人有地址的toString(),所以我必须在一个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; 
}