Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:12:21+00:00 2026-06-08T03:12:21+00:00

I need to save the barcode with timestamp and save that in a .csv

  • 0

I need to save the barcode with timestamp and save that in a .csv file and send it to another device via Bluetooth. I am taking the barcode through SerialMagic Gear Keyboard option which take the barcode as an input

Problem: When the application run and i enter data, when the file is received by another device the file only contains the last entry.

I am sure that I am making some mistake in the structure of the program. If so kindly indicate where.

And sorry for the long code.

package com.android.app;

public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Timestamp for file name e.g. Data_7-19-2012
    SimpleDateFormat s1 = new SimpleDateFormat("MM-dd-yyyy_hh:mm:ss");
    final String format1 = s1.format(new Date()); 

    //Creating file name
    final String FileName = "Data_"+format1+".csv";


    final EditText addbarcode = (EditText) findViewById(R.id.editText1); 


    //getting file directory
    final File dir = getFilesDir();



    final File shareFile = new File(dir, FileName);

    addbarcode.setOnKeyListener(new OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    //case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:




                        //to get Timestamp
                        SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
                        String format = s.format(new Date());

                        //get barcode in a variable
                        Editable barcode = addbarcode.getText();


                        final String DATASTRING = new String(""+barcode+","+format+"\n");



                        FileOutputStream fOut = null;
                        try {
                             fOut = openFileOutput(FileName , MODE_WORLD_READABLE);
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        OutputStreamWriter osw = new OutputStreamWriter(fOut); 

                        // Write the string to the file
                        try {
                            osw.write(DATASTRING);
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        try {
                            osw.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            osw.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        /* ensure that everything is
                         * really written out and close */

                        //For showing data recived on screen


                    FileInputStream fIn = null;
                        try {
                            fIn = openFileInput(FileName);
                        } catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        InputStreamReader isr = new InputStreamReader(fIn);


                         //Prepare a char-Array that will
                         //* hold the chars we read back in. 
                        char[] inputBuffer = new char[DATASTRING.length()];

                        // Fill the Buffer with data from the file
                        try {
                            isr.read(inputBuffer);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        // Transform the chars to a String
                        String readString = new String(inputBuffer);
                        TextView etData= (TextView)findViewById(R.id.textView3);

                        etData.setText(readString);



                        //Clear the editview for new entries
                        addbarcode.setText("");                     

                        return true;
                    default:
                        break;
                }
            }
            return false;
        }
    });



    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Uri u1  =   null;
            u1  =   Uri.fromFile(shareFile);

            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
            sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
            sendIntent.setType("text/csv");
            startActivity(sendIntent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:12:23+00:00Added an answer on June 8, 2026 at 3:12 am

    You need to use the MODE_APPEND flag in openFileOutput, otherwise the old file contents gets overwritten by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to save table to CSV file. Each cell of table could contains
I need to save data array to CSV file. The problem is the following:
.txt file need save in SD-card. Save(n + String.valueOf(key) + .txt); private void Save(String
I need to save to an isolated storage the total time that the user
I need to save some classes and data structures to a file. My first
I need to save all .xml file names in a directory to a vector.
Is it possible? I need save the sreen and send from iPad to a
I need to save private data as a world readable file in order to
I need to save output in Python. Example: import os ob_start(); os.system( 'c:\files\file.exe' );
i need to save a xml file from a server.. i found a code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.