简体   繁体   中英

Saving files doesn't work always android

So what started as A problem with buttons, just means there is A problem with my files. I want to save numbers to my files, and also wich languege I selected. The languege works perfect but when I try to do the same with my numbers it just go wrong

So this is the code of my languege file

french.setOnClickListener(new View.OnClickListener() {  
        public void onClick(View v) {
            try{
                buttonSound.start();
                String FILENAME = "LangFile";
                String data ="FR";
                FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                fos.write(data.getBytes());
                fos.close();
                FILENAME="TempFile";
                data="0";
                FileOutputStream fostemp = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                fostemp.write(data.getBytes());
                fostemp.close();
                startActivity(new Intent("com.technopolisapp.LOGOTECHNOPOLIS"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally{
                finish();
            }               
        }
    }); 

Here I save 2 letters for the button pressed And this is the code that reads it again and make A the buttons with the languege chosen

try{
        String FILENAME = "LangFile";
        FileInputStream fos = openFileInput(FILENAME);
        byte[] b = new byte[2];
        fos.read(b);
        fos.close();
        if(new String(b).equalsIgnoreCase("NL")){
            Tower.setText(classNamesDutch[0]);
            Puzzle.setText(classNamesDutch[1]);
            Mirror.setText(classNamesDutch[2]);
            Tangram.setText(classNamesDutch[3]);
            Frog.setText(classNamesDutch[4]);
        }
        if(new String(b).equalsIgnoreCase("GB")){
            Tower.setText(classNamesEnglish[0]);
            Puzzle.setText(classNamesEnglish[1]);
            Mirror.setText(classNamesEnglish[2]);
            Tangram.setText(classNamesEnglish[3]);
            Frog.setText(classNamesEnglish[4]);
        }
        if(new String(b).equalsIgnoreCase("FR")){
            Tower.setText(classNamesFrench[0]);
            Puzzle.setText(classNamesFrench[1]);
            Mirror.setText(classNamesFrench[2]);
            Tangram.setText(classNamesFrench[3]);
            Frog.setText(classNamesFrench[4]);
        }
    }catch (java.io.FileNotFoundException e) {
      } catch (IOException e) {
        e.printStackTrace();
    }

Now this is my code for saving and reading for my numbers

public void Savefile(){
    int level=0;
    String data;
    try{
        String FILENAME = "TowerFile";
        FileInputStream fos = openFileInput(FILENAME);
        byte[] b = new byte[1];
        fos.read(b);
        fos.close();            
        if(new String(b).equalsIgnoreCase("1")){
            level=1;
        }
        if(new String(b).equalsIgnoreCase("2")){
            level=2;
        }
        if(new String(b).equalsIgnoreCase("3")){
            level=3;
        }
        if(new String(b).equalsIgnoreCase("4")){
            level=4;
        }
        if(new String(b).equalsIgnoreCase("5")){
            level=5;
        }
        if(level!=1 || level!=2 || level!=3 || level!=4 || level!=5){
            FILENAME="TowerFile";
            data="een";
            FileOutputStream fostemp = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fostemp.write(data.getBytes());
            fostemp.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And this is the way I want to read it

try{
        String FILENAME = "TowerFile";
        FileInputStream fos = openFileInput(FILENAME);
        byte[] b = new byte[1];
        fos.read(b);
        fos.close();
        if(new String(b).equalsIgnoreCase("1")){
            button1.setVisibility(Button.GONE);
        }
        if(new String(b).equalsIgnoreCase("2")){
            level=2;
        }
        if(new String(b).equalsIgnoreCase("3")){
            level=3;
        }
        if(new String(b).equalsIgnoreCase("4")){
            level=4;
        }
        if(new String(b).equalsIgnoreCase("5")){
            level=5;
        }
    }catch (java.io.FileNotFoundException e) {
    } catch (IOException e) {
            e.printStackTrace();
    }

So I don't really get it where it goes wrong at the moment

You want to use Context.openFileStream(Context.MODE_PRIVATE) . It doesn't explain why you must do it this way, I am assuming OS permissions, but this should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM