• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Get value and compare it to file content same value

LefmyHans

Lurker
Hi ! I want to create a small program with android studio that can take a value entered by a user and compare that value to another identical value that exists in a file stored in the internal memory of the phone. If this value exists in the file, select the file in question and send it to a server... Could someone help me with that ?
 
Hi ! I want to create a small program with android studio that can take a value entered by a user and compare that value to another identical value that exists in a file stored in the internal memory of the phone. If this value exists in the file, select the file in question and send it to a server... Could someone help me with that ?

I'll post some code I use in my own apps to get you started. The rest you can figure out on your own with a little googling and asking around.

The BufferedReader is perfect for this situation. You can use it to get the contents of a file on your phone and compare the contents to a string which is what it seems you're trying to do.

Method to read file contents - getFileContent():
Java:
private static String getFileContent(String file) {
  String content = "";
  try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    while (line != null) {
      sb.append(line);
      line = br.readLine();
      if (line != null) {
        sb.append(System.lineSeparator());
      }
    }
    content = sb.toString();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return content;
}

Using the method getFileContent():
Java:
if (getFileContent("/path/to/filename").contains("string-to-check")) {
  // run your code here for a match
} else {
  // run code here for no match
}

Good luck ;)
 
Back
Top Bottom