如何将数据附加到android中的文件

时间:2015-03-24 12:28:54

标签: android file append

我正在创建一个包含注册表单的Android应用程序。如果单击登录按钮,则会创建一个日志文件,其中包含用户名和登录时间。我成功创建了文件。但是我要附加数据一个EROFS例外。请帮我解决这个问题。

这是我的家庭活动:

package com.techblogon.loginexample;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HomeActivity extends Activity 
{
    Button btnSignIn,btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;
    String userName,password,filename;
    FileOutputStream fileoutputstream;
    File user_data;
    StringBuffer string_buffer;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

        SimpleDateFormat simpledate =new SimpleDateFormat("dd-mm-yyyy--hh:mm:ss");
                        filename = simpledate.format(new Date());


         // create a instance of SQLite Database
         loginDataBaseAdapter=new LoginDataBaseAdapter(this);
         loginDataBaseAdapter=loginDataBaseAdapter.open();

         // Get The Refference Of Buttons
         btnSignIn=(Button)findViewById(R.id.buttonSignIN);
         btnSignUp=(Button)findViewById(R.id.buttonSignUP);

        // Set OnClick Listener on SignUp button 
        btnSignUp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
            startActivity(intentSignUP);
            }
        });
    }
    // Methos to handleClick Event of Sign In Button
    public void signIn(View V)
       {
            final Dialog dialog = new Dialog(HomeActivity.this);
            dialog.setContentView(R.layout.login);
            dialog.setTitle("Login");

            // get the Refferences of views
            final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
            final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

            Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

            // Set On ClickListener
            btnSignIn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // get The User name and Password
                    userName=editTextUserName.getText().toString();
                    password=editTextPassword.getText().toString();

                    // fetch the Password form database for respective user name
                    String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                    // check if the Stored password matches with  Password entered by user
                    if(password.equals(storedPassword))
                    {
                        Toast.makeText(HomeActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                        File UserDirectory = new File("/sdcard/Panelusers/"+userName);
                        UserDirectory.mkdirs();
                        user_data = new File("/sdcard/Panelusers/" + userName + "/log.txt");
                        try {
                            fileoutputstream = new FileOutputStream(user_data);
                            fileoutputstream = openFileOutput(userName, Context.MODE_PRIVATE);
                            PrintStream print_data = new PrintStream(fileoutputstream);
                            print_data.print(userName + "-------" + filename);
                            fileoutputstream.close();
                        }

                        catch (Exception e)
                        {
                            Toast.makeText(HomeActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                        if(!user_data.exists()) {
                            user_data = new File("/sdcard/Panelusers/" + userName + "/log.txt");
                        }
                        try
                        {

                            FileWriter file_writer = new FileWriter(user_data.getName(),true);

                            BufferedWriter buffered_writer = new BufferedWriter(file_writer);
                            buffered_writer.newLine();
                            buffered_writer.write("/n" +userName + "--------" + filename);
                            buffered_writer.close();

                        }
                        catch(Exception e)
                        {
                            Toast.makeText(HomeActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
                        }

                    }


                    else
                    {
                        Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                    }
                }
            });

            dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Close The Database
        loginDataBaseAdapter.close();
    }
}

0 个答案:

没有答案