-3

I'm having difficulty performing what should be a simple task. Let's say I have a text file that states:

a = b

I want my program to read this file and output "b" whenever the user inputs "a". In Python or C++ I can accomplish this in around 7 lines or less.

However, I'm having difficulty finding a simple way to do this on Android. For example, one sample that I found here on SO had nearly 900 lines in 6 files.

Is there a simple way to parse a file and return a variable on Android that I am missing?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Joe
  • 3
  • 1
  • 5
    The Gettysburg address is actually quite short, as it's ~20 lines with 80 char columns and a monospaced font. – dave.c Jan 25 '11 at 20:39
  • Additionally, I would like this supposed answer on SO with 900 LOC. I can't seem to find it... – Anon. Jan 25 '11 at 20:44
  • Sounds like you want to extract stuff from a file (see Anon's answer), then use a HashMap. – Rooke Jan 25 '11 at 20:47
  • So where do you want the 'output' to be? In a TextView? On a button? In the status bar? Where's the 'file'? On the SD card? On a server somewhere? Android provides you with a framework for a GUI on a phone, you must expect an overhead that comes with that. It's not there for providing output to console apps – NickT Jan 25 '11 at 20:52
  • 2
    @Joe Please read through the FAQs at the top on the best way to construct a question. You will get much better help if you phrase your question as an actual question with only technical details related to the question. Please avoid whining and ranting. – Cheryl Simon Jan 25 '11 at 21:21
  • I've edited the question to be more focused and less inflammatory. I think you'll find that you'll get a better response from other contributors using more objective wording. – Brad Larson Jan 26 '11 at 18:45

3 Answers3

12
BufferedReader r = new BufferedReader(new FileReader(filename));
string s;
while(s = r.readLine()) {
    //Oh hey, I got a line out of the file.
    //In three lines of code.
    //So what's all this Android-bashing about?
}
Anon.
  • 58,739
  • 8
  • 81
  • 86
4

As long as you're happy with thea = b format used in properties files, you get 99% of the way to your goal with

Properties properties = new Properties();
try {
    properties.load(new FileInputStream(filename));
} catch (IOException e) {
    // the file is missing or is not in 'a = b' format
}

The, having got a variable key containing the string "a" from the user, the result of properties.getProperty ( key ) will equal "b" if the file contains the line a = b. I'm pretty sure you need more than that in C++ to load a map from a file and handle all the escaping and character encoding issues.

If the properties are held in a text file called mappings.properties in the assets folder of your Android project rather than in the user's file system, then you get at it like this:

    final AssetManager am = getResources().getAssets();
    final Properties properties = new Properties();

    try {
        properties.load( am.open("mappings.properties"));
    } catch (IOException e) {
        // the file is missing or is not in 'a = b' format
    }

This next bit is borrowed from the android tutorial to show a toast message with 'b' in it if 'a' is entered in the edit box. Maybe here is where you get your line count from, as setting up a GUI with XML files and adding listeners in Java is fairly verbose compared with other languages. That's due to Java and XML syntax rather than the virtual machine.

    final EditText edittext = (EditText) findViewById(R.id.edittext);

    edittext.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              Toast.makeText(YourOuterViewClass.this, 
                  properties.getProperty(edittext.getText().toString()),
                  Toast.LENGTH_SHORT).show();
              return true;
            }
            return false;
        }
    });
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
2

You definitely don't need hundreds of lines of code to achieve this. It can be done in a few lines. I don't know what examples you were looking at but they were probably doing way more than what you are describing.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200