从计算机中读取文件

时间:2013-04-06 17:45:16

标签: java

现在我有这个错误:

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:4     在Lotto1.main(Lotto1.java:37)

第37行:arr [count] [0] = s.next()+“”+ s.next();

import java.io.*;
import java.util.*;

public static void main(String[] args) throws IOException  {

        File f = new File("D:\\Filipe\\Project Final\\src\\database_lotto.txt");
        Scanner s;
        try {
            s = new Scanner(f);

           BufferedReader reader = new BufferedReader(new FileReader(f));
            int lines = 0;
            while (reader.readLine() != null) {
                lines++;
            }
            reader.close();

            arr  = new String[lines][3];

            int count = 0;
            //while theres still another line
            while (s.hasNextLine()){
                arr[count][0] = s.next() + ""+ s.next();
                arr[count][1] = s.next();
                arr[count][2] = s.next();
               count++;                   
            }
        } catch (FileNotFoundException ex) {
           System.out.println("File does not exist");
        }

1 个答案:

答案 0 :(得分:3)

您没有将正确的路径放入FileReader只进入File f,您可以将f传递给Filereader而不是重复路径:

File f = new File("D:\\database_lotto.txt");
[...]
BufferedReader reader = new BufferedReader(new FileReader(f));