使用OutputStreamWriter的Android应用程序中的错误

时间:2017-01-06 16:16:57

标签: java android

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

我遇到错误的行特别是粗体文字。此外, directory.close()

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

代码:

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

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

感谢。

1 个答案:

答案 0 :(得分:0)

  •   

    关闭流,先冲洗它。

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

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