Vijay 11:30 12:30
Jai 12:30 12:50
Vijay 13:45 14:25
Vijay 17:25 17:50
我希望每行和每个输入都插入到不同的数组或列表中 日期应采用相同的格式。
答案 0 :(得分:0)
这就是你想要的,
BufferedReader br = new BufferedReader(new FileReader("/path/to/file"));
ArrayList<String[]> list = new ArrayList<>();
String str;
while(null != (str = br.readLine()))
list.add(str.split(" "));
这将为您提供包含文件数据的String数组列表。
答案 1 :(得分:0)
根据您的要求,这根本不是一个好方法。 您应该将数据保存为xml格式,如:
data.xml中
<services>
<service>
<serviceName>Vijay</serviceName><startTime>11:30</startTime><endTime>12:30</endTime>
</service>
<service>
<serviceName>John</serviceName><startTime>14:30</startTime>16:30<endTime></endTime>
</service>
<service>
<serviceName>John</serviceName><startTime>13:30</startTime><endTime>14:30</endTime>
</service>
</employees>
应该有DTO类,例如Service.java。您必须反序列化该xml以使用JAXB创建Service类的多个对象。在这种方法中,您可以更轻松地处理数据。
Service.java
package test.com;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="service")
@XmlAccessorType(XmlAccessType.FIELD)
public class Service {
@XmlElement(name="serviceName")
private String serviceName;
@XmlElement(name="startTime")
private String startTime;
@XmlElement(name="endTime")
private String endTime;
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return serviceName + " "+startTime+ " "+endTime;
}
}
ServiceList.java
package test.com;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "serviceList")
@XmlAccessorType(XmlAccessType.FIELD)
public class ServiceList {
@XmlElement(name = "service")
private ArrayList<Service> serviceList;
public ArrayList<Service> getServiceList() {
return serviceList;
}
public void setServiceList(ArrayList<Service> serviceList) {
this.serviceList = serviceList;
}
}
TestMain.java
public class TestMain {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(ServiceList.class);
Unmarshaller m = context.createUnmarshaller();
ServiceList s = (ServiceList)m.unmarshal(new File("D:/data.xml"));
System.out.println(s.getServiceList().get(0).getServiceName());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}