简体   繁体   English

用java解析平面文件

[英]Flat file parsing with java

I want to parse below wmi output to hashmap as a key value pair using java.Please give me suggestions .. My WMI Output contains 2 rows with multiple columns, first row is header and second row contains data.我想使用java将wmi输出解析为hashmap作为键值对。请给我建议..我的WMI输出包含2行多列,第一行是标题,第二行包含数据。 I want either regex or any approach to seperate the header with corresponding data as a key value for hashmap.我想要正则表达式或任何方法来将标头与相应的数据分开作为哈希图的键值。

I am not getting any idea how to proceed...我不知道如何继续......

Caption                  Description              IdentifyingNumber  Name                   

Computer System Product  Computer System Product                     HP xw4600 Workstation                   

Parsing output should be like ...解析输出应该像......

Key = Value键 = 值

Caption = Computer System Product Caption = 计算机系统产品

Description = Computer System Product描述 = 计算机系统产品

IdentifyingNumber =识别号码 =

Name = HP xw4600 Workstation名称 = HP xw4600 工作站

If your file format is always the same, you can easy use parser:如果您的文件格式始终相同,您可以轻松使用解析器:

FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
Reader in = new BufferedReader(isr);
String[] array = new String[2];
for(int i = 0; i < 2; i++)
{
    ((BufferedReader)in).readLine();
}
for(int i = 0; i < array.length; i++)
{
   array[i] = ((BufferedReader)in).readLine();
   if(array[i] == null)
   {
    array[i] = ""; //$NON-NLS-1$
   }
}
in.close();
String[] headers = array[0].split(Pattern.quote("\t")); //$NON-NLS-1$
String[] values = array[1].split(Pattern.quote("\t")); //$NON-NLS-1$

And then running through both and filling hashmap然后遍历两者并填充哈希图

我将 wmi 输出格式化为列表,现在很容易格式化输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM