I have done returning list from one method and trying to receive the list data from another method to write in excel file but it is printing only last row. please help me where i am missing the part. thanks in advance. 1.Method one readingFile is reading the text file 2. Second method is to writing file from the list data received from method one
My code with two methods are as follows
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();
}
}
}
>`.
– MC Emperor Aug 16 '18 at 16:25