How to load FileInputStream in a septate class

笑着哭i 提交于 2019-12-25 06:48:08

问题


I've saved two strings to a file and want to display those strings in another class, I tried the usually FileInputStream, but it wasn't having any of it, what other way could I solve this, I want to stick to files and not sharedprefernces.

Is there a way to solve this issue with using Inputstreams in another class?

Thanks My Code:

EditText eTuser;
EditText eTpassword;
CheckBox StaySignedIn;
Button bSubmit;
String user;
String pass;
FileOutputStream fos;
FileInputStream fis = null;
String FILENAME = "userandpass";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    eTuser = (EditText) findViewById(R.id.eTuser);
    eTpassword = (EditText) findViewById(R.id.eTpassword);
    StaySignedIn = (CheckBox) findViewById(R.id.Cbstay);
    bSubmit = (Button) findViewById(R.id.bLogIn);
    bSubmit.setOnClickListener(this);
    File file = getBaseContext().getFileStreamPath(FILENAME);
    if (file.exists()) {
    Intent i = new Intent(LogIn.this, ChatService.class);
    startActivity(i);     
    }
    //if if file exist close bracket
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // end of catch bracket
catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // end of catch

} // create ends here

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    case R.id.bLogIn:
        String user = eTuser.getText().toString();
        String pass = eTpassword.getText().toString();
        Bundle userandpass = new Bundle();
        userandpass.putString("user", user);
        userandpass.putString("pass", pass);
        Intent login = new Intent(LogIn.this, logincheck.class);
        login.putExtra("pass", user);
        login.putExtra("user", pass);
        startActivity(login);

        if(StaySignedIn.isChecked());
        String userstaysignedin = eTuser.getText().toString();
        String passstaysignedin = eTpassword.getText().toString();
        try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(userstaysignedin.getBytes());
        fos.write(passstaysignedin.getBytes());
        fos.close();
        } catch(IOException e) {
            // end of try bracket, before the Catch IOExceptions e.
            e.printStackTrace();




        } // end of catch bracket


    } // switch and case ends here
}// Click ends here

 }// main class ends here

FileInPutStreamCode:

fis = openFileInput(FILENAME);  
            byte[] dataArray = new byte[fis.available()];
            while (fis.read(dataArray) != -1); { 
                //while statement }

来源:https://stackoverflow.com/questions/9548318/how-to-load-fileinputstream-in-a-septate-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!