我从文件ID,公司名称和日期中得到了三个不同的子字符串
检索时,我需要将按日期值排序的对象存储到对象中。
我已检索并将字符串转换为我需要并存储的日期格式。 而不是每次都使用sql排序再次拉动,而是尝试在插入之前存储按日期排序的排序。
class ReadingFile
{
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;
void read(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM.ss");
SimpleDateFormat parsingSdf = new SimpleDateFormat("hh:MM.ss a");
ArrayList<Object[]> list = new ArrayList<Object[]>();
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(">"))
{
Object[] received = new Object[3];
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);
}
}
}
}
在下面添加了示例输入和相应的输出
input
(8834675) 06/01/2013 04:03.36.562 -->Successful password change for user=<U753838>, Password Target=<DOW>.
(8858218) 06/01/2013 07:18.42.312 -->Successful password change for user=<U640630>, Password Target=<DOW>.
(8893874) 06/01/2013 12:14.42.410 -->Successful password change for user=<U090521>, Password Target=<DOW>.
输出
06/01/2013 04:03.36 AM U753838 Target=<DOW>
06/01/2013 07:06.42 AM U640630 Target=<DOW>
06/01/2013 12:02.42 AM U090521 Target=<DOW>
答案 0 :(得分:0)
请尝试一次。应该做事。
public class FileWriter {
public static void main(String[] args) throws Exception{
try {
BufferedReader br = new BufferedReader(new FileReader
(new File("Path of sample Data")));
List<String> list = new ArrayList<>();
br.lines().forEach(s -> {
System.out.println(s);
Arrays.stream(s.split("\\)")).skip(1).forEach(s1 -> {
String[] strArr = s1.split("-->");
String p2 = s1.split("=<")[1].split(">")[0];
String p3 = s1.split("Password")[1].trim();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm.ss.SSS");
list.add(strArr[0].trim() + " " + p2 + " " + p3);
Collections.sort(list, (d1, d2) -> {
try{
return sdf.parse(d1.substring(0, 21)).compareTo(sdf.parse(d2.substring(0, 21)));
}
catch (ParseException e) {
e.printStackTrace();
}
return 0;
});
});
});
list.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
一种简单的方法是使用这样的ISO日期格式:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat iso = new SimpleDateFormat("yyyy-dd-MM");
...
BufferedReader br = new BufferedReader(new FileReader(fileName));
while ((data = br.readLine()) != null) {
if (data.contains(">")) {
String[] parts = data.split(" ");
Date date = sdf.parse(parts[1]);
String newDate = iso.format(date);
StringBuilder newLine = new StringBuilder(newDate);
for (int i = 2; i < parts.length; i++) {
newLine.append(" ").append(parts[i]);
}
System.out.println(newLine);
list.add(newLine.toString());
}
}
br.close();
这将导致
2013-06-01 04:03.36.562 -->Successful password change for user=<U753838>, Password Target=<DOW>.
2013-06-01 07:18.42.312 -->Successful password change for user=<U640630>, Password Target=<DOW>.
2013-06-01 12:14.42.410 -->Successful password change for user=<U090521>, Password Target=<DOW>.
可以很容易地用Collections.sort(list);
答案 2 :(得分:0)
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 ReadingFile {
public static String input_path = ("D:\\Aritra\\inputs");
public static String output_path = ("D:\\Aritra\\outputs");
static BufferedReader br;
void read() {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm.ss");
SimpleDateFormat parsingSdf = new SimpleDateFormat("hh:mm.ss a");
ArrayList<Object[]> list = new ArrayList<Object[]>();
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(">")) {
Object[] received = new Object[3];
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);
boolean isAdded = false;
if (!list.isEmpty()) {
Date currentEntry = new SimpleDateFormat("dd/MM/yyyy hh:mm.ss a")
.parse((String) received[0]);
int count = 0;
do {
Object[] loopElement = list.get(count);
Date loopElementDate = new SimpleDateFormat("dd/MM/yyyy hh:mm.ss a")
.parse((String) loopElement[0]);
int compareResult = currentEntry.compareTo(loopElementDate);
if(compareResult<=0) {
list.add(count,received);
isAdded = true;
break;
}
count++;
} while (count < list.size());
}
if(!isAdded) {
list.add(received);
}
}
}
}
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("<<<<<<<<<<<<<< Result >>>>>>>>>>>>>>>>");
for(Object[] l:list) {
for(Object l1:l) {
System.out.print("["+l1+"]");
}
System.out.println();
}
}
public static void main(String[] args) {
new ReadingFile().read();
}
}