[英]Android - Saving object on internal memory
this code is supposed to create a new user with the username and password he entered and then save that new object to phone memory with the file name matching his email so that in the login method I can look for the file matching the email entered deserialize it, and all his user info would be there... But I keep getting a FileNotFooundException... I really don't understand... please someone help me! 该代码应使用他输入的用户名和密码创建一个新用户,然后将该新对象保存到手机存储器中,文件名与他的电子邮件匹配,以便在登录方法中我可以查找与输入的电子邮件匹配的文件反序列化,他的所有用户信息都在那里...但是我一直收到FileNotFooundException ...我真的不明白...请有人帮我! :)
:)
Here's the code: 这是代码:
package com.example.eventmanager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class CreateAccount extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
}
public void createUserAccount(View v) {
EditText username = (EditText) findViewById(R.id.editText1);
EditText password = (EditText) findViewById(R.id.editText2);
EditText secondPassword = (EditText) findViewById(R.id.editText3);
if (!(password.getText().toString().equals((secondPassword.getText()
.toString())))) {
Toast.makeText(this, "Passwords Don't Match", Toast.LENGTH_LONG).show();
} else {
User newUser = new User(username.getText().toString(), password.getText().toString());
String fileName = newUser.getEmail();
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
os.writeObject(newUser);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG)
.show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "IOException", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Intent intent = new Intent(this, LoginScreen.class);
startActivity(intent);
Toast.makeText(this, "Account Created Successfully",
Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_create_account, menu);
return true;
}
}
Per FileOutputStream
documentation: it throws FileNotFoundException
in below scenario: 每个
FileOutputStream
文档:在以下情况下,它将引发FileNotFoundException
:
FileNotFoundException - if the file exists but is a directory rather than a regular file OR does not exist but cannot be created, or cannot be opened for any other reason
FileNotFoundException-如果文件存在但是目录而不是常规文件, 或者不存在但无法创建, 或者由于任何其他原因而无法打开
Please make sure, String fileName = newUser.getEmail().toString();
请确保,
String fileName = newUser.getEmail().toString();
results in valid file name, which I suspect is the case. 导致有效的文件名,我怀疑是这种情况。
FileOutputStream
uses an absolute path which (I think) will default to the root of the internal storage if you only provide a filename - on a normal device, the root of the internal storage will not be accessible. FileOutputStream
使用一个绝对路径(我认为),如果您仅提供文件名,则它将默认为内部存储的根目录-在普通设备上,内部存储的根目录将不可访问。
You should use openFileOutput(String name, int mode)
instead. 您应该改用
openFileOutput(String name, int mode)
。 This guarantees creating a file in the internal storage in the area allocated to your own app. 这样可以确保在内部存储中分配给您自己的应用的区域中创建文件。 To read the file back, use the corresponding
openFileInput(String name)
method. 要读回文件,请使用相应的
openFileInput(String name)
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.