1. Are you ready for the Galaxy S20? Here is everything we know so far!

Get value and compare it to file content same value

Discussion in 'Android Lounge' started by LefmyHans, Sep 12, 2019.

  1. LefmyHans

    LefmyHans Lurker
    Thread Starter

    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 ?
     


  2. MoodyBlues

    MoodyBlues Compassion is cool!
    VIP Member

    Hi there, @LefmyHans, and welcome to Android Forums!

    We have a board dedicated to Android Development. That's where you're most likely to get the help you need.

    Good luck with your project!
     
    GameTheory likes this.
  3. GameTheory

    GameTheory Android Expert

    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():
    Code (Java):
    1. private static String getFileContent(String file) {
    2.   String content = "";
    3.   try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    4.     StringBuilder sb = new StringBuilder();
    5.     String line = br.readLine();
    6.     while (line != null) {
    7.       sb.append(line);
    8.       line = br.readLine();
    9.       if (line != null) {
    10.         sb.append(System.lineSeparator());
    11.       }
    12.     }
    13.     content = sb.toString();
    14.   } catch (IOException e) {
    15.     e.printStackTrace();
    16.   }
    17.   return content;
    18. }
    Using the method getFileContent():
    Code (Java):
    1. if (getFileContent("/path/to/filename").contains("string-to-check")) {
    2.   // run your code here for a match
    3. } else {
    4.   // run code here for no match
    5. }
    Good luck ;)
     
Loading...
Similar Threads - value compare file
  1. Samsung
    Replies:
    0
    Views:
    169
  2. gmrcs
    Replies:
    1
    Views:
    667
  3. Ryosuke Matsuzaka
    Replies:
    1
    Views:
    4,018
  4. kumaranil13k
    Replies:
    2
    Views:
    342
  5. ralaitojo7
    Replies:
    0
    Views:
    469
  6. wat eber
    Replies:
    2
    Views:
    805
  7. Jay Roa
    Replies:
    2
    Views:
    3,510
  8. irajfacebook84
    Replies:
    0
    Views:
    333
  9. Chirayu Bansal
    Replies:
    0
    Views:
    3,254

Share This Page

Loading...