我正在尝试从结构化TXT到Excel表创建转换器,而TXT文件读取和正确的数据存储遇到问题。
TXT文件具有以下内容:
-
主机名:主机名1
IP:ipAddress1
工具信息:
名称:deviceName1_1,SERIALNUMBER:serialNumber1_1
-
主机名:主机名2
IP:ipAddress2
工具信息:
名称:deviceName2_1,SERIALNUMBER:serialNumber2_1
名称:deviceName2_2,SERIALNUMBER:serialNumber2_2
名称:deviceName2_3,SERIALNUMBER:serialNumber2_3
-
HOSTNAME :主机名3
IP:ipAddress3
设备信息:
名称:deviceName3_1,序列号:serialNumber3_1
名称:deviceName3_2,序列号:serialNumber3_2
请注意,此TXT文件包含3个信息块。每个信息块均由特殊符号“-”分隔。一个TXT文件可以包含更多或更少的信息块。在设备信息下,可以包含或多或少的设备名称和序列号行。
期望解析文件并将数据存储在创建的对象中。
对象应具有以下字段:
String hostname;
String ip;
String name;
String serialnumber;
通常,每个信息块可以创建1个对象。如果在设备信息下有多行设备名称和序列号,则应创建多个具有相同主机名和IP,但设备名称和序列号不同的对象。
预期结果:
应该使用以下数据创建总共6个对象。
对象#1具有主机名1,ipAddress1,设备名1,serialNumber1
对象#2具有主机名2,ipAddress2,设备名2_1,serialNumber2_1
对象#3具有主机名2,ipAddress2,设备名2_2,serialNumber2_2
对象#4具有主机名2,ipAddress2,设备名2_3,serialNumber2_3
对象#5具有主机名3, ipAddress3,deviceName3_1,serialNumber3_1
对象#6具有主机名3,ipAddress3,deviceName3_2,serialNumber3_2
到目前为止,我已经尝试编写以下代码:
private ArrayList<Device> readFile() {
ArrayList<Device> devices = new ArrayList<Device>
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(txtFile)));
String line = "";
String data;
int i=0;
while ((line = br.readLine()) != null) {
// Device device = new Device();
if (line.equals("-")) {
// Device device = new Device();
while ((line = br.readLine()) != null) {
Device device = new Device();
if (line.contains("HOSTNAME: ")) {
// Device device = new Device();
if (line.substring(0,10).equals("HOSTNAME: ")) {
device.setHostname(line.substring(10,line.length()));
devices.add(device);
}
}
if (line.contains("IP: ")) {
// Device device = new Device();
if (line.substring(0,4).equals("IP: ")) {
device.setIp(line.substring(4,line.length()));
devices.add(device);
}
}
devices.add(device);
}
}
}
}
但是,对象具有主机名,但IP为NULL,反之亦然。我也尝试过此代码:
try {
Scanner scanner = new Scanner(txtFile);
int i = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String array[] = line.split(",");
DeviceData device = new DeviceData(array[0],array[1]);
System.out.println("Eilute " + (i++) + ": " + array[0]);
}
但是我在这里迷失了自己。
我在分离所需的数据并将其放入对象时遇到了麻烦。我应该如何正确地做呢?有什么想法和/或建议吗?
遇到以“ NAME:”开头的行时,应创建设备对象。为了保留IP和HOSTNAME值,应使用局部变量。一个示例实现可能如下所示:
br = new BufferedReader(new InputStreamReader(
new FileInputStream(txtFile)));
String line;
while ((line = br.readLine()) != null) {
if (line.equals("-")) {
String ip = null;
String hostname = null;
while ((line = br.readLine()) != null) {
if (line.contains("HOSTNAME: ")) {
if (line.substring(0, 10).equals("HOSTNAME: ")) {
hostname = line.substring(10, line.length());
}
}
if (line.contains("IP: ")) {
if (line.substring(0, 4).equals("IP: ")) {
ip = line.substring(4, line.length());
}
}
if (line.contains("NAME: ")) {
if (line.substring(0, 6).equals("NAME: ")) {
Device device = new Device();
device.setIp(ip);
device.setHostname(hostname);
System.out.println("Adding device with ip = " +
ip + " and hostname " + hostname);
System.out.println("Details: " + line);
devices.add(device);
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句