每个人怎么样?我正在尝试读取文件并将该文件中的每个String存储到Array中的元素中。问题是,我无法转移到下一行数据。我假设这是我需要使用ArrayList的时候,但是我很难弄清楚如何遍历每行上的每个文本并将其存储到元素中然后转到下一行。我希望将每个数据存储在自己的元素中。另外,我如何调用该元素中存储的数据?这是我的代码:
Scanner readPayrollData = new Scanner(new FileReader ("employeeData.dat"));
String[] employee = new String[3];
ArrayList<String> data = new ArrayList<String>();
while(readPayrollData.hasNextLine())
{
for(int i = 0; i < employee.length; i++)
{
readPayrollData.hasNext();
employee[i] = readPayrollData.next();
}
readPayrollData.hasNextLine();
data.add(employee);
}
System.out.println(data);
readPayrollData.close();
答案 0 :(得分:1)
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("textfile.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(inFile.hasNextLine()){
list.add(inFile.nextLine());
}
答案 1 :(得分:0)
List<String> employee=new ArrayList<>();
Scanner fin=null;
try{
fin=new Scanner(new File("employeeData.dat"));
}catch(Exception e){
System.out.println("unable to open file");
e.printStackTrace();
}
while(fin.hasNextLine()){
employee.add(fin.nextLine());
}
fin.close();