文本文件无法正确写入和存​​储数据

发布于 2024-11-10 15:39:37 字数 2510 浏览 5 评论 0原文

我在创建和编辑文本文件时遇到一些问题。该文件似乎从未存储过数据。

  • 我需要创建一个文本文件(如果有) 不可用。
  • 如果文件中有数据,请读取该数据并使其可用。
  • 存储的数据是一个 String,由三个整数值组成,用 , 分隔,例如:String finalWrite = "3,5,1"
  • 所以该字符串需要拆分并转换为整数以允许添加新计数器。
  • 这些新计数器需要写入设备上存储的文本文件中。

不会发生错误,也不会强制关闭。

我只能使用 Logcat 发现这些值没有正确存储。

我已查看 Android 开发网站上的文档。如果有人可以帮助或指出我正确的方向,我将不胜感激!

我正在使用的写入方法:

public void WriteItIn() throws IOException
{
    FileOutputStream fOut = openFileOutput("stats.txt", Context.MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut);
    ReadItIn(); //calls the read method, to get the values from the file
    int tmp1 = 0 + countertmp + counter;
    int tmp2 = 0 + counterpostmp + counterpos;
    int tmp3 = 0 + counternegtmp + counterneg;
    finalwrite = "" + tmp1 + "," + tmp2 + "," + tmp3;
    osw.write(finalwrite);
    osw.flush();
    osw.close();
}

读取方法:

public void ReadItIn() throws IOException
            {
                FileInputStream fIn = openFileInput("stats.txt");
                InputStreamReader isr = new InputStreamReader(fIn);
                char[] inputBuffer = new char[fIn.available()];
                isr.read(inputBuffer);
                stringFromFile = new String(inputBuffer);
                String [] tmp = stringFromFile.split("\\,");
                if(tmp.length > 0)
                {
                    Log.d("READ", " NOT NULL");
                    for(int i = 0;i<tmp.length ; i++)
                    {
                        String temper = tmp[i];
                        if(temper == null || temper == "")
                        {
                                Log.d("NULL", "NULLIFIED");
                        }
                        else
                            try
                        {
                            int x = Integer.parseInt(temper, 10);
                            if(i == 0){counter = x;}
                            else if(i == 1){counterpos = x;}
                            else if(i == 2){counterneg = x;}
                        }
                        catch(NumberFormatException e)
                        {
                            e.printStackTrace();
                        }
                    }   
                }
                else
                    Log.d("READ", "NULL");
            }

I am having some problems with creating and editing text files. The file never seems to store the data.

  • I need to create a text file, if one
    is not available.
  • If there is data in the file, read that data and make it available.
  • The data stored is a String comprising of three integer values, separated by a , Eg: String finalWrite = "3,5,1"
  • So this string needs to be split up, and converted into integers to allow for addition of new counters.
  • Those new counters need to be written into the text file stored on the device

There are no errors occurring, and no force closures.

I was only able to figure out that the values are not being stored properly, using Logcat.

I have reviewed documentation on the Android development site. If anyone can help or point me in the right direction, it would be much appreciated!

The write method that I am using:

public void WriteItIn() throws IOException
{
    FileOutputStream fOut = openFileOutput("stats.txt", Context.MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut);
    ReadItIn(); //calls the read method, to get the values from the file
    int tmp1 = 0 + countertmp + counter;
    int tmp2 = 0 + counterpostmp + counterpos;
    int tmp3 = 0 + counternegtmp + counterneg;
    finalwrite = "" + tmp1 + "," + tmp2 + "," + tmp3;
    osw.write(finalwrite);
    osw.flush();
    osw.close();
}

The Read method:

public void ReadItIn() throws IOException
            {
                FileInputStream fIn = openFileInput("stats.txt");
                InputStreamReader isr = new InputStreamReader(fIn);
                char[] inputBuffer = new char[fIn.available()];
                isr.read(inputBuffer);
                stringFromFile = new String(inputBuffer);
                String [] tmp = stringFromFile.split("\\,");
                if(tmp.length > 0)
                {
                    Log.d("READ", " NOT NULL");
                    for(int i = 0;i<tmp.length ; i++)
                    {
                        String temper = tmp[i];
                        if(temper == null || temper == "")
                        {
                                Log.d("NULL", "NULLIFIED");
                        }
                        else
                            try
                        {
                            int x = Integer.parseInt(temper, 10);
                            if(i == 0){counter = x;}
                            else if(i == 1){counterpos = x;}
                            else if(i == 2){counterneg = x;}
                        }
                        catch(NumberFormatException e)
                        {
                            e.printStackTrace();
                        }
                    }   
                }
                else
                    Log.d("READ", "NULL");
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神妖 2024-11-17 15:39:37

主要问题是,一旦您调用 openFileOutput,您的 stats.txt 文件就会一次又一次地被删除。

如果您尝试逐步调试代码,您会发现第一次运行应用程序时,调用 openFileOutput 时会创建大小为 0 的文件。您可以从 DDMS 文件资源管理器检查这一点。

因此,当您读取它时,它不包含任何内容,并且 ReadItIn 也不会读取任何内容。当您写入并关闭它时,您可以从DDMS文件资源管理器中看到该文件存在并且大小> 0,没错。

但是,当您再次通过 WriteItIn 时,只要调用 openFileOutput,您就可以从文件资源管理器中看到文件大小又回到 0。

The main problem is that as soon as you invoke openFileOutput, your stats.txt file gets erased every time again and again.

If you try to debug step by step your code, you can see that the first time you run the application, the file gets created with 0 size when you invoke openFileOutput. You can check this from the DDMS File explorer.

So when you read it it holds nothing, and nothing is read by ReadItIn. And when you write and close it, you can see from the DDMS File explorer that the file exists and has size > 0, rightly so.

But when you pass again by WriteItIn, as soon as you invoke openFileOutput, you can see from File explorer that the file size goes back to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文