【问题标题】:Errors in android application with OutputStreamWriter带有 OutputStreamWriter 的 android 应用程序中的错误
【发布时间】:2017-01-07 00:53:11
【问题描述】:

我正在尝试找出我在 Android 应用程序中的一些代码存在的问题。我正在尝试从 EditText (几个框)中写一些文本。我有两个错误,并且一次又一次地查看它,但无法弄清楚我哪里出错了。我可能已经盯着它太久了。

我有错误的那一行是加粗的文本。另外,directory.close()

file = new FileOutputStream(view); OutputStreamWriter outputstreamwriter = new OutputStreamWriter(文件);

代码:

 try

            {
                File directory = new File(Environment.getExternalStorageDirectory(), "/Car/");
                directory.mkdirs();
                File file = new File(directory, "Car.txt");
                file.createNewFile();
                **file = new FileOutputStream(view);
                OutputStreamWriter outputstreamwriter = new OutputStreamWriter(file);**
                outputstreamwriter.append((new StringBuilder()).append(txtName.getText()).append("\n").append("\n").toString());
                outputstreamwriter.append((new StringBuilder()).append(txtAddress.getText()).append("\n").append("\n").toString());
                outputstreamwriter.append((new StringBuilder()).append(txtEmail.getText()).append("\n").append("\n").toString());
                outputstreamwriter.append((new StringBuilder()).append(txtPhone.getText()).append("\n").append("\n").toString());
                outputstreamwriter.append((new StringBuilder()).append(txtDesc.getText()).append("\n").append("\n").toString());
                outputstreamwriter.close();
                directory.close();
            }
            // Misplaced declaration of an exception variable
            catch (IOException ex)
            {
                Toast.makeText(getApplicationContext(), "balls", Toast.LENGTH_SHORT).show();
            }

感谢您提供的任何帮助或见解。

谢谢。

【问题讨论】:

  • 您是否检查过您是否拥有写入外部存储所需的权限?

标签: java android


【解决方案1】:
  • 关闭流,先刷新它。

我认为你应该先做outputStreamWriter.flush(),然后关闭它。 OutputStreamWriter.close()

  • 您应该提供准确的错误堆栈跟踪。

【讨论】:

  • 谢谢。我想我明白了。
猜你喜欢
  • 2015-11-27
  • 1970-01-01
  • 2018-11-06
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多