无法读取/写入android中的文件

时间:2014-02-27 04:02:10

标签: java android file file-io

我正在尝试让我的应用程序将文本从EditText写入文本文件并从中读取到TextView中。它从未更新过TextView,现在应用程序崩溃了。任何建议都将非常感谢!

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;

  }

}

}

2 个答案:

答案 0 :(得分:2)

使用Environment.getExternalStorageDirectory()获取sdcard目录。尝试在代码中更改此行:

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

答案 1 :(得分:0)

提供崩溃日志。

  1. 您是否在清单xml文件中获得了sdcard读写权限。
  2.   

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

    1. 而不是像 String file =“sdcard / CSCI598.txt”; 使用硬编码路径

        

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

    2. 当您使用模拟器时,您在创建AVD时有很多SD卡空间