views:

104

answers:

3

Hi everyone

I wrote a simple java application, I have a problem please help me;

I have a file (JUST EXAMPLE):

1.TXT
-------
SET MRED:NAME=MRED:0,MREDID=60;
SET BCT:NAME=BCT:0,NEPE=DCS,T2=5,DK0=KOR;
CREATE LCD:NAME=LCD:0;
-------

and this is my source code

import java.io.IOException;
import java.io.*;
import java.util.StringTokenizer;

class test1 {

    private final int FLUSH_LIMIT = 1024 * 1024;
    private StringBuilder outputBuffer = new StringBuilder(
            FLUSH_LIMIT + 1024);
    public static void main(String[] args) throws IOException {
        test1 p=new test1();
        String fileName = "i:\\1\\1.txt";
        File file = new File(fileName);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, ";|,");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                p.processToken(token);

            }
        }
        p.flushOutputBuffer();
    }

    private void processToken(String token) {
        if (token.startsWith("MREDID=")) {
            String value = getTokenValue(token,"=");
            outputBuffer.append("MREDID:").append(value).append("\n");
        } else if (token.startsWith("DK0=")) {
            String value = getTokenValue(token,"=");
            outputBuffer.append("DK0=:").append(value).append("\n");
        } else if (token.startsWith("NEPE=")) {
            String value = getTokenValue(token,"=");
            outputBuffer.append("NEPE:").append(value).append("\n");
        }
        if (outputBuffer.length() > FLUSH_LIMIT) {
            flushOutputBuffer();
        }
    }

    private String getTokenValue(String token,String find) {
        int start = token.indexOf(find) + 1;
        int end = token.length();
        String value = token.substring(start, end);
        return value;
    }

    private void flushOutputBuffer() {
        System.out.print(outputBuffer);
        outputBuffer = new StringBuilder(FLUSH_LIMIT + 1024);
    }

}

I want this output :

MREDID:60
DK0=:KOR
NEPE:DCS

But this application show me this :

MREDID:60
NEPE:DCS
DK0=:KOR

please tell me how can i handle this , because of that DK0 must be at first and this is just a sample ; my real application has 14000 lines

Thanks ...

A: 

Create a class, something like

class data {
   private int mredid;
   private String nepe;
   private String dk0;

   public void setMredid(int mredid) {
      this.mredid = mredid;
   }

   public void setNepe(String nepe) {
      this.nepe = nepe;
   }

   public void setDk0(String dk0) {
      this.dk0 = dk0;
   }

   public String toString() {
      String ret = "MREDID:" + mredid + "\n";
      ret = ret + "DK0=:" + dk0 + "\n";
      ret = ret + "NEPE:" + nepe + "\n";
   }

Then change processToken to

private void processToken(String token) {
    Data data = new Data();
    if (token.startsWith("MREDID=")) {
        String value = getTokenValue(token,"=");
        data.setMredid(Integer.parseInt(value));
    } else if (token.startsWith("DK0=")) {
        String value = getTokenValue(token,"=");
        data.setDk0(value);
    } else if (token.startsWith("NEPE=")) {
        String value = getTokenValue(token,"=");
        data.setNepe(value);
    }
    outputBuffer.append(data.toString());
    if (outputBuffer.length() > FLUSH_LIMIT) {
        flushOutputBuffer();
    }
}
Buhb
Thanks but as I message ; my application has 14000 lines and setter not good for it ...
Mike Redford
+3  A: 

Instead of outputting the value when you read it, put it in a hashmap. Once you've read your entire file, output in the order you want by getting the values from the hashmap.

JRL
Nice call! I generally tune out when an American, who writes like a third-worlder, doesn't even explain what he is trying to do with his program.
gary
A: 

Use a HashTable to store the values and print from it in the desired order after parsing all tokens.

//initialize hash table
HashTable ht = new HashTable();

//instead of outputBuffer.append, put the values in to the table like
ht.put("NEPE", value);
ht.put("DK0", value); //etc

//print the values after the while loop
System.out.println("MREDID:" + ht.get("MREDID"));
System.out.println("DK0:" + ht.get("DK0"));
System.out.println("NEPE:" + ht.get("NEPE"));
Amarghosh
Thanks but with Hashtable i have low speed ...
Mike Redford