Android Java读取和保存数据不起作用

Android Java读取和保存数据不起作用,java,android,eclipse,load,save,Java,Android,Eclipse,Load,Save,嘿,我想保存数据,然后我想阅读它们并将它们放在编辑文本中 speichern.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { save(); tv.setText("gespeichert"); } }); private void save() { try {

嘿,我想保存数据,然后我想阅读它们并将它们放在编辑文本中

    speichern.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
        save();
        tv.setText("gespeichert");

        }

    });

private  void save() {
    try {
        File myFile = new File("bla.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new    OutputStreamWriter(fOut);
        myOutWriter.append(string.toString() + "\n");
        myOutWriter.append(string2.toString());

        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Gespeichert",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
}
我能换什么` 请帮帮我 我正在使用eclipse
并且我希望将.txt文件保护为非外部文件。

我认为您创建的文件不正确:

File file = new File("test.txt");
file.createNewFile();

if(file.exists())
{
     OutputStream fo = new FileOutputStream(file);              
     fo.write("Hello World");
     fo.close();
     System.out.println("file created: "+file);
     url = upload.upload(file);
}
要读取此文件,请执行以下操作:

 try {
    // open the file for reading
    InputStream instream = openFileInput("test.txt");


// if file the available for reading
if (instream) {
  // prepare the file for reading
  InputStreamReader inputreader = new InputStreamReader(instream);
  BufferedReader buffreader = new BufferedReader(inputreader);

  String line;

  // read every line of the file into the line-variable, on line at the time
  while (( line = buffreader.readLine())) {
    // do something with the settings from the file
  }

}

// close the file again      
instream.close();
  } catch (java.io.FileNotFoundException e) {
    // do something if the myfilename.txt does not exits
  }
不要忘记用
try catch
块封装代码,以捕获从这些对象抛出的
IOException

编辑

将此添加到清单文件:

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

希望有帮助!:)

你有什么错误吗?如果是,您能提供stacktrace吗?:)当我按下load时,我得到了android.content.res.Resources$NotFoundException:String resource ID#0x0
private void read(){try{File myFile=new File(“bla.txt”);FileInputStream fIn=new FileInputStream(myFile);BufferedReader myReader=new BufferedReader(new InputStreamReader(fIn));for(int i=0;i@user3722368在阅读之前,您正在创建一个新文件。我可以将我的完整项目发送给您并修复它吗?@user3722368我将无法为您完成此操作:(抱歉…但是此链接可能可以帮助您哦,我重新启动模拟器,现在我可以保护数据了:)谢谢:)
 try {
    // open the file for reading
    InputStream instream = openFileInput("test.txt");


// if file the available for reading
if (instream) {
  // prepare the file for reading
  InputStreamReader inputreader = new InputStreamReader(instream);
  BufferedReader buffreader = new BufferedReader(inputreader);

  String line;

  // read every line of the file into the line-variable, on line at the time
  while (( line = buffreader.readLine())) {
    // do something with the settings from the file
  }

}

// close the file again      
instream.close();
  } catch (java.io.FileNotFoundException e) {
    // do something if the myfilename.txt does not exits
  }
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
File filesDir = getFilesDir();
Scanner input = new Scanner(new File(filesDir, filename));