2011-10-09 3 views
0

android에서 글쓰기 및 읽기에 몇 가지 문제가 있습니다. 여기 내 코드입니다 :android에서 파일을 읽고 쓰는 법

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="filereader.testing.com" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="10" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name" android:permission="android.permission.WRITE_EXTERNAL_STORAGE"> 
    <activity android:name=".FileReaderTestingActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
</manifest> 

:

package filereader.testing.com; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class FileReaderTestingActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button submit_btn = (Button) findViewById (R.id.submit_btn); 
     final EditText textbox = (EditText) findViewById (R.id.first_text); 
     final TextView newtext = (TextView) findViewById (R.id.read_text); 
     submit_btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
        byte[] readinfo = new byte[3]; 
        String FILENAME = "first_file"; 
       FileOutputStream mystream; 
      try { 
       mystream = openFileOutput (FILENAME, Context.MODE_PRIVATE); 
       String variabletowrite = textbox.toString(); 
       mystream.write(variabletowrite.getBytes()); 
       mystream.close(); 
       FileInputStream readstream = openFileInput (FILENAME); 
       readstream.read(readinfo); 
       newtext.setText(readinfo.toString()); 
       readstream.close(); 
      } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
      } 
     }); 
    } 
} 

은 try와 캐치에서 추가 된 일식 여기

여기 내 XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="1"> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
/> 
<EditText android:id="@+id/first_text" android:layout_width="match_parent"  
android:layout_height="wrap_content"> 
<requestFocus></requestFocus> 
</EditText> 
<Button android:layout_height="wrap_content" android:text="Submit"  
android:id="@+id/submit_btn" android:layout_width="match_parent"></Button> 
<TextView android:textAppearance="?android:attr/textAppearanceLarge"  android:layout_height="wrap_content" android:text="TextView" android:id="@+id/read_text" android:layout_weight="0.12" android:layout_width="280dp"></TextView> 
</LinearLayout> 

이고 나의 매니페스트입니다 모든 텍스트 상자가 나타나고 제출 버튼을 실행하는 코드를 실행합니다. 그러나 입력 할 때 그런 다음 제출 버튼을 누르면 텍스트 상자가 [B @ 40532d08]과 같이 바뀌고 그 의미가 전혀 없습니다. 그 숫자는 제출 버튼을 클릭 할 때마다 카운트 업되는 것처럼 보입니다. 그리고 [B @ 파트는 절대로 바뀌지 않으며 그 뒤에 나오는 숫자와 문자 만 변경됩니다.

도움을 주시면 대단히 감사하겠습니다. 감사합니다.

답변

0

변경이 줄을 ...

String variabletowrite = textbox.toString(); 

에 ...

String variabletowrite = textbox.getText().toString(); 

첫 번째 줄 (그건 당신이 그것을하고있다) 즉합니다 (EditText 개체 자체에 toString()를 호출, 그것의 참조) 및 텍스트 내용이 아닙니다.

편집 TextView이 줄의 텍스트를 설정 ...

newtext.setText(readinfo.toString()); 

는 단순히 문자열에 byte[] 참조 변환된다. 바이트 배열에서 문자열을 만들려면 다음과 같이해야합니다.

newtext.setText(new String(readinfo)); 
+0

여전히 같은 오류가 발생합니다. – Phoenix

+0

@Phoenix : 내 대답은 EDIT를 참조하십시오. – Squonk

관련 문제