gpt4 book ai didi

java - Android - 将对象保存在内部存储器上

转载 作者:行者123 更新时间:2023-12-01 04:58:46 26 4
gpt4 key购买 nike

这段代码应该使用他输入的用户名和密码创建一个新用户,然后将该新对象保存到手机内存中,文件名与他的电子邮件匹配,以便在登录方法中我可以查找与电子邮件匹配的文件输入反序列化它,他的所有用户信息都会在那里...但我不断收到 FileNotFoooundException...我真的不明白...请有人帮助我! :)

代码如下:

  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;
}
}

最佳答案

根据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

请确保 String fileName = newUser.getEmail().toString(); 产生有效的文件名,我怀疑是这种情况。

关于java - Android - 将对象保存在内部存储器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13665824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com