无法从Java中另一个方法的Arraylist <object []>数据中获取全部数据

时间:2018-08-16 14:53:11

标签: java

我已经完成了从一种方法返回列表的操作,并试图从另一种方法接收列表数据以将其写入excel文件,但是它仅打印最后一行。 请帮助我我缺少的部分。提前致谢。 1.方法一readingFile正在读取文本文件 2.第二种方法是从方法一接收到的列表数据中写入文件

我的两种方法的代码如下

   package one;  
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 AutoPasswordReset 
{
    public String input_path = ("C:\\Users\\RAVI\\Desktop\\Skills\\inputs");
    public String output_path = ("C:\\Users\\RAVI\\Desktop\\Skills\\outputs");
    BufferedReader br;
    SimpleDateFormat sdf = new SimpleDateFormat("hh:MM.ss");
    SimpleDateFormat parsingSdf = new SimpleDateFormat("hh:MM.ss a");
    Object[] received = new Object[3];
    static ArrayList<Object[]> list= new ArrayList<Object[]>();

    public  ArrayList<Object[]>  readingFile() throws IOException{
        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);

                        }
                    }

                }
            }

        }catch(FileNotFoundException | ParseException fe){
            System.out.println(fe.getMessage());
        }
        return list;
    }
    public  void writingFile(ArrayList<Object[]> list){
        try{
             /*for(int i=0;i<list.size();i++){
                    Object[] obj = list.get(i);
                    System.out.println(obj[0]);
             }*/
            AutoPasswordReset apr = new AutoPasswordReset();
        ArrayList<Object[]> listTwo = apr.readingFile();
        for(Object[] data : listTwo){
            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();

        }

    }
    public static void main(String[] args) throws FileNotFoundException {
        AutoPasswordReset ar = new AutoPasswordReset();
        ar.writingFile(list);
        // Unable to pass as argument after changed to non static variable 

        try{
        }catch (Exception fnt){
            System.out.println(fnt.getMessage());
            fnt.printStackTrace();
        }

    }
}

0 个答案:

没有答案