Android:在文件中保存一个条目而不是更多数据

时间:2012-07-21 15:28:45

标签: android file-io android-intent

我需要使用时间戳保存条形码并将其保存在.csv文件中,然后通过蓝牙将其发送到其他设备。我正在通过SerialMagic Gear Keyboard选项获取条形码,该条目将条形码作为输入

问题:当应用程序运行并输入数据时,当其他设备收到该文件时,该文件只包含最后一个条目。

我确信我在程序结构中犯了一些错误。如果善意地指出在哪里。

抱歉长码。

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 个答案:

答案 0 :(得分:2)

您需要在openFileOutput中使用MODE_APPEND标志,否则默认会覆盖旧文件内容。