【问题标题】:FileOutputStream not working in Android [closed]FileOutputStream 在 Android 中不起作用 [关闭]
【发布时间】:2014-02-26 06:12:54
【问题描述】:

我第一次使用内部存储,我的 openFileOutput 给了我一个错误。我不知道如何使这项工作,但如果 FileOutputStream 被删除,下面的方法工作得很好。谢谢!

public void Addition(View view) {

    int num1 = numberone.getText().toString().trim().length();
    int num2 = numbertwo.getText().toString().trim().length();
    String numone = numberone.getText().toString();
    String numtwo = numbertwo.getText().toString();

    if(num1 == 0 || num2 == 0){
    TextView value = (TextView)findViewById(R.id.answer);
    value.setText("Numbers not entered");
    }
    else if(numone.equals("+") || numtwo.equals("+") || numtwo.equals("-") || numone.equals("-")){
        TextView value = (TextView)findViewById(R.id.answer);
        value.setText("Please enter numbers");
    }
    else{

answer = Float.parseFloat(numberone.getText().toString()) + Float.parseFloat(numbertwo.getText().toString());;

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your sum is " + answer);
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

    }

}

【问题讨论】:

  • 它无法识别 openFileOutput(FILENAME, Context.MODE_PRIVATE);它建议我添加一个 throw 声明或用 try/catch 包围。谢谢!
  • openFileOutputContextWrapper 的函数。这个Addition 函数声明在哪里?
  • 如何正确使用它来存储浮动“答案”?
  • 如果它建议你用 try/catch 包围代码,那么就这样做。您还面临其他问题吗?
  • 定义“不识别”。到目前为止提供的信息不足。

标签: java android storage internal internal-storage


【解决方案1】:

如果您的类没有扩展 Context 子类,则需要传递一个 Context 实例(一个 Activity、一个服务、etc)来调用 openFileInput。 您还需要捕获 java.io.FileNotFoundException 或在您的方法中添加 throws 声明。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 2015-07-17
    • 1970-01-01
    • 2015-01-02
    • 2018-12-27
    • 2013-06-26
    • 2016-10-28
    • 2013-05-13
    • 2012-06-19
    相关资源
    最近更新 更多