【发布时间】:2019-02-09 11:02:03
【问题描述】:
我已将 String 值存储在 Object 数组中。完成后,我将对象添加到列表中,当我尝试从列表中获取存储值时,我只得到最后一行。我使用了增强型 for 循环,将 Object 数组作为数据类型并在右侧列出。
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
预期的输出不是从输入文件中打印三次最后一行值,而是需要从文件中获取每一行的实际值。
【问题讨论】:
-
首先,你在 for 循环打印中有两次 data[1] - 对吗?
-
是的,我拥有以及我能够从文件中获取的所有值,我面临的唯一问题是从列表中获取时,该列表将所有值作为对象在 try 块的末尾保持准确我已经使用增强的 for 循环来打印那是我无法弄清楚的地方 @JGFMK 休息一切正常
-
您不需要 0,1 和 2 - 不是 0、1 和 1 吗?
-
我需要收到 [0]、1 和 2 @JGFMK,我刚刚了解了应该在 while 循环内的 arraylist 的声明,然后只有在拆分时新值到达时才会创建新对象文件中的字符串
-
您可能在那里遇到了多个问题 - 但我立即发现了我提到的问题。
标签: java