为什么我找不到我在Android手机上创建的文本文件?

时间:2015-11-14 12:39:14

标签: java android android-studio text-files

我创建了一个应用程序,用户输入将其保存到文本文件,然后在用户点击提交按钮后将其从文本文件显示到屏幕上。该应用程序运作完美,但当我进入手机时,我似乎无法找到文本文件。有人知道为什么会这样吗?

我的java代码是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myFile = new FileUtility();
    myFile.createFile(getApplicationContext(), "tester");
    Button b = (Button)findViewById(R.id.button); //button

    final  TextView out = (TextView) findViewById(R.id.output);



    //button listener, when button clicked, produce output on textfield "Score"
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final  TextView line1 = (TextView) findViewById(R.id.line1);
            final  TextView line2 = (TextView) findViewById(R.id.line2);
            final  TextView t = (TextView) findViewById(R.id.title);
            Button b = (Button)findViewById(R.id.button);

            line1.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
            b.setVisibility(View.INVISIBLE);

            String first = line1.getText().toString();
            String second = line2.getText().toString();
            myFile.writeLine(first);
            myFile.writeLine(second);

            String output = myFile.readAll();

            out.setText(output);
            t.setText("File Output");

        }

    });

这里参考的是我制作的FileUtility类:

public class FileUtility {

private File root;
private File file;

public FileUtility() {
    root = Environment.getExternalStorageDirectory();
}

public void createFile(Context context, String fileName) {
    try {
        if (root.canWrite()) {
            file = new File(root, "//" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
        else
        {
            file = new File(context.getFilesDir(), "//" + fileName); // File(root, "//" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
    } catch (IOException e) {
        Log.e("Error", "fail to create a new file");
    }

}

public String readAll() {
    StringBuilder returnString = new StringBuilder();
    try {
        BufferedReader in;
        FileReader datawriter = new FileReader(file);
        in = new BufferedReader(datawriter);
        if (file.exists()) {
            String str = null;
            while((str=in.readLine())!=null)
            {
                returnString.append(str + "\n");
            }
        }
        in.close();
    } catch (IOException e) {
        Log.e("Error", "fail to write file");
    }
    return returnString.toString();
}

public void writeLine(String message) {
    try {
        BufferedWriter out;
        FileWriter datawriter = new FileWriter(file,true);
        out = new BufferedWriter(datawriter);
        if (file.exists()) {
            out.write(message + "\n");
            out.flush();
        }
        out.close();
    } catch (IOException e) {
        Log.e("Error", "fail to write file");
    }
}

}

2 个答案:

答案 0 :(得分:1)

可能还没有创建该文件。您是否已获得在Android清单中写入外部存储空间的权限?

http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE

答案 1 :(得分:1)

我测试了你的代码工作,我用

    myFile.createFile(this, "tester.txt");

也许您的Filemanager没有显示该文件,您使用的是哪个文件管理器?我使用ES explorer

查看了它