简体   繁体   中英

How to store and pull internally on android?

I am having trouble trying to store a username string internally on an android devices. My code is written to store the username and read it into a string and then display that string in a toast text box. I'm having errors creating the bold_username.txt file. Here is my code so far:

package hello.tab.widget;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

public class DisclaimerActivity extends Activity
{

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.disclaimer);

final EditText et = (EditText)findViewById(R.id.et_username);
et.setOnClickListener(new View.OnClickListener() {



@Override
public void onClick(View v) {
et.setText("");

}
});

final Button button = (Button) findViewById(R.id.btn_accept);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox checkBox = (CheckBox)findViewById(R.id.cb_disclaimer);

if (checkBox.isChecked()) {

String username = et.getText().toString();

String FILENAME = "bold_username.txt";
try {
FileOutputStream fos = new FileOutputStream(new File("bold_username.txt"));
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(username.getBytes());
fos.close();
InputStream inputStream = new FileInputStream("bold_username.txt");
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
Toast.makeText(DisclaimerActivity.this, line,
Toast.LENGTH_SHORT).show();
}

}


catch (IOException e){
e.printStackTrace();
}

}
else {
Toast.makeText(DisclaimerActivity.this, "Please Check the
Checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}


public void onCheckboxClicked(View v) {
}
}

Try this

//Write the data
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
OutputStreamWriter out = new OutputStreamWriter(fos);
out.write(username);
out.flush();
out.close();

Read the data                   
FileInputStream fis = openFileInput(FILENAME);
InputStreamReader in = new InputStreamReader(fis);
BufferedReader r = new BufferedReader(in);
StringBuilder total = new StringBuilder();
String line;

while ((line = r.readLine()) != null) {
    Log.i("TEMP", line);
    total.append(line);
}
Toast.makeText(MainActivity.this, total.toString(),
    Toast.LENGTH_SHORT).show();

Unless you have a reason to store the username in a file, it is better to use SharedPreferences to store information like this. SharedPreferences allows you to store settings and other general pieces of information for your app, it is automatically available to any Activity class, and handles all the reading and writing for you. The values you store in SharedPreferences are only available to the app that creates them, so it provides a layer of security too. The SharedPreferences are stored in the Android filesystem when you set the values, and they are automatically loaded whenever your app is run.

Here is an example where you can get and see the value of Username in a SharedPreferences...

// For reading preferences
SharedPreferences reader = PreferenceManager.getDefaultSharedPreferences(this.getContext());
// For writing preferences
Editor writer = reader.edit();

// Store the username in the preferences
writer.putString("Username","Fred");
writer.commit();

// Get the username from the preferences
String username = reader.getString("Username",""); // the "" at the end says to return "" if the Username isn't set yet

Pretty simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM