Results 1 to 4 of 4
  1. #1
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default How can I read the new content of a text file while the application is running.

    Hello! So I am writing integers to a txt file and I want the sum of those integers to show up via a textView.The problem is that the sum is not updating while the application is running.The sum is updating only when I start the application.I use a scanner to read the txt file when the application starts and I use the same scanner to read the same file every time a integer is added.Thank you!

    Java Code:
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            editText = (EditText) findViewById(R.id.editText);
            button_2 = (Button) findViewById(R.id.scrie);
            textView = (TextView) findViewById(R.id.textView);
            textView_2 = (TextView) findViewById(R.id.textView_2);
    
            try {
                scanner = new Scanner(myFile_1);
                var_2 = 0;
    
                while(scanner.hasNextInt()) {
    
                    int var_1 = scanner.nextInt();
                    var_2 += var_1;
                }
            }catch (Exception e){}
         
            textView_2.setText("Total fuel added is : " + var_2);
    
        }
    
    
    
        public void button_1(View v){
            try{
                File myFile  = new File(path + "/fuel_1.txt");
                FileOutputStream fop = new FileOutputStream(myFile, true);
                String f = editText.getText() + "\n";
                byte[] contentInBytes = f.getBytes();
    
                fop.write(contentInBytes);
                fop.flush();
                fop.close();
    
                editText.getText().clear();
                while (scanner.hasNextInt()) {
                    int var_1 = scanner.nextInt();
                        var_2 += var_1;
                    }
    
    
                System.out.println("update");
    
            }catch (Exception e){}
            textView_2.setText("Total fuel added is : " + var_2);
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How can I read the new content of a text file while the application is running.

    Have you tried debugging the code to see what it is doing? print statements will be shown in the logcat.

    Also it's a bad idea to ignore exceptions. Add a call to the printStackTrace() method to all the catch blocks.

    It's possible the Scanner object thinks that it has read all the data from the file and won't see any further updates to the file. Have the code in button_1 read the whole file instead of trying to continue reading.
    Last edited by Norm; 05-28-2016 at 02:51 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default Re: How can I read the new content of a text file while the application is running.

    How would I get buton_1 read the whole file? I thought about it but I can't figure it out.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How can I read the new content of a text file while the application is running.

    I'm confused about why the code in button_1 needs to read any more of the file. If the data it is to read has just been written to the file on line 34, there is no need to read the file. Just use the data from line 34.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  2. How to read content from a file
    By eden83 in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 10:25 PM
  3. Read text content from pdf using pdfbox
    By cssatheesh in forum Advanced Java
    Replies: 0
    Last Post: 09-22-2009, 08:54 PM
  4. how to read content of .xls file
    By kirtichopra2003 in forum Advanced Java
    Replies: 10
    Last Post: 09-11-2009, 01:03 PM
  5. Read the content of file from a specified path
    By shiva in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 07:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •