我正在阅读的文件有40
不同的字符串,我想把它放在一个2d-array
大小为[10][4]
。如何读取一个字符串文件到一个二维数组
代码到目前为止
public class GetAnswers {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("Answers.txt")))
{
String [][] answers;
answers = new String[10][4];
String line;
int i = 0;
String [] temp;
while ((line = br.readLine()) != null) {
temp = line.split("\n");
for (int j = 0; j < answers[i].length; j++)
{
answers[i][j] = temp[j];
System.out.println(j);
}
i++;
}
//System.out.println(answers[1][2]);
} catch (IOException e) {
e.printStackTrace();
}
文本文件格式:
apple
orange
dog
cat
非常感谢队友 –