读取文件时出现ArrayIndexOutOfBoundsException

时间:2016-01-26 19:22:06

标签: java file file-io readfile

我正在尝试使用此方法读取文件:

    public static char [] llegir() {
    Palabra nova = new Palabra();
    botarBlancs();
    while ((lletra != fiSequencia) && // No ha acabat la seqüència
            (lletra != blanc)) { // Hi ha prou espai
        nova.lletres[nova.llargaria++] = lletra;
        lletra = leerCarTeclado();
    }
    //System.out.println(nova.girar());
    return nova.lletres;
}

(botarBlancs()只是跳空格来区分一个词)

这是我正在使用的代码:

    public static void generador_de_cartas() throws Exception{
    Palabra pl = new Palabra();

    FileReader fr = new FileReader("datos_clientes.txt");
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    char [] linea;

    while (line != null) {
        linea=line.toCharArray();
        linea=Palabra.llegir();
        System.out.println(linea);
        line=br.readLine();

    }

    br.close();
    fr.close();
}

它在线程“main”中给出了一个异常java.lang.ArrayIndexOutOfBoundsException:20在方法llegir()中,它表示:nova.lletres [nova.llargaria ++] = lletra;

我该如何解决这个问题?

有关如何使用该方法读取文件行的​​任何想法吗?

提前谢谢

*********************** EDIT ***********************

这是我尝试过的:

    public static void generador_de_cartas() throws Exception {
    Palabra pl = new Palabra();
    char[] tipo1 = {'t', '1'}, tipo2 = {'t', '2'}, tipo3 = {'t', '3'}, nRef = {'#', 'n'}, dRef = {'#', 'd'};
    FileReader fr = new FileReader("datos_clientes.txt");
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    char[] linea;

    while (line != null) {
        linea = line.toCharArray();
        Palabra.llegir(); //reads line 
        if (Palabra.llegir() == tipo1) {
            //code that creates a new file following the pattern of tipo 1 files
        } else {
            if (Palabra.llegir() == tipo2) {
                //code that creates a new file following the pattern of tipo 2 files
            } else {
                if (Palabra.llegir() == tipo2) {
                    //code that creates a new file following the pattern of tipo 3 files
                }
            }
            line = br.readLine();

        }
        line = br.readLine(); //reads following line
    }

    br.close();
    fr.close();
}

但它一直报告同样的例外情况。

该程序的主要思想是为主文件(datos_clientes.txt)的不同行中表示的每个人创建一个文本文件,文件具有不同的文本或模式,具体取决于人。

主文件的一行如下所示:

t1 #n name #d address(t1是将要创建的文件的模式)

之后我必须阅读该人的姓名,保存,阅读地址,保存并将其写入为他们创建的文件中。

这是我到目前为止所做的,只是创建不同文件的方法。

希望你能帮助我

谢谢!

这是Palabra类:

public class Palabra {

public static final char blank = ' ';
public static final char endSequence = '\n';
// Constants privades
// Llargària màxima d'una paraula
private static final int MAXIM = 20;
// ATRIBUTS
private char[] letters;
private int length;
// atribut de classe, per simplificar les operacions de lectura
private static char letter = ' ';
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++EXTRA
private static char[] frase = null;
private static int indice;

// INTERFICIE
// Constructor
public Palabra() {
    letters = new char[MAXIM];
    length = 0;
}

public static char[] llegir() {
    Palabra nova = new Palabra();
    botarBlancs();
    while ((letter != endSequence) && // No ha acabat la seqüència
            (letter != blank)) { // Hi ha prou espai
        if (nova.length < nova.letters.length) {
            nova.letters[nova.length++] = letter;
            letter = leerCarTeclado();

        }
    }

    return nova.letters;
}

public boolean esIgualA(Palabra b) {
    boolean iguals = length == b.length;
    for (int idx = 0; (idx < length) && iguals; idx++) {
        iguals = letters[idx] == b.letters[idx];
    }
    return iguals;
}

public static boolean iguals(Palabra a, Palabra b) {
// using previous esIgualA
    return a.esIgualA(b);
}

public static void botarBlancs() {
    while (letter == blank) {
        letter = leerCarTeclado();
    }
}

static public char leerCarTeclado() {
    char res = '.';
    if (frase != null) {
        res = frase[indice++];
    }
    return res;
}

}

我把一些改为非静态,至少我可以运行它,现在我想知道我是否正确读取了这条线。如果我对方法llegir()没有错,我可以读一个单词并保存它,对吗?如我错了请纠正我。我正在尝试读取行中的第一个“单词”,例如“t1”,然后我将它与另一个char数组进行比较,我尝试打印以检查代码是否有效,但事实并非如此。我做错了什么?

        while (line != null) {
        linea = line.toCharArray();
        char [] tipo=Palabra.llegir(); //reads first word 

        if (tipo == tipo1) {
           System.out.print("tipo1") ;//code that creates a new file following the pattern of tipo 1 files
        } else {
            if (tipo == tipo2) {
                System.out.print("tipo2") ;//code that creates a new file following the pattern of tipo 2 files
            } else {
                if (tipo == tipo2) {
                   System.out.print("tipo3") ; //code that creates a new file following the pattern of tipo 3 files
                }
            }


        }
        line = br.readLine(); //reads following line
    }

2 个答案:

答案 0 :(得分:2)

每次调用方法时都应该从数组的开头开始,即0,你应该确保你没有尝试访问一个不存在的索引,即必须为真nova.llargaria < nova.lletres.length

注意:您正在阅读一行,但您似乎忽略了它。这是为了吗?

答案 1 :(得分:0)

你没有给我们足够的信息......但是:

在调试器中打开它(Intellij IDEA有一个很棒的例子)。如果通过单击左边距并右键单击它来创建断点,则可以告诉它在断点处停止的条件。

告诉它在nova.llargaria is >= nova.lletres.length时停在断点处。这样可以让你在崩溃之前看到正在发生的事情。

此外,您只需检查代码中的nova.llargaria is >= nova.lletres.lengthcontinue;是否跳过它,以便在这种情况下不会崩溃。

相关问题