gpt4 book ai didi

java - 从计算机读取文件

转载 作者:行者123 更新时间:2023-12-04 05:03:18 27 4
gpt4 key购买 nike

现在我有这个错误:

线程“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");
}

最佳答案

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

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

关于java - 从计算机读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com