我构建了一个示例应用程序,该应用程序从用户那里获取输入并将其保存在我的应用程序资产文件夹中的文本文件“ sample”中。

这是我的MainActivity.java,

 package com.example.sampledatabaseapp;

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.AssetManager;
 import android.text.Editable;
 import android.view.Menu;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;

  public class MainActivity extends Activity {

protected static final android.content.Context Context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText txtname=(EditText)findViewById(R.id.editText1);
            String name=txtname.getText().toString();

             AssetManager am=Context.getAssets();
            try {

                InputStream is=am.open("sample");
                OutputStreamWriter out=new OutputStreamWriter(openFileOutput("sample",MODE_APPEND));
                out.write(name);

                out.close();
                Toast.makeText(Context,"Text Saved !",Toast.LENGTH_LONG).show();

            } catch (IOException e) {

                e.printStackTrace();
                Toast.makeText(Context,"Sorry Text could't be added",Toast.LENGTH_LONG).show();
            }


        }
    });
}

@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_main, menu);
    return true;
}

 }


我的activity_main.xml,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:ems="10"
    android:hint="@string/Enter_Name">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignRight="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="82dp"
    android:text="@string/Save" />

</RelativeLayout>


我开始逐行调试,当调试器调试行“ AssetManager am = Context.getAssets();”时,在我的MainActivity.java文件中,它显示“找不到ZygoteInit $ MethodAndArgsCaller.run()的源”。我下载了源代码并附加了。

但是,我仍然不知道哪里出了问题。
   这是我的堆栈跟踪,

  12-27 13:32:16.467: D/InputEventConsistencyVerifier(1803): KeyEvent: ACTION_UP but key was not down.
  12-27 13:32:16.467: D/InputEventConsistencyVerifier(1803):   in  android.widget.Button{40d02568 VFED..C. .F....I. 135,168-345,216 #7f070001 app:id/button1}
  12-27 13:32:16.467: D/InputEventConsistencyVerifier(1803):   0: sent at  5236119000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=5236119, downTime=5235983, deviceId=0, source=0x101 }

  12-27 13:32:16.908: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:16.938: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:16.969: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:16.969: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:17.008: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:17.038: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:17.118: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
  12-27 13:32:17.148: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0


我不知道我是否使用正确的方法将文本写入android中的文本文件。如果有误,请提出建议。

请任何人帮忙!!

谢谢。

最佳答案

MODE_APPEND当然是将数据附加到现有文件中。
如果您想创建一个新的,应该使用MODE_PRIVATE

OutputStreamWriter out=new OutputStreamWriter(openFileOutput("sample",MODE_PRIVATE));


当您读取文件时,

        try {
            FileInputStream fileInputStream;
            fileInputStream = openFileInput("sample");
            byte[] readBytes = new byte[fileInputStream.available()];
            fileInputStream.read(readBytes);
            String readString = new String(readBytes);
            Log.v("readString", readString);
            fileInputStream.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }


好,删除这行怎么样?

AssetManager am=Context.getAssets();


您不必使用AssetManager。

然后用这个写。

try {
            FileOutputStream fileOutputStream = openFileOutput("myfile.txt", MODE_PRIVATE);
            String writeString = "test";
            fileOutputStream.write(writeString.getBytes());
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

10-07 23:34