简体   繁体   English

使用Java中的indexOf()方法解析文件

[英]Parsing file using indexOf() method in Java

After verifing this post in stackoverflow I am using indexOf() method to parse values from file. 在stackoverflow中验证这篇文章后 ,我使用indexOf()方法来解析文件中的值。 Below is the format of my file- 以下是我档案的格式 -

10/05/2005 10:02;AM;a@xyz.com;student=student1 std=X marks=87 rollnumber=102
10/05/2005 10:05;AM;b@xyz.com;student=student2 std=IX rollnumber=26
10/05/2005 10:15;PM;c@xyz.com;student=student3 std=VII marks=87 attandance=5 rollnumber=12
10/05/2005 10:32;AM;d@xyz.com;student=student4 std=V marks=87 rollnumber=69

Note:The domain name ie xyz.com in email is not going to be changed in anywhere. 注意:电子邮件中的domain namexyz.com不会在任何地方更改。
Below is the code snippet i am using currently- 以下是我目前使用的代码片段 -

            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    fis));

            String line = "";
            while ((line = br.readLine()) != null) {

                int index = -1;

                if ((index = line.indexOf("xyz.com")) != -1) {
                    int inStudent = line.indexOf("student=", index);
                    int spaceExistsinStudent = -1;
                    int studentIndex = -1;

                    if ((spaceExistsinStudent = line.indexOf("student=\"", inStudent)) != -1)
                        studentIndex = line.indexOf(" ", inStudent);
                    else
                        studentIndex = line.indexOf("\" ", spaceExistsinStudent);

                    int inSTD = line.indexOf("std=", studentIndex);
                    int spaceExistsinSTD = -1;
                    int stdIndex = -1;

                    if ((spaceExistsinSTD = line.indexOf("std=\"", inSTD)) != -1)
                        stdIndex = line.indexOf(" ", inSTD);
                    else
                        stdIndex = line.indexOf("\" ", spaceExistsinSTD);

                    String studentName = line.substring(inStudent + 9, studentIndex);
                    String stdName = line.substring(inSTD + 4, stdIndex);

There is no need to paste the entire code. 无需粘贴整个代码。
Well, using the above implementation, i am able to work, but is this effective solution as performace is considered? 那么,使用上面的实现,我能够工作,但这是有效的解决方案,因为性能被考虑? Any better way for achiveing the same.... 任何更好的方法来实现同样的....
Thank you in advance. 先感谢您。

Instead of indexOf(), i would suggest StringTokenizer. 而不是indexOf(),我会建议StringTokenizer。 basically you can split you String based on some separator (eg: ;).. 基本上你可以根据一些分隔符(例如:;)分割你的字符串。

Example inside your while loop while循环中的示例

        StringTokenizer st = new StringTokenizer(line,";");


        st.nextToken(); //Date
        st.nextToken(); //AM
        String email = st.nextToken();
        String values = st.nextToken();

        StringTokenizer st2 = new StringTokenizer(values," ");


        while (st2.hasMoreElements()) {
            String token = (String) st2.nextElement();
            if(token.startsWith("student=")){
                System.out.println(token.substring("student=".length()));
            }else if(token.startsWith("std=")){
                System.out.println(token.substring("std=".length()));
            }

        }

You don't need to use indexOf for everything. 您不需要为所有内容使用indexOf。 If you want to look at one character, you can use charAt() eg where you are checking for a '"' 如果你想看一个字符,你可以使用charAt(),例如你要检查一个'''

I would use a method which extracts the value for a field to simplify the code. 我会使用一种方法来提取字段的值来简化代码。

As I stated in an earlier comment, I am surprised that the parsing is the bottleneck here. 正如我在之前的评论中所说,我很惊讶解析是这里的瓶颈。 But if you wish to know other ways that you could do this, and just try them out and see which is the fastest, here are two more ideas that haven't been posted- using .split : 但是如果你想知道其他方法可以做到这一点,只是尝试一下,看看哪个是最快的,这里还有两个没有发布的想法 - 使用.split

String[] arr1 = line.split(";");
String dateTime = arr1[0];
String ampm = arr1[1];
String email = arr1[2];
String[] arr2 = arr1[3].split(" ");
String student, std, marks, rollnumber;
student = std = marks = rollnumber = null;
for (String str : arr2) {
    String value = str.substring(str.indexOf("=") + 1);
    switch(str.charAt(2)) {
    case 'u': student = value; break;
    case 'd': std = value; break;
    case 'r': marks = value; break;
    case 'l': rollnumber = value; break;
    }
}

Or using a regex: 或使用正则表达式:

private static final Pattern PATTERN = Pattern.compile("([^;]+);([^;]+);([^;]+);student=([^ ]+) std=([^ ]+) marks=([^ ]+) rollnumber=([^ ]+)");

Matcher m = PATTERN.matcher(line);
m.find();
String dateTime = m.group(1);
String ampm = m.group(2);
String email = m.group(3);
String student = m.group(4);
String std = m.group(5);
String marks = m.group(6);
String rollnumber = m.group(7);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM