0
DurationOfRun:5
ThreadSize:10
ExistingRange:1-1000
NewRange:5000-10000
Percentage:55 - AutoRefreshStoreCategories  Data:Previous/30,New/70    UserLogged:true/50,false/50      SleepTime:5000     AttributeGet:1,16,10106,10111       AttributeSet:2060/30,10053/27
Percentage:25 - CrossPromoEditItemRule      Data:Previous/60,New/40    UserLogged:true/50,false/50      SleepTime:4000     AttributeGet:1,10107                AttributeSet:10108/34,10109/25
Percentage:20 - CrossPromoManageRules       Data:Previous/30,New/70    UserLogged:true/50,false/50      SleepTime:2000     AttributeGet:1,10107                AttributeSet:10108/26,10109/21

上記の .txt ファイルを解析しようとしています (最初の 4 行は修正され、最後の 3 行は 3 行以上になる可能性があることを意味します)。上記の.txtファイルを解析するより良い方法はありますか?また、パフォーマンスを考慮する場合、上記のtxtファイルを解析する最良の方法はどれですか.

private static int noOfThreads;
private static List<Command> commands;
public static int startRange;
public static int endRange;
public static int newStartRange;
public static int newEndRange;
private static BufferedReader br = null;
private static String sCurrentLine = null;
private static List<String> values;
private static String commandName;
private static String percentage;
private static List<String> attributeIDGet;
private static List<String> attributeIDSet;
private static LinkedHashMap<String, Double> dataCriteria;
private static LinkedHashMap<Boolean, Double> userLoggingCriteria;
private static long sleepTimeOfCommand;
private static long durationOfRun;

br = new BufferedReader(new FileReader("S:\\Testing\\PDSTest1.txt"));
values = new ArrayList<String>();

while ((sCurrentLine = br.readLine()) != null) {
    if(sCurrentLine.startsWith("DurationOfRun")) {
        durationOfRun = Long.parseLong(sCurrentLine.split(":")[1]);
    } else if(sCurrentLine.startsWith("ThreadSize")) {
        noOfThreads = Integer.parseInt(sCurrentLine.split(":")[1]);
    } else if(sCurrentLine.startsWith("ExistingRange")) {
        startRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
        endRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
    } else if(sCurrentLine.startsWith("NewRange")) {
        newStartRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[0]);
        newEndRange = Integer.parseInt(sCurrentLine.split(":")[1].split("-")[1]);
    } else {
        attributeIDGet =  new ArrayList<String>();
        attributeIDSet =  new ArrayList<String>();
        dataCriteria = new LinkedHashMap<String, Double>();
        userLoggingCriteria = new LinkedHashMap<Boolean, Double>();

        percentage = sCurrentLine.split("-")[0].split(":")[1].trim();
        values = Arrays.asList(sCurrentLine.split("-")[1].trim().split("\\s+"));
        for(String s : values) {
            if(s.startsWith("Data")) {
                String[] data = s.split(":")[1].split(",");
                for (String n : data) {
                    dataCriteria.put(n.split("/")[0], Double.parseDouble(n.split("/")[1]));
                }
                //dataCriteria.put(data.split("/")[0], value)
            } else if(s.startsWith("UserLogged")) {
                String[] userLogged = s.split(":")[1].split(",");
                for (String t : userLogged) {
                    userLoggingCriteria.put(Boolean.parseBoolean(t.split("/")[0]), Double.parseDouble(t.split("/")[1]));
                }
                //userLogged = Boolean.parseBoolean(s.split(":")[1]);
            } else if(s.startsWith("SleepTime")) {
                sleepTimeOfCommand = Long.parseLong(s.split(":")[1]);
            } else if(s.startsWith("AttributeGet")) {
                String[] strGet = s.split(":")[1].split(",");
                for(String q : strGet) attributeIDGet.add(q); 
            } else if(s.startsWith("AttributeSet:")) {
                String[] strSet = s.split(":")[1].split(",");
                for(String p : strSet) attributeIDSet.add(p); 
            } else {
                commandName = s;
            }
        }
        Command command = new Command();
        command.setName(commandName);
        command.setExecutionPercentage(Double.parseDouble(percentage));
        command.setAttributeIDGet(attributeIDGet);
        command.setAttributeIDSet(attributeIDSet);
        command.setDataUsageCriteria(dataCriteria);
        command.setUserLoggingCriteria(userLoggingCriteria);
        command.setSleepTime(sleepTimeOfCommand);
        commands.add(command);
4

3 に答える 3

0

若くて便利なクラスはスキャナーです。区切り文字を変更するだけで、必要な形式(readInt、readLong)でデータを一度に読み取ることができます。個別のx.parseX呼び出しは必要ありません。

2番目:コードを小さくて再利用可能な部分に分割します。それらはプログラムを読みやすくし、あなたは簡単に詳細を隠すことができます。

たとえば、範囲に構造体のようなクラスを使用することを躊躇しないでください。メソッドから複数の値を返すことは、ボイラープレート(getter、setter、ctor)なしで、これらによって実行できます。

import java.util.*;
import java.io.*;

public class ReadSampleFile
{
    // struct like classes:
    class PercentageRow {
        public int percentage;
        public String name;
        public int dataPrevious;
        public int dataNew;
        public int userLoggedTrue;
        public int userLoggedFalse;
        public List<Integer> attributeGet;
        public List<Integer> attributeSet;
    }
    class Range {
        public int from;
        public int to;
    }

    private int readInt (String name, Scanner sc) {     
        String s = sc.next (); 
        if (s.startsWith (name)) {
            return sc.nextLong ();
        }
        else err (name + " expected, found: " + s);     
    }

    private long readLong (String name, Scanner sc) {
        String s = sc.next (); 
        if (s.startsWith (name)) {
            return sc.nextInt ();
        }
        else err (name + " expected, found: " + s);     
    }

    private Range readRange (String name, Scanner sc) {
        String s = sc.next (); 
        if (s.startsWith (name)) {
            Range r = new Range ();
            r.from = sc.nextInt ();
            r.to = sc.nextInt ();
            return r; 
        }
        else err (name + " expected, found: " + s);
    }

    private PercentageLine readPercentageLine (Scanner sc) {
        // reuse above methods
        PercentageLine percentageLine = new PercentageLine ();
        percentageLine.percentage = readInt ("Percentage", sc);
        // ...
        return percentageLine;
    }

    public ReadSampleFile () throws FileNotFoundException
    {       
        /* I only read from my sourcefile for convenience. 
        So I could scroll up to see what's the next entry.                  
        Don't do this at home. :) The dummy later ...
        */ 
        Scanner sc = new Scanner (new File ("./ReadSampleFile.java"));
        sc.useDelimiter ("[ \n/,:-]");
        // ... is the comment I had to insert.
        String dummy = sc.nextLine (); 
        List <String> values = new ArrayList<String> ();
        if (sc.hasNext ()) {
            // see how nice the data structure is reflected 
            // by this code:  
            long duration = readLong ("DurationOfRun");         
            int noOfThreads = readInt ("ThreadSize");
            Range eRange = readRange ("ExistingRange");
            Range nRange = readRange ("NewRange");
            List <PercentageRow> percentageRows = new ArrayList <PercentageRow> ();
            // including the repetition ...
            while (sc.hasNext ()) {
                percentageRows.add (readPercentageLine ()); 
            }
        }
    }

    public static void main (String args[])  throws FileNotFoundException
    {
        new ReadSampleFile ();
    }

    public static void err (String msg)
    {
        System.out.println ("Err:\t" + msg);
    }
}
于 2012-05-29T03:28:23.647 に答える