文件I / O不工作 [英] file i/o not working

查看:193
本文介绍了文件I / O不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试Android上的一个简单的文件I / O程序,其中我在一些文本键入
EditText上,当我点击保存按钮,将其写入一个文件中的内容。当我点击Load按钮加载内容回的EditText。

I am trying to test a simple file i/o program on android where i type in some text in EditText and when i click the Save Button, it writes the content in a file. and when i click the Load button it loads the content back into the EditText.

这是我的code ---

here is my code---

public class Activity2 extends Activity {

private EditText textBox;
private static final int READ_BLOCK_SIZE = 100;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.layout2);

textBox = (EditText) findViewById(R.id.txtText1);

Button saveBtn = (Button) findViewById(R.id.btnSave);
Button loadBtn = (Button) findViewById(R.id.btnLoad);

saveBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String str = textBox.getText().toString();

try
{

PrintWriter PR=new PrintWriter(new File("text1.txt"));

PR.write(str);
PR.flush();
PR.close();

//textBox.setText("");
//---display file saved message---
Toast.makeText(getBaseContext(),
"File saved successfully!",
Toast.LENGTH_SHORT).show();
//---clears the EditText---
textBox.setText("");
}

catch (IOException ioe)
{
 ioe.printStackTrace();
}

}

});


loadBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

try
{
  File f=new File("text1.txt");

  BufferedReader BR=new BufferedReader(new FileReader(f));

  String txt="";
      String str;    

  while((str=BR.readLine())!=null)
  {
      txt+=str;

  }

//---set the EditText to the text that has been
// read---
textBox.setText(txt);

BR.close();

Toast.makeText(getBaseContext(),
"File loaded successfully!",
Toast.LENGTH_SHORT).show();


}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
}
}

`

为什么它不工作?感谢您事先的任何帮助。任何的解释将是非常美联社preciated。

why is it not working? Thanks in advance for any help. Any explanation would be highly appreciated.

推荐答案

使用新的文件(text1.txt)作为输出不会在Android上运行。当前工作directoy总是 / 这是不是写你的应用程序。使用

Using new File("text1.txt") as output does not work on Android. The current working directoy is always / which is not writable for your app. Use

File f = new File(Environment.getExternalStorageDirectory(), "text1.txt");

<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29\">getExternalStorageDirectory()是较新的手机内部存储。

getExternalStorageDirectory() is the internal storage for newer phones.

您的应用程序<一href=\"http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29\">Context有,如果你想将数据存储在您的应用程序,私有文件夹,您可以使用多个路径。从环保路径是在公共场所,你可以简单地用文件管理器读取数据

Your app Context has several paths that you can use if you want to store data in your app-private folder. Paths from Environment are in public places where you can simply read the data with a filemanager

不要忘了添加

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

如果你想写信给这些公共路径

in case you want to write to those public paths

这篇关于文件I / O不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆