无法读取/写入文件在android

问题描述:

我试图让我的应用程序从EditText写入文本到文本文件并从它读入到TextView。它从未更新过TextView,现在应用程序崩溃。任何建议,将不胜感激!无法读取/写入文件在android

public class MainActivity extends Activity implements OnClickListener { 

String file = Environment.getExternalStorageDirectory() + "/CSCI598.txt"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button rea = (Button) findViewById(R.id.reads1); 
    rea.setOnClickListener(this); 

    Button ap = (Button) findViewById(R.id.appends1); 
    ap.setOnClickListener(this); 

} 

private void toast(String text) 
{ 
    Context context = getApplicationContext(); 
    Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG); 
    toast.show(); 
} 

@SuppressWarnings("resource") 
@Override 
public void onClick(View v) { 

    TextView tv=(TextView)findViewById(R.id.textView1); 
    EditText et=(EditText)findViewById(R.id.editText1); 

    switch(v.getId()) { 
    case R.id.reads1: 

      try 
      { 
       FileInputStream fis = new FileInputStream(file); 
       BufferedReader bfr = new BufferedReader(new InputStreamReader(fis)); 
       String in; 
       StringBuffer stringBuffer = new StringBuffer();     
       while ((in = bfr.readLine()) != null) { 
        stringBuffer.append(in + "\n"); 
       } 
       tv.setText(stringBuffer.toString()); 
       toast("File successfully loaded."); 

      } 
      catch (Exception ex) 
      { 
       toast("Error loading file: " + ex.getLocalizedMessage()); 

      } 

     break; 

    case R.id.appends1: 

     String txt=et.getText().toString(); 

     try 
      { 
      FileWriter writer = new FileWriter(file); 
      writer.write(txt); 
      writer.flush(); 
      writer.close(); 

       toast("File successfully saved."); 
      } 
      catch (Exception ex) 
      { 
       toast("Error saving file: " + ex.getLocalizedMessage()); 
      } 

     break; 

    } 

} 

} 
+1

它在哪里崩溃?您是否设法捕获导致崩溃的LogCat输出?您是否尝试在调试器上运行代码?你确定你有正确的文件/目录权限? – MarsAtomic

+0

我正在使用android模拟器运行它,所以不确定权限。 Logcat显示了大量的错误,我不知道我应该看什么..它在我点击'阅读'时崩溃 – Prasad

+0

看到我的更新在答案 – Peshal

。试着在你的代码修改这一行:

String file = "sdcard/CSCI598.txt"; 

有了:

String file = Environment.getExternalStorageDirectory() + "/CSCI598.txt"; 

此外,作为@Yahya提到请确保您在您的Android清单文件中有这样的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

如果这不起作用。尝试阅读这样的文件:

FileReader fr = new FileReader(file); 
BufferedReader inputReader = new BufferedReader(fr); 

然后使用你的while循环,因为它是现在。

写这样的文件

FileWriter out = new FileWriter(file, true); 
out.write(txt + "\n"); 
out.close(); 
+0

我刚刚做到了。它不再在'读'崩溃,但仍然不更新TextView ...我想知道如果我的追加函数有任何问题,或者它甚至被存储? – Prasad

+0

我已经将该权限附加到清单。 – Prasad

+0

尝试记录stringBuffer。toString()在textview上设置以查看字符串缓冲区是否正确填充 – Peshal

提供崩溃日志。

  1. 你给你的清单XML文件的SD卡读写权限?

<使用许可权 机器人:名字= “android.permission.WRITE_EXTERNAL_STORAGE” >< /使用许可权>

  1. 而不是使用像字符串文件的硬编码路径= “SD卡/ CSCI598.txt”;以下使用

    String file = Environment.getExternalStorageDirectory()+“/CSCI598.txt”;

  2. 当您使用模拟器有你有很多SD卡空间,同时获得使用Environment.getExternalStorageDirectory()的SD卡目录中创建AVD

+0

我已经将该权限附加到清单。 – Prasad

+0

提供你的logcat –