保存文件总是不起作用的android

时间:2012-04-03 11:23:16

标签: android file save

所以最开始是按钮的问题,只是意味着我的文件存在问题。我想将数字保存到我的文件中,还有我选择的languege。 languege工作得很完美,但当我尝试对我的数字做同样的事情时,它就出错了

所以这是我的languege文件的代码

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

这里我为按下的按钮保存了2个字母 这是再次读取它的代码,并使选择了languege的按钮

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

现在这是我保存和阅读我的号码的代码

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

这就是我想要阅读的方式

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

所以我现在并没有真正意识到它出错的地方

1 个答案:

答案 0 :(得分:0)

您想使用Context.openFileStream(Context.MODE_PRIVATE)。它没有解释为什么你必须这样做,我假设操作系统权限,但这应该适合你。