如何在Android中从文件中读取/写入字符串我希望通过获取从EditText输入的文本将文件保存到内部存储。然后,我希望同一个文件返回字符串形式的输入文本,并将其保存到另一个字符串中,该字符串将在以后使用。下面是代码:package com.omm.easybalancerecharge;import android.app.Activity;import android.content.Context;import android.content.Intent;
import android.net.Uri;import android.os.Bundle;import android.telephony.TelephonyManager;import android.view.Menu;import android.view.View;
import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText num = (EditText) findViewById(R.id.sNum);
Button ch = (Button) findViewById(R.id.rButton);
TelephonyManager operator = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String opname = operator.getNetworkOperatorName();
TextView status = (TextView) findViewById(R.id.setStatus);
final EditText ID = (EditText) findViewById(R.id.IQID);
Button save = (Button) findViewById(R.id.sButton);
final String myID = ""; //When Reading The File Back, I Need To Store It In This String For Later Use
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Get Text From EditText "ID" And Save It To Internal Memory
}
});
if (opname.contentEquals("zain SA")) {
status.setText("Your Network Is: " + opname);
} else {
status.setText("No Network");
}我包含了一些注释,以帮助您进一步分析我希望操作的位置/使用变量的位置。
3 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
private void writeToFile(String data,Context context) { try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE)); outputStreamWriter.write(data); outputStreamWriter.close(); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } }
private String readFromFile(Context context) { String ret = ""; try { InputStream inputStream = context.openFileInput("config.txt"); if ( inputStream != null ) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String receiveString = ""; StringBuilder stringBuilder = new StringBuilder(); while ( (receiveString = bufferedReader.readLine()) != null ) { stringBuilder.append(receiveString); } inputStream.close(); ret = stringBuilder.toString(); } } catch (FileNotFoundException e) { Log.e("login activity", "File not found: " + e.toString()); } catch (IOException e) { Log.e("login activity", "Can not read file: " + e.toString()); } return ret;}
波斯汪
TA贡献1811条经验 获得超4个赞
public static void writeStringAsFile(final String fileContents, String fileName) { Context context = App.instance.getApplicationContext(); try { FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName)); out.write(fileContents); out.close(); } catch (IOException e) { Logger.logError(TAG, e); }}public static String readFileAsString(String fileName) { Context context = App.instance.getApplicationContext(); StringBuilder stringBuilder = new StringBuilder(); String line; BufferedReader in = null; try { in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName))); while ((line = in.readLine()) != null) stringBuilder.append(line); } catch (FileNotFoundException e) { Logger.logError(TAG, e); } catch (IOException e) { Logger.logError(TAG, e); } return stringBuilder.toString();}
添加回答
举报
0/150
提交
取消