如何将输出放在ArrayList中的文件中

时间:2016-06-06 13:15:20

标签: java arrays arraylist bufferedreader

我正在尝试将我的数据分成多个数组列表,以便我可以在以后的代码中使用它们。但是我无法将数据放入数组列表中。

我的代码是关于根据各种过滤器将数据分成不同主题的三个数组列表(例如:物理,化学),您可以在我的代码中找到它。

Input data file:

1|150|20150328|20150406|Physics|1600|1600|2|68|92
2|152|20150328|20150406|Physics|1600|1500|2|68|89
3|153|20150328|20150406|Physics|1600|1500|2|68|60
4|155|20150328|20150406|Physics|1600|1600|2|68|72
5|161|20150328|20150406|Chemistry|1600|1600|2|68|77

这是我的代码:

Public Class filter{
  public static void main(String args[])

BufferedReader in= null;
BufferedWriter out= null;
String in_line;
String PrevRollNo= "";
int PrevDate= 0;

ArrayList<transaction> PhysicsList= new ArrayList<transaction>();
ArrayList<transaction> scList= new ArrayList<transaction>();
ArrayList<transaction> Chemistry= new ArrayList<transaction>();

try{
in = new BufferedReader(new FileReader(Path for input file));
File out_file= new File(Path for output file);
if(!out_file.exists())
{
(!out_file.createNewFile();
}

 FileWriter fw=new FileWriter(out_file);
out= new BufferedWriter(fw);

while ((in_line=in.readline())!=null)
{
Transaction transact=new Transaction(in_line);

if(transact.RollNo.equals(PrevRollNo))
{
if(transact.subject.equals("Physics")&& transact.Prs_Date= PrevDate
{
PhysicsList.add(transact);
}
else if(transact.subject.equals("Physics")&&transact.wk_date != PrevDate}

Iterator<Transaction> it;
if(!transact.RoomNo.equals("102")&&!transact.lcl_RoomNo.equals("102");

{
it= scList.iterator();
while(it.hasnext())
{
Transaction sc= it.next();

if(sc.lcl_RoomNo.equals(transact.RoomNo) && sc.l1 equals(tansact.l1) && sc.l2 equals(transact.l2)

if(sc.marks==transact.marks)
{
transact.srsfound= true;
}
else
{
System.out.print.ln( "not found");
}
scList.remove(sc))
out.write(in_line);
break;
}}}}

Static Class Transaction
{
Public String RollNo, Subject, RoomNo, lcl_RoomNo, l1, l2;

Public int wk_date, prs_date;
Public double marks , amt;
Public boolean srcfound, tgtfound;

Public Transaction(String in_line)
{
String [] SplitData= in_line.split("\\|");
RollNo = SplitData[1];
Subject = SplitData[4]
RoomNo = SplitData[5];
lcl_RoomNo = SplitData[6];
l1 = SplitData[7];
l2 = SplitData[8];
wk_date = SplitData[3];
prs_date = SplitData[2];

marks = Double.parsedouble(SplitData[9]);
amt = Double.parsedouble(SplitData[]);
srcfound = false;
tgtfound = false;
}

请帮助您的专业知识。

2 个答案:

答案 0 :(得分:1)

使用Java 8 NIO和Streams。这将使工作变得轻松。

Files.lines(Paths.get("fileName.txt")).map(line -> {
    String[] tokens = line.split("|");
    //tokens contains individual elements of each line. Add your grouping logic on tokens array
}

答案 1 :(得分:0)

我在某些方面同意其他答案。应该使用NIO,它会使它变得容易多了。但是,我会避免使用stream而是使用readAllLines方法,如下所示:

try{
    List<String> filecontents = new String(Files.readAllLines(file.toPath()); //file is the object to read from.
    for(int i = 0; i < filecontents.size(); i++){
        String line = lines.get(i);
        //New code starts here
        if(!line.contains("|") continue; //Ignore that line
        //New code ends here
        String[] array = line.split("|");
        ArrayList<String> list = new ArrayList<String>();
        for(int a = 0; a < array.length; a++){
            String part = array[a];
            list.add(part);
        }
        Transaction t = new Transaction(line);
        if(line.contains("Physics") PlysicsList.add(t);
        else if(line.contains("Chemistry") Chemistry.add(t);
        else{ //Do nothing}
    }
}catch(IOException e){
    e.printStackTrace();
}
编辑:我在那里添加了一张支票。第一行和最后一行可能无法正常工作的原因是,正在解析的行未正确解析。看看这是否可以解决您的问题