1

I have a class which extends TreeMap with one external method. The external method "open" suppose to read lines from a given file in the following format "word:meaning" and add it to the TreeMap - put("word", "meaning").

So I read the file with RandomAccessFile and put the keys-values in the TreeMap and when I print the TreeMap I can see the proper keys and values, for example:

{AAAA=BBBB, CAB=yahoo!}

But for some reason when I do get("AAAA") I get null.

Any reason why it's happening and how to solve it?

Here is the code

public class InMemoryDictionary extends TreeMap<String, String> implements
    PersistentDictionary {
private static final long serialVersionUID = 1L; // (because we're extending
                                                    // a serializable class)
private File dictFile;

public InMemoryDictionary(File dictFile) {
    super();
    this.dictFile = dictFile;
}

@Override
public void open() throws IOException {     
    clear();
    RandomAccessFile file = new RandomAccessFile(dictFile, "rw");
    file.seek(0);
    String line;
    while (null != (line = file.readLine())) {
        int firstColon = line.indexOf(":");
        put(line.substring(0, firstColon - 1),
                line.substring(firstColon + 1, line.length() - 1));
    }       
    file.close();
}

@Override
public void close() throws IOException {    
    dictFile.delete();
    RandomAccessFile file = new RandomAccessFile(dictFile, "rw");       
    file.seek(0);
    for (Map.Entry<String, String> entry : entrySet()) {            
        file.writeChars(entry.getKey() + ":" + entry.getValue() + "\n");
    }
    file.close();
}

}

10
  • 5
    For better help sooner, post an SSCCE. Don't include sigs. in questions, they are noise. This question has some very strange characters in the text of the key/value. Commented Mar 18, 2013 at 19:06
  • Please provide a minimal, complete example which illustrates your problem. Commented Mar 18, 2013 at 19:08
  • {�A�A�A�A=�B�B�B�B, �C�A�B=�y�a�h�o�o�!}..What is this?? Commented Mar 18, 2013 at 19:10
  • ignore the questions mark, refresh the page... Commented Mar 18, 2013 at 19:11
  • 2
    @gilsilas: Can you also post the code, where you print {AAAA=BBBB, CAB=yahoo!} and after that receive null for .get("AAAA"); ? Commented Mar 18, 2013 at 19:23

1 Answer 1

2

the "question marks" from a previous version of your question are important. they indicate that the strings you thought you were seeing are not in fact the strings you are using. RandomAccessFile is a poor choice to read a text file. You are presumably reading a text file with a text encoding which is not single byte (utf-16 perhaps)? the resulting strings are mis-encoded since RandomAccessFile does an "ascii" character conversion. this is causing your get() call to fail.

first, figure out the character encoding of your file and open it with the appropriately configured InputStreamReader.

second, extending TreeMap is a very poor design. Use aggregation here, not extension.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.