简体   繁体   English

使用FileOutputStream保存文件不会创建任何文件

[英]Save file with FileOutputStream does not create any file

Currently I'm developing an android app where the user has to click on a button this adds +1 to a count. 目前,我正在开发一个android应用,其中用户必须单击一个按钮,这会将+1加到计数中。 After 100 there is another button which causes a reset of the count and increase the level and difficulty which is stored in another 2 "ints". 在100之后,还有另一个按钮会导致计数重置并增加级别和难度,该按钮存储在另外2个“整数”中。 Well its all working but I seriously have big problems with creating a save file. 一切正常,但是我在创建保存文件方面遇到了严重的问题。

  1. -I gave me the permission via AndroidManifest.xml -我通过AndroidManifest.xml给了我许可
  2. -Tryed 3 other code examples -尝试了3个其他代码示例
    • I did import everything that is necessary and the rest of the code is working 我确实导入了所有必要的东西,其余的代码都在工作

There has to be a mistake in this part of my code: 我的代码的这一部分一定有一个错误:

(my part for the Saving the "Stats") (我为保存“统计信息”所做的工作)

save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                FileOutputStream savelvl = openFileOutput("savelvl.data", Context.MODE_PRIVATE);
                savelvl.write(level);
                savelvl.close();

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


        }
    });

can anyone suggest an improvement to the code or tell me the mistake in saving the file to the internal storage? 谁能提出对代码的改进建议或告诉我将文件保存到内部存储器的错误?

You cannot flush your data. 您无法刷新数据。 When you use writer you must call flush method after write method. 使用writer时,必须在write方法之后调用flush方法。

OutputStrem stream = ... // I cannot create new reference because "OutStream" class is abstract
stream.write(data, offset, length);
stream.flush();
stream.close();

I've had success with using a BufferedWriter instead of FileOutputStream, although I'm sure it could given a different setup. 我已经成功使用BufferedWriter而不是FileOutputStream,尽管我确信它可以给出不同的设置。 Below is some code with "FileOutputStream out", which I was trying to use initially, commented out. 下面是一些我最初尝试使用的带有“ FileOutputStream out”的代码,已注释掉。

BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file,true));
            //out = new FileOutputStream(file);
            Log.i("file","file opened");
            writer.write(someString);
            //out.write(someString.getBytes());
            Log.i("file","file written");
        } catch (Exception e) {
            Log.e("fileNotFound","file not found exception");
            e.printStackTrace();
        }/*
        try {
            out.write(someString.getBytes());
            Log.i("file","file written");
            Log.i("str2File",someString.getBytes().toString());
        } catch (IOException e) {
            Log.e("fileWrite","file write error");
            e.printStackTrace();
        }*/ finally {
            try {
                //out.getFD().sync();
                //out.close();
                writer.close();
                Log.i("file","file closed");
            } catch (Exception e) {
                Log.e("closeError","error closing file");
                e.printStackTrace();
            }
        }

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

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