2011-10-09 24 views
0

我在使用android寫入和閱讀時遇到了一些問題。 這裏是我的代碼:如何在android中的文件中進行讀寫

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(); 
      } 
      } 
     }); 
    } 
} 

的嘗試和漁獲物中加入日食

,這裏是我的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> 

,這裏是我的清單:

<?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> 

時我運行代碼,所有的文本框出現,提交按鈕也一樣,但是當我鍵入一些時然後點擊提交按鈕,文本框變成[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:查看我的答案編輯。 – Squonk

相關問題