简体   繁体   English

如何在android内部存储和提取?

[英]How to store and pull internally on android?

I am having trouble trying to store a username string internally on an android devices. 我在尝试在Android设备上内部存储用户名字符串时遇到麻烦。 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. 我在创建bold_username.txt文件时出错。 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来存储这样的信息。 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. SharedPreferences允许您存储应用程序的设置和其他一般信息,它可以自动供任何Activity类使用,并为您处理所有读写操作。 The values you store in SharedPreferences are only available to the app that creates them, so it provides a layer of security too. 您存储在SharedPreferences中的值仅适用于创建它们的应用程序,因此它也提供了一层安全性。 The SharedPreferences are stored in the Android filesystem when you set the values, and they are automatically loaded whenever your app is run. 设置值时,SharedPreferences存储在Android文件系统中,并且在您运行应用程序时会自动加载它们。

Here is an example where you can get and see the value of Username in a SharedPreferences... 这是一个示例,您可以在其中获取并查看SharedPreferences中的Username值。

// 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. 很简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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