2013-03-27 3 views
0

edittext를 사용하여 my file.txt의 변경 사항을 저장하는 방법에 어려움을 겪고 있습니다. 지금까지는 내 코드를 사용하여 텍스트 파일을 열 수 있었지만 닫을 때 변경된 내용은 저장되지 않습니다. 텍스트 파일은 다른 활동으로 열리고 방향이 변경되거나 최소화 된 경우 변경 사항을 저장합니다. 나는 여러 가지 다른 솔루션을 시도했지만 변경된 사항을 저장하는 방법에 대한 내 머리를 맞이할 수 없습니다.Edittext의 정보를 android의 원본 파일에 저장하는 방법

public class Editor extends Activity { 
private String Text; 
private String Folder; 
private String toast; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_editor); 
    Bundle extras = getIntent().getExtras(); 
    this.Text = extras.getString ("txt"); 
    this.Folder = extras.getString("s"); 
    this.toast = (Folder + "/" + Text); 
    Toast.makeText(this, toast, Toast.LENGTH_SHORT).show(); 
    File sdcard = Environment.getExternalStorageDirectory(); 
    File file = new File(sdcard + "/NoteTaker/" + Folder + "/" + Text); 
    StringBuilder text = new StringBuilder(); 
    try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 
    while ((line = br.readLine()) != null) { 
    text.append(line); 
    text.append('\n'); 
    } 
    } 
    catch (IOException e) { 
    } 
    TextView tv = (TextView)findViewById(R.id.editText1); 
    tv.setText(text); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_editor, menu); 
    return true; 
} 

답변

1

당신이 perfoming 경우 쓰기 작업은 여기 OuptputStream

내가 편집기 변경에 대한 오류를 받고 있어요 one of your solution

+0

OMG. 나는 그것을 밖으로 분류하는 것을 시도하는 시간 동안 이것을보고있다. 그리고 그것은 마침내 작용한다 –

+0

그 나의 즐거움. :) – QuokMoon

0

이 당신을 위해 무엇을 찾고있는 수 있습니다 :

public void onPause(){ 
    super.onPause(); 

    try{ 
     OutputStreamWriter out=new OutputStreamWriter(openFileOutput(NOTES, 0)); 

     out.write(editor.getText().toString()); 
     out.close(); 
    } 
    catch (Throwable t) { 
     Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show(); 
    } 
} 

당신은 당신의 file_name.txt에 참고 변수와 글고 이름이 무엇이든에 편집기를 변경해야합니다. 당신이 읽기 작업은 다음 InputStream

또는

를 사용 perfoming 경우

+0

이다 사용 EditText에. 오류는 비 정적 스레드에 대한 정적 참조를 만들 수 없습니다. 나는 창조를 시도했다 : EditText = 편집; 상단에있는 을 사용하고 있지만 여전히 오류가 있습니다. –

+0

EditText는 사용자가 자신의 데이터를 입력하도록하지 않는 한 파일에 저장하려는 정보가있는 레이아웃의 텍스트 상자입니다.이 경우 EditText 텍스트 상자가 없습니다. EditText를 다음과 같이 정의합니다 :'EditText myText = (EditText) findViewById (R.id.name_of_edittext_from_layout_xml);' – TronicZomB

관련 문제