我正在编写一个程序,我必须逐行将文件的所有数据传输到一个数组.当我显示该行时,数组中唯一的东西是最后一行.我需要数组包含文件的所有行,以便我可以选择数组的索引.
这是到目前为止的代码,
while(inputFileTest.hasNext()) //counts amount of lines in the file
{
count++;
inputFileTest.nextLine();
}
fileTest= new File("TestBank.txt");
inputFileTest= new Scanner(fileTest);
String[] testArr=new String[count];
while(inputFileTest.hasNext())
{
int i=0;
String line= inputFileTest.nextLine();
testArr= line.split("\n");
testArr[i]=testArr[0];
i++;
}
//int i=rand.nextInt(testArr.length);
for(String test:testArr)
System.out.println(test);
Run Code Online (Sandbox Code Playgroud)
}}