0
나중에 실행하기 위해 사용자가 입력 한 내용을 저장하려는 프로그램이 있습니다. 현재 텍스트 파일에 저장하려고합니다. 나는 프로그램을 잘 실행할 수 있고 모든 것이 잘된 것 같지만, 결국 나는 파일과 그것의 손길이 닿지 않은 것을 체크한다. 다음은 코드입니다.Android 기록 파일에는 아무런 변화가 없습니다.
public void onClick(View v)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_tax_popup, (ViewGroup) findViewById(R.id.taxWindow), false);
String filename = "tax.txt";
boolean isEnabled = layout.findViewById(R.id.taxBox).isEnabled();
String enable = String.valueOf(isEnabled);
String taxPercent = layout.findViewById(R.id.enterTax).toString();
FileOutputStream fos;
try
{
fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(enable.getBytes());
fos.write(taxPercent.getBytes());
fos.close();
}
catch (Exception e)
{e.printStackTrace();}
pw.dismiss();
}
어떻게 파일에 쓸 수 있습니까? 또한 누구든지 실행간에 데이터를 저장하는 더 좋은 방법을 알고 있다면 제안에 개방적입니다.
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
당신이 할 수있는 (그리고 아마도 것입니다) 물론, 더 많은 작업을 수행 :
openFileOutput 및 private 모드로 만든 파일은 에뮬레이터 또는 루팅 된 장치 외부에서 확인하기가 어렵습니다. 환경 설정이나 다른 DB 대신 리터럴 파일을 사용하는 경우 초기 테스트 중에 외부 저장소 사용을 고려하십시오. –