简体   繁体   English

Android文件编写

[英]Android file writing

Having a problem writing out to a file, this code is taken directly from the android developer page and then tweaked a bit by me. 写入文件时遇到问题,此代码直接从android开发人员页面获取,然后由我进行了一些调整。 Is there something i am missing? 有什么我想念的吗? Quite new to Android development so sorry if it's something blatantly obvious. 对Android开发来说是相当新的东西,所以很抱歉,如果这很明显。

     send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FileOutputStream outputStream;
            String data = "hello";
            File fileDir = new File("data.txt");
            if (!fileDir.exists())
                fileDir.mkdirs();

            try {
                outputStream = openFileOutput("data.txt",Context.MODE_PRIVATE);
                outputStream.write(data.getBytes());
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    });

Basically, your problem is that you are trying to do it twice, once in a way that won't work, and once in a way that will, but hides the result. 基本上,您的问题是您尝试执行两次,一次尝试无效,而一次尝试隐藏结果。

        File fileDir = new File("data.txt");
        if (!fileDir.exists())
            fileDir.mkdirs();

This would create a Java File object connected to a hypothetical file called "data.txt" located in the current working directory, which for an Android app is the root directory of the device - a place you most definitely are not allowed to write to. 这将创建一个Java File对象,该对象连接到位于当前工作目录中的假定文件“ data.txt”,对于Android应用程序,该文件是设备的根目录-绝对不允许您写入该目录。 However, this may not obviously cause any errors, as the root directory exists so mkdirs() will do nothing, and you only create a File object, you don't actually try to create a file on "disk". 但是,这显然不会引起任何错误,因为根目录存在,所以mkdirs()不会执行任何操作,并且您只创建File对象,实际上并没有尝试在“ disk”上创建文件。 Effectively this code does nothing for you - get rid of it. 实际上,此代码对您没有任何帮助-摆脱它。

Next you try something basically workable: 接下来,您尝试一些基本可行的方法:

        try {
            outputStream = openFileOutput("data.txt",Context.MODE_PRIVATE);
            outputStream.write(data.getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

openFileOutput() is a method of a Context (Activity or Service) which creates an output stream to write to an actual file located in the private internal storage area of your app. openFileOutput()是上下文(活动或服务)的一种方法,该方法创建输出流以写入位于应用程序专用内部存储区中的实际文件。 This is all fine and good, and normally a good choice for storing typical data. 一切都很好,并且通常是存储典型数据的不错选择。 However, it is not a place that you will be able to examine when running a release app on a secured device, as neither ADB based tools nor Mass Storage or MTP access over USB have rights to it. 但是,在基于安全的设备上运行发行版应用程序时,这里不是您可以检查的地方,因为基于ADB的工具,通过USB的海量存储或MTP访问都没有权限。 So it's entirely possible that this code worked, but you had no way to discover that fact. 因此, 这段代码很有可能起作用,但是您无法发现这一事实。 If you are on an emulator, you can access this location with ADB or the DDMS browser, and if your apk is a debug one, you can use the run-as command line tool in the shell. 如果您使用的是仿真器,则可以使用ADB或DDMS浏览器访问此位置,如果apk是调试的,则可以在外壳程序中使用run-as命令行工具。

If you want to share the data, you might consider putting it on the External Storage instead. 如果要共享数据,则可以考虑将其放在外部存储上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM