2014-10-23 2 views
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(); 
} 
} 

당신이 할 수있는 (그리고 아마도 것입니다) 물론, 더 많은 작업을 수행 :

+0

openFileOutput 및 private 모드로 만든 파일은 에뮬레이터 또는 루팅 된 장치 외부에서 확인하기가 어렵습니다. 환경 설정이나 다른 DB 대신 리터럴 파일을 사용하는 경우 초기 테스트 중에 외부 저장소 사용을 고려하십시오. –

답변

1

이 정확한 것 : 여기

에 대한 공유 설정이라는 아름다운 작은 일이있다는 Android Developer Guide에서 예입니다 putBoolean보다 - putString ("HEY THERE"); 훨씬 더 자주.

이 정보가 도움이되기를 바랍니다. 행운을 빌어 요 :)

관련 문제