如何使用BufferedReader从平面文件读取数据,其值由新行分隔?

时间:2016-02-22 14:15:21

标签: java string split bufferedreader delimiter

平面文件中的数据如下所示

   START  
    Student.Number = 14  
    Student.Name = JACK  
    Student.Class = 9  
    END


    START  
    Student.Number = 15  
    Student.Name = EMILY  
    Student.Class = 10  
    END


            File myFile = new File("firstfile.txt");   
            FileReader fileReader = new FileReader(myFile);  
            BufferedReader reader= new BufferedReader(fileReader);   

            String line = null; //string that will hold the contents of the file
            while((line=reader.readLine())!=null)  
            {
                String[] token = line.split("START");
                //int number = Integer.parseInt(token[0].substring(14));
            //  token[0] = token[0].substring(14);
            //  String name = token[1].substring(12);
            //  token[1] = token[1].substring(12); 
                //int std = Integer.parseInt(token[2].substring(13));
            //  token[2] = token[2].substring(13);
} 

我想将数据放入Map中,然后根据他们的Student.Number对它们进行排序。

解决:使用以下方法提出解决方案。但是,如果可以做得更好,请告诉我。

    File myFile = new File("firstfile.txt"); 
        FileReader fileReader = new FileReader(myFile);
        BufferedReader reader= new BufferedReader(fileReader);



        String line = null; //string that will hold the contents of the file


        while((line=reader.readLine())!=null)
        {
            ///Do something
            if(line.equals("START"))
            {
                System.out.println("Header Present");
            }

            if(line.contains("Student.number="))
            {
                stuNum = line.substring(15);
                System.out.println(stuNum);

            }

            if(line.contains("Student.name="))
            {
                stuName = line.substring(13);
                System.out.println(stuName);

            }

            if(line.contains("Student.class="))
            {
                stuClass = line.substring(14);
                System.out.println(stuClass);

            }


            if(line.equals("END"))
            {
                System.out.println("Trailer Present");
                myList.add(new balak( 

                            Integer.parseInt(stuNum),
                            stuName,
                            Integer.parseInt(stuClass)



                        ));
            }
        }
        reader.close();
    }catch(IOException ie)
    {
        ie.printStackTrace();
    }

2 个答案:

答案 0 :(得分:1)

public class Student{
    private String name;
    private int number;
    private int class;
public Student(String name, int number, int class){
  this.name = name;
  this.number = number;
  this.class = class;
}
public String getName(){
        return name;
    }
public int getNumber(){
        return number;
    }
public String getClass(){
        return class;
    }

public void setName(String name){
        this.name=name;
    }
public void setNumber(int number){
        this.number=number;
    }
public void setClass(int class){
        this.class=class;
    }
}


ArrayList<Student> studentList= new ArrayList<String>();    
FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader(fr);
String line;
    while((line = textReader.readLine()) != null){
       if(line.equals("START")){
         Student st = new Student("",0,0);
          student.add(st);
        }else{
          StringTokenizer st = new StringTokenizer(line, "=");
          String title = st.nextElement();
          String data = st.nextElement();
          if(title.equals("Student.Number"))
             student.get(student.size-1).setNumber(data);
          if(title.equals("Student.Class"))
             student.get(student.size-1).setClass(data);
          if(title.equals("Student.Name"))
             student.get(student.size-1).setName(data);

        }
    }
    textReader.close();

然后查看this

我希望我能解决你的问题。

答案 1 :(得分:0)

这样你可以阅读以下几行:

final List<String> lines = new ArrayList<String>();
final BufferedReader reader = new BufferedReader(new FileReader(path_to_file));

String text = null;
while ((text = reader.readLine()) != null)
    lines.add(text);

然后,您将需要遍历寻找&#34; START&#34;和&#34;结束&#34;关键字。

评估关键字之间的线时,使用trim()删除空格和拆分(&#34; =&#34;)以将键与值分开。它将返回一个字符串数组。您应该使用第一个字符串作为地图的键,第二个字符串作为值。

我还建议您创建一个Java Bean来保存该信息。排序和管理它比使用地图列表更好。

在bean类中执行自己的equals方法实现,以便可以使用Collections.sort()方法。