java - 改进解析文本文件的代码

标签 java file parsing file-io

文本文件(前三行简单易读,后三行以p开头)

ThreadSize:2
ExistingRange:1-1000
NewRange:5000-10000
p:55 - AutoRefreshStoreCategories  Data:Previous    UserLogged:true    Attribute:1    Attribute:16      Attribute:2060  
p:25 - CrossPromoEditItemRule      Data:New         UserLogged:false     Attribute:1      Attribute:10107   Attribute:10108
p:20 - CrossPromoManageRules       Data:Previous    UserLogged:true      Attribute:1      Attribute:10107   Attribute:10108

下面是我为解析上述文件而编写的代码,解析后我使用它的 Setter 设置相应的值。我只是想知道我是否可以通过使用其他方式(如使用 RegEx)在解析和其他方面更多地改进这段代码?我的主要目标是解析它并设置相应的值。我们将不胜感激任何反馈或建议。

private List<Command> commands;
private static int noOfThreads = 3;
private static int startRange = 1;
private static int endRange = 1000;
private static int newStartRange = 5000;
private static int newEndRange = 10000;
private BufferedReader br = null;
private String sCurrentLine = null;
private int distributeRange = 100;
private List<String> values = new ArrayList<String>();
private String commandName;
private static String data;
private static boolean userLogged;
private static List<Integer> attributeID =  new ArrayList<Integer>();

    try {
        // Initialize the system
        commands = new LinkedList<Command>();
        br = new BufferedReader(new FileReader("S:\\Testing\\Test1.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            if(sCurrentLine.contains("ThreadSize")) {
                noOfThreads = Integer.parseInt(sCurrentLine.split(":")[1]);
            } else if(sCurrentLine.contains("ExistingRange")) {
                startRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
                endRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
            } else if(sCurrentLine.contains("NewRange")) {
                newStartRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
                newEndRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
            } else {
                allLines.add(Arrays.asList(sCurrentLine.split("\\s+")));
                String key = sCurrentLine.split("-")[0].split(":")[1].trim();
                String value = sCurrentLine.split("-")[1].trim();
                values = Arrays.asList(sCurrentLine.split("-")[1].trim().split("\\s+"));
                for(String s : values) {
                    if(s.contains("Data:")) {
                        data = s.split(":")[1];
                    } else if(s.contains("UserLogged:")) {
                        userLogged = Boolean.parseBoolean(s.split(":")[1]);
                    } else if(s.contains("Attribute:")) {
                        attributeID.add(Integer.parseInt(s.split(":")[1]));
                    } else {
                        commandName = s;
                    }
                }

                Command command = new Command();
                command.setName(commandName); 
                command.setExecutionPercentage(Double.parseDouble(key));
                command.setAttributeID(attributeID);
                command.setDataCriteria(data);
                command.setUserLogging(userLogged);
                commands.add(command);

            }
        }
    } catch(Exception e) {
        System.out.println(e);
    }

最佳答案

我认为您应该知道在使用 RegEx 时您到底期望什么。 http://java.sun.com/developer/technicalArticles/releases/1.4regex/应该会有帮助。

关于java - 改进解析文本文件的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658419/

相关文章:

JavaFx - 在警告对话框中包装内容文本

java - 机器人java.io.IOException : Transport endpoint is not connected

JAVA:使用 YouTube Live Streaming API 创建新的直播 Activity

c - 有没有更好的方法来管理C中的文件指针?

php - 是否可以从 php 中的转发电子邮件中检索原始 header

javascript - 如何使用URL从xml中获取数据

java - 我可以使用 Guice 在方法中获取实例吗?

email - swiftmailer::发送虚拟文件?

python - 通过预先应用的参数 move 多个文件

android - 使用 jsoup 解析抛出错误(NetworkOnMainThreadException)