[英]Read file to Array then store Array into ArrayList Java
大家好嗎? 我試圖讀取一個文件,並將該文件中的每個字符串存儲到數組中的元素中。 問題是,我無法移至下一行數據。 我假設那是我需要使用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();
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());
}
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.