在用户登录时的活动中 我检查具有用户列表的文件是否存在,如果方法返回false,我创建一个新文件保存此列表。
这是方法:
public boolean isLoginFilePresent() {
file = new File(loginData.getPath());//file name /sdcard/Hotel_Manager/HotelsMangerUser.dat;
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
present = file.exists();
return present;
}
但是我收到以下错误(这是日志):
java.io.FileNotFoundException: /sdcard/Hotel_Manager/HotelsMangerUser.dat (No such file or directory)
08-13 11:15:36.174 12174-12174/app.com.hotelsmanager W/System.err: at java.io.FileOutputStream.open(Native Method)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at app.com.hotelsmanager.Controller.LoginDataSaver.saveLoginData(LoginDataSaver.java:108)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at app.com.hotelsmanager.Controller.LoginController.addDemoLoginFile(LoginController.java:130)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at app.com.hotelsmanager.View.Activities.LoginViewActivity.loginMe(LoginViewActivity.java:101)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at app.com.hotelsmanager.View.Activities.LoginViewActivity.-wrap0(LoginViewActivity.java)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at app.com.hotelsmanager.View.Activities.LoginViewActivity$1.onClick(LoginViewActivity.java:71)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at android.view.View.performClick(View.java:5610)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at android.view.View$PerformClick.run(View.java:22260)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at android.os.Looper.loop(Looper.java:154)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
08-13 11:15:36.175 12174-12174/app.com.hotelsmanager W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我错了什么?
感谢您的帮助!!!!
答案 0 :(得分:1)
这是将用户保存到文件
的方法public void saveLoginData(Set<User> accountSet, Context context) {
final List<String> loginFile = new ArrayList<>();
for (final User u : accountSet) {
loginFile.add(u.getName() + Constants.USERDATA_SEPARATOR + u.getPassword() +
Constants.USERDATA_SEPARATOR + u.getAccessLevel().toString());
}
try {
sdDir = new File(Constants.USER_DIR); //USER_DIR = "/sdcard/Hotel_Manger"
sdDir.mkdirs();
userOutput = new File(sdDir,Constants.USER_PATH);//USER_PATH = "HotelManagerUser.dat"
fos = new FileOutputStream(userOutput); //<------this return FileNotFoundException e
out = new ObjectOutputStream(fos);
out.writeObject(loginFile);
out.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}