File数组中的IndexOutOfBoundsException。可能是什么问题?

时间:2015-10-14 16:20:03

标签: java xml indexoutofboundsexception

所以我在本地C盘中有一个目录。

C:/ Search Files / Folder [number] /hello.txt 内部搜索文件我有四个名为: 文件夹1 文件夹2 文件夹3 文件夹4

Inside Folder 1我有一个名为hello.txt的文件,里面有一些String。

我想要做的是获取fileDirectory,fileName和fileContent并将其放入XMLMessage对象列表中。我已经粘贴了我的主类和我的XMLMessage POJO。当我运行它时,我得到一个indexOutOfBoundsException。我现在已经被困了几个小时了。我需要另外一双眼睛来研究它。

谢谢,

package org.raghav.stuff;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class GetFile {

public static void main(String[] args) throws IOException {
    File[] files = new File("C:\\Search Files").listFiles();

    showFiles(files);
}

public static void showFiles(File[] files) throws IOException {
    String line = null;
    List<XMLMessage> xmlMessageList = new ArrayList<XMLMessage>();

    int i = 0;
    //XMLMessage folderFile = new XMLMessage();
    try {
        for (File file : files) {
            if (file.isDirectory()) {
                String fileName = file.getName();
                System.out.print(fileName);
                xmlMessageList.get(i).setFileName(fileName);

                //folderFile.setFileName(fileName);
                showFiles(file.listFiles()); // Calls same method again.
            } else {

                xmlMessageList.get(i).setFileDirectory(file.getName() + file.toString());
                //folderFile.setFileDirectory(file.getName() + file.toString());
                System.out.print("\tFile: " + file.getName()
                        + file.toString());
                // System.out.println("Directory: " + file.getName());
                BufferedReader in = new BufferedReader(new FileReader(file));
                while ((line = in.readLine()) != null) {
                    xmlMessageList.get(i).setFileContent(line);
                //  folderFile.setFileContent(line);
                    System.out.print("\t Content:" + line);
                }
                in.close();
                System.out.println();
            }
            i++;
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    System.out.println(xmlMessageList.toString());
}
}

这是POJO:

package org.raghav.stuff;

public class XMLMessage {

private String fileDirectory;
private String fileName;
private String fileContent;

public final String FILE_NAME = "fileName";
public final String FILE_DIRECTORY = "fileDirectory";



public XMLMessage(String fileDirectory, String fileName, String fileContent) {
    this.fileDirectory = fileDirectory;
    this.fileName = fileName;
    this.fileContent = fileContent;
}

public XMLMessage() {

}

public String getFileDirectory() {
    return fileDirectory;
}

public void setFileDirectory(String fileDirectory) {
    this.fileDirectory = fileDirectory;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getFileContent() {
    return fileContent;
}

public void setFileContent(String fileContent) {
    this.fileContent = fileContent;
}

public String toString(){
    String returnString = "File Directory: " + fileDirectory + "\n" + "File Name" + fileName + "\n" + "File Content: " + fileContent;
    return returnString;
}

/*public String createResponseFileName(String fileName){

    int lastDot = fileName.lastIndexOf('.');
    String responseFileName = fileName.substring(0, lastDot) + "Response" + fileName.substring(lastDot);    
    return responseFileName;

}*/

/*public String createResponseFileContent(String fileContent){
    this.
}*/
}

2 个答案:

答案 0 :(得分:1)

你永远不会填写你的清单。我怀疑你应该有:

for (File file : files) {
    XMLMessage message = new XMLMessage();
    xmlMessageList.add(message);
    if (file.isDirectory()) {
        String fileName = file.getName();
        System.out.print(fileName);
        message.setFileName(fileName);

        //folderFile.setFileName(fileName);
        showFiles(file.listFiles()); // Calls same method again.
    } else {
        ... etc, using message instead of xmlMessageList.get(i)
    }
}

然后你根本不需要i变量。

答案 1 :(得分:-1)

我认为Jon Skeet是对的。

你永远不会填写你的清单。 你应该使用你的构造函数

XmlMessage m = new XMLMessage( fileDirectory, fileName,fileContent)

xmlMessageList.add(m);