• 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

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 ;)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones