[英]Read file to Array then store Array into ArrayList Java
How's it going everyone. 大家好吗? I am trying to read a file and store each String within that file into an element in the Array.
我试图读取一个文件,并将该文件中的每个字符串存储到数组中的元素中。 The problem is, I cannot move to the next line of data.
问题是,我无法移至下一行数据。 I am assuming that is when I would need to use an ArrayList, but I am having difficulty figuring out how to iterate through each text on each line and store that into an element then move on to the next line.
我假设那是我需要使用ArrayList的时候,但是我很难弄清楚如何遍历每行中的每个文本并将其存储到元素中然后移至下一行。 I want to have each data stored in its own element.
我想将每个数据存储在其自己的元素中。 Also how would I call the data stored within that element?
另外,我将如何调用存储在该元素中的数据? Here is my code:
这是我的代码:
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.