0

I'm trying to create a clock in, clock out app, and thought the best way to go about this would be use a csv file.

//The code below over writes the entire CSV file //what I would like to happen is it appends the persons name and what time they clocked in so I can calcualte their pay at the end

public void onClick(View v) {
            Member = clock_In.getTag().toString();
            Date currentTime = Calendar.getInstance().getTime();
            String text = Member + " "+currentTime;
            FileOutputStream fos = null;
            try {
                fos = openFileOutput(FILENAME, MODE_PRIVATE);
                fos.write(text.getBytes());

                Toast.makeText(getApplicationContext(), "Saved to "+getFilesDir() + "/" + FILENAME,Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (fos!=null ){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
  • https://stackoverflow.com/questions/29248600/how-to-append-data-in-a-file-in-android fos.write(text.getBytes(),true) is what you need to append. This is a very unsecure method to calculate pay. A csv can be easy manipulated by everyone who has access to this device. A database could still be manipulated but not as easy by a person with not much experience as a csv. A better solution would be to upload the data to a server and there in a database. – Dominik Wuttke Oct 01 '20 at 21:24
  • @DominikWuttke it's fine cause this is a theoretical app for college – Zakariya Islam Oct 01 '20 at 21:31
  • Okay, than you only have to add a 'true' to your 'fos.write' which switches the write mode to append instead of replace. – Dominik Wuttke Oct 01 '20 at 21:33
  • @DominikWuttke it doesn't work. It says error "error: no suitable method found for write(byte[],boolean)" – Zakariya Islam Oct 01 '20 at 21:40
  • sorry my mistake, you need to open the file in append mode. https://developer.android.com/reference/java/io/FileOutputStream with this you can create a Fileoutputstream in append mode FileOutputStream(String name, boolean append) Creates a file output stream to write to the file with the specified name. – Dominik Wuttke Oct 01 '20 at 21:56
  • Why is it that after I fixed it I checked stack and seen your message from 15 min ago tell me the fix :) – Zakariya Islam Oct 01 '20 at 22:12

0 Answers0