【问题标题】:writing and saving a file in android application在android应用程序中写入和保存文件
【发布时间】:2017-02-15 14:39:05
【问题描述】:

我试图在按下按钮Save_btn 时写入文件,但是,当我运行应用程序时,它运行平稳,没有错误,但找不到文件。

我正在尝试写入设备的内部存储。正在写入的文本位于 edittext 字段中。我希望将 EditText 中的此文本写入文件

我在下面包含了我正在使用的代码;

Save_btn = (Button) findViewById(R.id.g);
    Save_btn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View views) {
                                        TextView CodeView = (TextView) findViewById(R.id.Code_Viewer);
                                        CodeView.setText(CodeView.getText());
                                        try {
                                            String etName = CodeView.getText().toString();
                                            if (!etName.trim().equals("")) {
                                                File file = new File("/Documents/test.txt");

                                                //if file doesnt exists, then create it
                                                if (!file.exists()) {
                                                    file.createNewFile();
                                                }


                                                FileWriter fileWritter = new FileWriter(file.getName(), true);
                                                BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
                                                bufferWritter.write(etName);
                                                bufferWritter.close();
                                            }
                                        } catch (IOException e) {

                                            e.printStackTrace();
                                        }
                                    }
                                }
    );

任何关于如何正确写入文件的建议将不胜感激。

提前致谢。

【问题讨论】:

  • 首先,您要将文件保存在哪里?你想达到什么目的? developer.android.com/guide/topics/data/data-storage.html
  • 我正在尝试写入设备的内部存储。正在写入的文本位于 edittext 字段中。我希望将 EditText 中的此文本写入文件
  • 没有。 File file = new File("/Documents/test.txt");。那是一条不可能的路。将Toast() 放入显示e.getMessage() 的catch 块中。
  • if (!file.exists()) { file.createNewFile(); }。删除帽子代码。没用。

标签: android file filewriter


【解决方案1】:

将此代码替换为您的代码:

    Save_btn = (Button) findViewById(R.id.g);
    Save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View views) {
                TextView CodeView = (TextView) findViewById(R.id.Code_Viewer);
                CodeView.setText(CodeView.getText());
                String etName = CodeView.getText().toString();

                File dir = new File(getFilesDir(), "yourfolder");
                if(!dir.exists())
                {
                    dir.mkdirs();
                }
                String textFileName="textFile.txt";
                File file = new File(dir.getPath(), textFileName );
                if(file.exists())
                {
                    file.delete();
                }
                try {
                    FileWriter fw = new FileWriter(file);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(etName);
                    bw.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }
    );

您应该注意以下几点:

  • 此代码将在 /data/data/[your app package name]/files/[your folder]/[your textFileName] 上创建文件
  • 如果文件名相同,它总是会删除您的文件,因此您应该为每个文件获取唯一的名称。(您可以在文件名中包含日期和时间)

【讨论】:

  • 感谢建议,还是看不到应用的目录
  • 你用的是实体手机还是模拟器?
  • 实体手机华为P9
  • 我有它不存在包名不是目录
  • 把这个** Log.i("FilePath" , file.getPath()); ** 之后 ** 文件 file = new File(dir.getPath(), textFileName ); ** 并使用 info filter 检查您的 logcat,它应该会显示文件的保存位置...
【解决方案2】:

我不确定这是否可行,因为您指定的文件路径是 a) 绝对路径和 b) 指向您可能没有写入权限的目录。

File file = new File("/Documents/test.txt");

这引用了文件系统根目录中的 Documents 文件夹,而不是您的应用程序文件。

如果您想将其保存在本地以供自己的应用程序使用,可以尝试Context#getFilesDir,例如

File file = new File(context.getFilesDirectory(), "yourfile.txt");

如果您想将其保存在其他应用程序可以使用的地方,您可能需要更复杂的方法,例如FileProvider

【讨论】:

  • 尝试此操作时出现错误“无法解析符号上下文”?
  • 在没有上下文的情况下进行。它是隐含的。
  • 您的活动是一个上下文(您的应用程序也是),您的片段有一个方法 getContext()。如果在活动中发生这种情况,请在单击处理程序之前尝试final Context context = this;。或者使用 greenapps 方法。
【解决方案3】:

你应该使用

File file = new File(context.getFilesDir(), "test.txt");

而不是

File file = new File("/Documents/test.txt");

保存到您的内部存储。您的其余代码可以保持不变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    相关资源
    最近更新 更多