Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用BufferedReader从带有新行分隔值的平面文件中读取数据?_Java_String_Split_Bufferedreader_Delimiter - Fatal编程技术网

Java 如何使用BufferedReader从带有新行分隔值的平面文件中读取数据?

Java 如何使用BufferedReader从带有新行分隔值的平面文件中读取数据?,java,string,split,bufferedreader,delimiter,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("firs

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

   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);
} 
我想把数据放在地图上,然后根据学生编号对它们进行排序

已解决:使用以下方法提出解决方案。不过,请让我知道,如果可以做得更好

    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();
    }
公共班级学生{
私有字符串名称;
私有整数;
私有int类;
公立学生(字符串名称、整数编号、整数类){
this.name=名称;
这个数字=数字;
this.class=class;
}
公共字符串getName(){
返回名称;
}
public int getNumber(){
返回号码;
}
公共字符串getClass(){
返回舱;
}
公共void集合名(字符串名){
this.name=name;
}
公共无效集合号(整数){
这个。数字=数字;
}
公共void集合类(int类){
这个类=类;
}
}
ArrayList studentList=新建ArrayList();
FileReader fr=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(fr);
弦线;
而((line=textReader.readLine())!=null){
如果(行等于(“开始”)){
学生st=新学生(“,0,0);
学生。添加(st);
}否则{
StringTokenizer st=新的StringTokenizer(行“=”);
字符串title=st.nextElement();
字符串数据=st.nextElement();
if(标题等于(“学生编号”))
student.get(student.size-1).setNumber(数据);
if(职称等于(“学生班级”))
student.get(student.size-1).setClass(数据);
if(title.equals(“Student.Name”))
student.get(student.size-1).setName(数据);
}
}
textReader.close();
然后再看一看


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

这样你就可以读到以下几行:

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);
final List line=new ArrayList();
final BufferedReader=new BufferedReader(new FileReader(路径_到_文件));
字符串文本=空;
而((text=reader.readLine())!=null)
行。添加(文本);
然后,您需要遍历这些行,查找“开始”和“结束”关键字

计算关键字之间的行时,请使用trim()删除空格,并使用拆分(“=”)将关键字与值分开。它将返回一个字符串数组。您应该使用第一个字符串作为映射的键,第二个字符串作为值

我还建议您创建一个Javabean来保存这些信息。对其进行排序和管理比使用地图列表更好


在bean类中,您可以自己实现equals方法,这样就可以使用Collections.sort()方法。

好的,那么您尝试过什么了吗?你逐行阅读,每次看到“开始”或“结束”时,你都会改变状态……你从文件中读取或将其放到地图上有困难吗?很好,你可以继续。当您编写了一些Java代码后,我们可能会帮助您解决任何问题。但每次我这样做,读者都无法阅读下一行。谢谢,谢谢你的帮助。昨晚我用了类似的方法,得到了我想要的。再次感谢。
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);