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

标签 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;
}


}

最佳答案

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

关于Android:在文件中保存一个条目而不是更多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593447/

相关文章:

java randomAccessFile 用每个字符之间的空格写每个东西

java - 未收到数据

Android启动WebBrowser Intent产生SecurityException

android - Mono for Android - 如何加速调试?

android - 适用于 Android 的 Crashlytics : Disable automatic upload of mapping file via Gradle build

android - Dalvik 字节码中的 "ins"和 "outs"是什么?

javascript - 从 Facebook 帖子打开 Chrome/Firefox (Android OS) 的 URL 方案

Android WIFI ADB 插件不适用于某些 Android 真实设备?

python - 如何使用 python io 库写入外部文件

C 编程——如何将文本文件内容放入我的数组中?