如何从存储在列表中的对象数组中获取每个列值

时间:2019-02-09 11:02:03

标签: java

我已将String个值存储在Object数组中。完成后,将对象添加到列表中,并且当我尝试从列表中获取存储的值时,仅获得最后一行。我已使用Object数组的增强型for循环作为数据类型和右侧的列表。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ReportOne{
public static String input_path = ("C:\\Users\\RAVI\\Desktop\\Skills\\inputs");
public static String output_path = ("C:\\Users\\RAVI\\Desktop\\Skills\\outputs");
static BufferedReader br;

public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("hh:MM.ss");
    SimpleDateFormat parsingSdf = new SimpleDateFormat("hh:MM.ss a");
    ArrayList<Object[]> list= new ArrayList<Object[]>();
    Object[] received = new Object[3];
        try{
            File fi = new File(input_path);
            File[] fileCount = fi.listFiles();
            for(int i = 0;i<fileCount.length;i++){
                File file = fileCount[i];
                if(file.isFile()){
                    System.out.println("Total file count : "+fileCount.length);
                    String fileName = file.getName();
                    System.out.println("File name : "+fileName);
                    String data;
                    br = new BufferedReader(new FileReader(input_path+"\\"+fileName));
                    while((data = br.readLine())!=null){
                        if(data.contains(">")){
                            String dat = data.substring(data.indexOf(" ")+1,data.indexOf("-")-1);
                            //System.out.println(dat);
                            Date date = sdf.parse(dat.substring(dat.indexOf(" "),dat.lastIndexOf(".")));
                            //System.out.println(date);
                            String timeFormat = parsingSdf.format(date);
                            //System.out.println(timeFormat);
                            received[0] = dat.substring(dat.indexOf("0"),dat.indexOf(" ")+1)+timeFormat;
                            //System.out.println(received[0]);
                            received[1] = data.substring(data.indexOf("<")+1,data.indexOf(",")-1);
                            //System.out.println(received[1]);
                            received[2] = data.substring(data.indexOf("Target"),data.lastIndexOf("."));
                            //System.out.println(received[2]);
                            list.add(received);
                        }
                    }

                }

            }

            for(Object[] data : list){
                System.out.println("-->  " +data[0]+" "+data[1]+" "+data[1]);
                //Unable to get all datas it is printing only last row from file
            }
        }catch(Exception e){
            e.printStackTrace();
        }
            }
        }

样本输入

(14581907) 06/29/2013 09:11.11.594 -->Successful password change for user=<U098095>, Password Target=<DOW>.
(14856413) 06/30/2013 21:39.35.837 -->Successful password change for user=<U545711>, Password Target=<a19mvs.nam.dow.com>.
(14858306) 06/30/2013 21:55.14.121 -->Successful password change for user=<NG34811>, Password Target=<a19mvs.nam.dow.com>.

输出

-->  06/30/2013 09:07.14 PM NG34811 NG34811
-->  06/30/2013 09:07.14 PM NG34811 NG34811
-->  06/30/2013 09:07.14 PM NG34811 NG34811

预期的输出不是打印最后一行的值,而要从输入文件获取三次,则需要从文件的每一行获取实际值。

1 个答案:

答案 0 :(得分:3)

每次添加到arraylist时都会初始化您的对象“接收”。

ie将您的Object []收到=新的Object [3];在if语句中